Computer science · Stack machines

Why machines prefer postfix.

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.

01 · Three notations

Same expression, three positions.

Each notation places the operator differently relative to its operands. The meaning is identical; the cost of evaluating it is not.

Infix
2 + 3
Operator between operands. Natural to read aloud, but the machine must apply precedence and parentheses to evaluate.
Prefix
+ 2 3
Operator first. Also called Polish notation, after Jan Łukasiewicz. Lisp uses this form.
Postfix
2 3 +
Operator last. Reverse Polish. Order alone encodes precedence — parentheses are never needed.
02 · Four examples

Translated, side by side.

Click any row to load that expression into the evaluator below.

InfixPostfixPrefix
03 · The advantage

Why postfix wins for evaluation.

Infix evaluation is a two-act problem; postfix is a one-act problem.

Infix
Scan first, then evaluate.
For 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.
Postfix
Scan and evaluate together.
For 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.
04 · The evaluator

Step through the stack machine.

Pick an expression, then advance one token at a time or play it through. Watch what the stack holds at every moment.

Speed
Stack ↑
Status
Ready.
Press Step or Play to begin.

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.

05 · In production

Where stack machines actually run.

Many of today's most heavily-used virtual machines use exactly this push-pop-compute model.

JVM
Java bytecode is a stack-based instruction set. iadd pops two ints, pushes their sum.
CPython VM
Python compiles to bytecode that runs on a stack-based interpreter loop.
.NET CLR
CIL — Common Intermediate Language — is stack-based, just like JVM bytecode.
WebAssembly
A stack machine designed for the web. Compact, fast, and easy to verify.
Ethereum EVM
Every smart contract on Ethereum runs on a 256-bit-word stack machine.
PostScript
The language driving most laser printers — an honest-to-goodness stack language.
Forth
A whole programming language built around two stacks. Still alive in firmware and embedded systems.
HP RPN calculators
The HP-12C and HP-48 series accept input directly in postfix. Engineers still swear by them.