One page · Print or save as PDF

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.

Open the converter

1 · Operator precedence

Higher binds tighter. Everything on one row has equal precedence.

LevelOperatorsAssociativity
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

  1. Operand — send it straight to the output.
  2. Left bracket ( — push it on the stack.
  3. Right bracket ) — pop to the output until the matching ( appears; discard both brackets.
  4. Operator, stack top has higher precedence — pop the top to the output, then test again.
  5. Operator, equal precedence and left-associative — pop, then test again.
  6. Operator, equal precedence and right-associative (^) — do not pop; push the new operator.
  7. 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

InfixPostfixWhat it tests
A + B * CA B C * +Precedence: * before +
(A + B) * CA B + C *Brackets beat precedence
A + B - CA B + C -Equal precedence, left to right
A ^ B ^ CA B C ^ ^Right-associativity of ^
A * B + C / DA B * C D / +Two equal-precedence pairs

4 · Mistakes that cost marks

  • Treating ^ as left-associative. A ^ B ^ C is A B C ^ ^, not A 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.