Postfix Evaluation
Postfix notation places every operator right after its operands, for example A B C * + for A + B * C. That ordering already encodes exactly which operation happens first, so evaluating it doesn't need any precedence rules at all, just a single left-to-right scan with a stack.
Note
Prefix expressions are evaluated the same way, but scanned right-to-left instead of left-to-right, everything else about the pop-combine-push logic is identical. See Infix to Postfix Conversion for why postfix is the more commonly used of the two.
The evaluation algorithm
EvaluatePostfix(postfix)
1. Create empty stack S
2. For each token in postfix (left to right):
a) If token is an operand:
Push its value onto S
b) If token is an operator (+, -, *, /):
operand2 = Pop from S (this is popped first)
operand1 = Pop from S (this is popped second)
result = operand1 operator operand2
Push result onto S
3. answer = Pop from S (the final result)
4. Return answerEvery operand gets pushed as it's read. Every operator pops the two most recent values, combines them, and pushes the result back, so the stack always holds exactly the intermediate results still waiting to be combined.
Why operand order matters
Key Idea
For addition and multiplication the pop order doesn't matter, but for subtraction and division it does. The second value popped is always the left operand, and the first value popped is always the right operand.
postfix: A B -
Pop B first → this is the right operand
Pop A second → this is the left operand
Result = A - B, not B - AExample
Evaluate 5 3 -:
Push 5: Stack: [5]
Push 3: Stack: [5, 3]
Operator -:
operand2 = Pop() = 3 (right operand)
operand1 = Pop() = 5 (left operand)
result = 5 - 3 = 2
Result: 2Getting the pop order backwards here would silently compute 3 - 5 = -2 instead, the wrong answer, with no error to catch it. This is the single most common mistake in postfix evaluation, worth double-checking every time.
Worked example
Evaluate 2 3 * 5 +:
| Token | Action | Stack |
|---|---|---|
| 2 | Push | 2 |
| 3 | Push | 2, 3 |
| * | Pop 3, pop 2, push 2×3 = 6 | 6 |
| 5 | Push | 6, 5 |
| + | Pop 5, pop 6, push 6+5 = 11 | 11 |
At the end, exactly one value remains on the stack: the final answer, 11.
Practice
Q1.Evaluate 4 5 6 * - 3 +.Show answer
4 5 6 * - 3 +.-23
| Token | Action | Stack |
|---|---|---|
| 4 | Push | 4 |
| 5 | Push | 4, 5 |
| 6 | Push | 4, 5, 6 |
| * | Pop 6, pop 5, push 5 * 6 = 30 | 4, 30 |
| - | Pop 30 (right), pop 4 (left), push 4 - 30 = -26 | -26 |
| 3 | Push | -26, 3 |
| + | Pop 3, pop -26, push -26 + 3 = -23 | -23 |
Q2.Evaluate 10 2 3 * - 4 /.Show answer
10 2 3 * - 4 /.1
| Token | Action | Stack |
|---|---|---|
| 10 | Push | 10 |
| 2 | Push | 10, 2 |
| 3 | Push | 10, 2, 3 |
| * | Pop 3, pop 2, push 2 * 3 = 6 | 10, 6 |
| - | Pop 6 (right), pop 10 (left), push 10 - 6 = 4 | 4 |
| 4 | Push | 4, 4 |
| / | Pop 4 (right), pop 4 (left), push 4 / 4 = 1 | 1 |
Q3.Evaluate 5 1 2 + 4 * + 3 -.Show answer
5 1 2 + 4 * + 3 -.14
| Token | Action | Stack |
|---|---|---|
| 5 | Push | 5 |
| 1 | Push | 5, 1 |
| 2 | Push | 5, 1, 2 |
| + | Pop 2, pop 1, push 1 + 2 = 3 | 5, 3 |
| 4 | Push | 5, 3, 4 |
| * | Pop 4, pop 3, push 3 * 4 = 12 | 5, 12 |
| + | Pop 12, pop 5, push 5 + 12 = 17 | 17 |
| 3 | Push | 17, 3 |
| - | Pop 3 (right), pop 17 (left), push 17 - 3 = 14 | 14 |
Quick recall
- Postfix evaluation is a single left-to-right scan: push operands, and on each operator pop two values, combine them, and push the result back.
- The first value popped is the right operand, the second value popped is the left operand, this matters for subtraction and division specifically.
- At the end of the scan, the stack holds exactly one value, the result of the whole expression.