Explore modules

Shunting Yard Algorithm

Convert an infix expression to space-separated postfix using an operator stack.

Free theory article

Convert 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.

  1. Open the Shunting Yard Algorithm module.
  2. Set the Expression input to (1+2)*3. Notice how this changes the order compared to the default expression.
  3. Click the Step button. The ( is pushed to the stack to act as a barrier.
  4. Continue clicking Step. The + is pushed above the parenthesis, and operands go to the output.
  5. When the ) is encountered, the stack pops everything until the matching parenthesis is found, flushing the + to the output.
  6. Finally, the * is processed. Click Finish to see the complete postfix notation 1 2 + 3 *.
Read the full free theory article