Shunting Yard Algorithm
Convert an infix expression to space-separated postfix using an operator stack.
Free theory articleConvert an infix expression to space-separated postfix using an operator stack.
Concept & interpretation
The Shunting Yard Algorithm parses standard mathematical notation (infix) into Reverse Polish Notation (postfix), where operators follow their operands. Consider the expression 3+4*2. Since multiplication has a higher precedence than addition, it must be evaluated first.
Experiment with Precedence and Parentheses
You can interactively trace the Shunting Yard Algorithm in the module by stepping through the tokens.
- Open the Shunting Yard Algorithm module.
- Set the Expression input to
(1+2)*3. Notice how this changes the order compared to the default expression. - Click the Step button. The
(is pushed to the stack to act as a barrier. - Continue clicking Step. The
+is pushed above the parenthesis, and operands go to the output. - When the
)is encountered, the stack pops everything until the matching parenthesis is found, flushing the+to the output. - Finally, the
*is processed. Click Finish to see the complete postfix notation1 2 + 3 *.