Handling extra operators in Shunting-yard

Valid expressions can be validated with a regular expression, aside from parenthesis mismatching. (Mismatched parentheses will be caught by the shunting-yard algorithm as indicated in the wikipedia page, so I’m ignoring those.) The regular expression is as follows: PRE* OP POST* (INF PRE* OP POST*)* where: PRE is a prefix operator or ( POST is … Read more

Handling parenthesis while converting infix expressions to postfix expressions

You need to push the left parenthesis onto the stack, and process the stack like so when you encounter a right parenthesis: // opening ( if (in_fix.peek().type == 4) { post_fix.push(in_fix.pop()); } //closing ) if(in_fix.peek().type == 5){ while(!(post_fix.isEmpty() || post_fix.peek().type == 4)){ postfixstr.append(post_fix.pop()); } if (post_fix.isEmpty()) ; // ERROR – unmatched ) else post_fix.pop(); // … Read more