Move the operator to the end and the whole problem of arithmetic precedence dissolves into a single linear scan. One stack, three rules, no parentheses — and most of today's virtual machines run on exactly this idea.
Each notation places the operator differently relative to its operands. The meaning is identical; the cost of evaluating it is not.
Click any row to load that expression into the evaluator below.
| Infix | Postfix | Prefix |
|---|
Infix evaluation is a two-act problem; postfix is a one-act problem.
2 + 3 * 5, the machine cannot fire when it reaches the + — the * arriving later has higher precedence. The whole expression must be parsed first; parentheses must be matched; precedence must be applied. Two passes, at minimum.2 3 5 * +, every operator can fire the moment it appears. Push operands, pop on operator, push the result. One pass, one stack — no precedence table, no parentheses.Pick an expression, then advance one token at a time or play it through. Watch what the stack holds at every moment.
Pop order matters. When the machine sees − or /, the second pop is the left operand and the first pop is the right. Get it backwards and 4 9 2 - * evaluates as 4 × (2 − 9) = −28 instead of 4 × (9 − 2) = 28.
The stack invariant. At every step the stack holds "results so far waiting to be combined." After the last token, exactly one value remains — that is the answer. If it is not exactly one, the expression was malformed. Stack-based VMs lean on this invariant for cheap verification: a single linear scan tells you whether a program is well-formed before you ever execute it.
Many of today's most heavily-used virtual machines use exactly this push-pop-compute model.
iadd pops two ints, pushes their sum.