Infix Postfix

This page illustrates stacks and queues for processing arithmetic expressions using Dijkstra's "shunting-yard algorithm".

An expression in Postfix notation, eg 3 4 5 * +, is unambiguous and readily evaluated using a stack.

A 'normal' infix expression, eg 3 + 4 * 5, is potentially ambiguous, as it could evaluate as 35 or 23. Conventionally, * has a higher precedence than +, so 4*5 is done before adding 3. This can be overridden using brackets, which are not needed in Postfix.

This page thus demonstrates how an infix expression is converted to postfix, using a combination of stacks and queues. The result is then evaluated, using a stack.

The user can type in a simple expression (single character numbers as well as + - * / or ^ and ( or ).

The algorithm moves numbers to the result queue and operators to the stack, but lower precedence operators move higher precedence operators to move to the result queue. Brackets are handled appropriately.

Press Convert to convert to Reverse Polish and then Evaluate to find the final value.

Expression

Animate