Infix to Postfix Cheat Sheet
Everything you need for an exam answer on one page: operator precedence, the seven shunting-yard rules, worked examples and the mistakes that cost marks.
1 · Operator precedence
Higher binds tighter. Everything on one row has equal precedence.
| Level | Operators | Associativity |
|---|---|---|
| 3 (highest) | ^ | Right to left |
| 2 | * / % | Left to right |
| 1 (lowest) | + - | Left to right |
Brackets are not an operator — they override precedence entirely, and never appear in the postfix output.
2 · The algorithm, in seven rules
- Operand — send it straight to the output.
- Left bracket
(— push it on the stack. - Right bracket
)— pop to the output until the matching(appears; discard both brackets. - Operator, stack top has higher precedence — pop the top to the output, then test again.
- Operator, equal precedence and left-associative — pop, then test again.
- Operator, equal precedence and right-associative (
^) — do not pop; push the new operator. - End of input — pop everything left on the stack to the output.
The stack only ever holds operators and left brackets. Operands never touch it.
3 · Worked examples
| Infix | Postfix | What it tests |
|---|---|---|
A + B * C | A B C * + | Precedence: * before + |
(A + B) * C | A B + C * | Brackets beat precedence |
A + B - C | A B + C - | Equal precedence, left to right |
A ^ B ^ C | A B C ^ ^ | Right-associativity of ^ |
A * B + C / D | A B * C D / + | Two equal-precedence pairs |
4 · Mistakes that cost marks
- Treating
^as left-associative.A ^ B ^ CisA B C ^ ^, notA B ^ C ^. - Leaving brackets in the output. Postfix never contains a bracket — that is the whole point of it.
- Putting
%with^. Modulo sits with*and/at level 2. - Forgetting the final pop. Operators left on the stack at the end still have to reach the output, deepest last.
- Popping the
(to the output. On a)the matching(is discarded, not emitted.
5 · Reading it back
To evaluate postfix, scan left to right with a second stack: push operands,
and on an operator pop two values — second popped, operator, first popped
— then push the result. Order matters for - and /.
Prefix is the mirror image: reverse the infix expression, swap every bracket, run the same algorithm, then reverse the output.
infixtopostfixconverter.com — check any expression against the converter or drill yourself with the practice questions.