Yasir Explains/Compiler Design/Intermediate Code Generation/Polish Notation
Intermediate Code Generation

Polish Notation

On this page

DefinitionFrom a Syntax TreeWhy It Is Parenthesis-Free and UnambiguousEvaluating Postfix with a StackRelation to Intermediate CodeKey Points
Intermediate Code Generation

Polish Notation

Polish notation puts the operator before its operands; reverse Polish (postfix) puts it after. Both are parenthesis-free. Learn how they come from tree traversals, why they need no precedence rules, and how a stack evaluates postfix as a linear intermediate representation.

Definition

Polish notation (prefix). A way of writing expressions in which the operator is written before its operands. For a binary operation, the form is op left right. The infix a + b becomes + a b.

Reverse Polish notation (postfix). The mirror image: the operator is written after its operands, as left right op. The infix a + b becomes a b +.

Both forms are parenthesis-free. The position of each operator relative to its operands already fixes the order of evaluation, so there is no need for parentheses and no need for precedence rules. This is the property that makes Polish notation so convenient for machines.

We will use one running expression throughout:

Example
Infix: (a + b) * (c - d)
Prefix (Polish): \* + a b - c d
Postfix (Reverse Polish): a b + c d - \*

From a Syntax Tree

Polish notation is not a trick — it falls straight out of the syntax tree of the expression. Here is the tree for (a + b) \* (c - d):

*
+
a
b
-
c
d

Now read the tree with two classic traversals:

  • Prefix = preorder traversal — visit the node first, then its left subtree, then its right subtree: \*, then + a b, then - c d.

    Prefix: \* + a b - c d

  • Postfix = postorder traversal — visit the left subtree, then the right subtree, then the node: a b +, then c d -, then \*.

    Postfix: a b + c d - \*

The infix (a + b) \* (c - d) itself corresponds to an inorder traversal — and inorder is exactly the traversal that loses information unless you add parentheses back in.

Why It Is Parenthesis-Free and Unambiguous

Infix notation is ambiguous on its own. Without rules, a + b \* c could mean "add then multiply" or "multiply then add". We patch this with precedence (\* binds tighter than +) and with parentheses to override precedence.

Polish notation needs neither. Compare the two readings:

Infix meaningPostfix
(a + b) \* ca b + c \*
a + b \* ca b c \* +

The two postfix strings are different sequences of tokens, so there is nothing to disambiguate — no brackets, no precedence table. The order of the operators alone tells you that in a b + c \* the + happens first, while in a b c \* + the \* happens first.

Evaluating Postfix with a Stack

Postfix has a simple, mechanical evaluation rule using a single stack.

Rule. Scan the tokens left to right. Push every operand onto the stack. On every operator, pop the top two values, apply the operator, and push the result. When the scan ends, the single value left on the stack is the answer.

Order matters for non-commutative operators. The first popped value is the right operand and the second popped is the left operand. So for - you compute left - right, not right - left.

Let us evaluate 5 3 + 8 2 - \* (the postfix of (a + b) \* (c - d) with a=5, b=3, c=8, d=2):

Example
Token Action Stack (bottom → top)
----- --------------------------------------- --------------------
5 push 5 [5]
3 push 3 [5, 3]
+ pop 3, pop 5; push 5 + 3 = 8 [8]
8 push 8 [8, 8]
2 push 2 [8, 8, 2]
- pop 2, pop 8; push 8 - 2 = 6 [8, 6]
* pop 6, pop 8; push 8 \* 6 = 48 [48]
----- --------------------------------------- --------------------
result 48

The final value is 48, matching (5 + 3) \* (8 - 2) = 8 \* 6 = 48.

Relation to Intermediate Code

Postfix is one of the simplest intermediate representations a compiler can emit. It is linear — just a flat list of tokens — and it carries no parentheses or precedence, so a tiny stack machine can execute it directly with the loop above. Many bytecode interpreters (and old calculators) are essentially postfix stack machines.

It also connects naturally to three-address code (TAC). Each operator in the postfix string corresponds to one operation that "pops two, computes, pushes one result". Naming each result with a temporary turns postfix into TAC:

Example
Postfix: a b + c d - \*
t1 = a + b
t2 = c - d
t3 = t1 \* t2

So postfix is essentially TAC with the temporaries left implicit on a stack instead of being named explicitly.

Key Points

  • Prefix = operator before operands (\* + a b - c d); postfix = operator after operands (a b + c d - \*).
  • Both are parenthesis-free and unambiguous — no precedence rules or brackets needed.
  • Prefix = preorder traversal of the syntax tree; postfix = postorder traversal.
  • Evaluate postfix with a stack: push operands; on an operator pop two, apply, push the result.
  • Mind operand order for - and /: first popped is the right operand.
  • Postfix is a compact linear intermediate representation runnable by a stack machine, closely related to three-address code.