Stack applications: postfix expressions
Evaluate a postfix expression with a stack, and convert an infix expression with + − * / and parentheses to postfix with the shunting-yard rules, keeping operators left associative.
It opens on
In 5 1 2 + 4 * + 3 −, every operator works on the two numbers just before it. Which two numbers does the first + add?
Why it matters
A compiler or a calculator must turn a line such as 5 + (1 + 2) * 4 − 3 into an order of operations a machine can follow, and the stack that does it is the same two-line idea in a spreadsheet, an interpreter and a bytecode virtual machine.
Good to know first
- Stacks: push, pop and peek are O(1) on a Python list whose end is the top
- Queues: a line of tokens is read from the front in order, one at a time
- Defining and calling functions: a function takes a list of tokens and returns a value or a list
Step by step
Evaluate with a stack
Evaluate a postfix expression by hand with a stack, pushing numbers and letting each operator pop two values and push the result.
The tokens 5, 1 and 2 are numbers, so the stack grows to 5, 1, 2 with the 2 on top. The + pops the 2 and then the 1, adds them to get 3, and pushes 3, leaving 5, 3.
the rows through the first +
A common mix-up
The first + adds the first two numbers, 5 and 1, since they come first.
In fact: An operator takes the two values just before it, and those are 1 and 2, which are on top of the stack, with the 5 waiting underneath.
The evaluator in code
Write eval_postfix, pop the right operand before the left, and say what a malformed postfix expression does.
The expressions 2 3 4 * + and 2 3 + 4 * use the same numbers and the same two operators. The first multiplies 3 and 4 to get 12 and adds 2, giving 14, while the second adds 2 and 3 to get 5 and multiplies by 4, giving 20.
the first two rows
A common mix-up
In a − b the first value popped is the left operand a.
In fact: The first pop is the later pushed value b, the right operand, so the second pop is a, and with + and * the slip would not show.
Infix to postfix
Convert an infix expression without brackets to postfix with the shunting-yard rules, giving higher precedence its turn first and equal precedence to the left operator.
Take 2 + 3 * 4 − 5. The 2 goes out and the + waits. The 3 goes out. The * arrives, and the + on top has precedence 1, below 2, so nothing leaves and the * waits above it. The 4 goes out.
the rows through 4
A common mix-up
Operators of equal precedence can wait on the stack together, so a − b − c gives a b c − −.
In fact: Equal precedence is popped first, because subtraction and division are left associative, and a b c − − means a − (b − c).
Parentheses
Handle parentheses in the conversion and convert 5 + (1 + 2) * 4 − 3 to the postfix expression 5 1 2 + 4 * + 3 −, with its value 14.
The 5 goes out, the + waits, the ( is pushed, the 1 goes out, and the second + arrives with the ( on top, which has precedence 0, so nothing leaves and the + waits above it. The 2 goes out.
the rows through 2
A common mix-up
The brackets have to be written to the output as well.
In fact: They only steer the stack, and the postfix output has none.
Try it
What is the value of 4 6 2 − *, and what is on the stack after the − ?Answer
16; the stack holds 4 and 4 after the −
Hint: − pops 2 and 6 and pushes 4
What does eval_postfix return for 20 5 / 2 −?Answer
2.0, since 20 / 5 = 4.0 and 4.0 − 2 = 2.0
Hint: The / gives a float in Python
Convert 6 / 2 * 3 to postfix and give its value.Answer
6 2 / 3 * with value 9.0
Hint: When the * arrives, the / on top has the same precedence and leaves
Convert 9 / ( 1 + 2 ) * 4 and give its value.Answer
9 1 2 + / 4 * with value 12.0 (hint: the ) releases the +, then / and * have the same precedence, so the / leaves first)
The next tokens are 4 and then *. What does the stack hold after the *?Answer
5 and 12, since 4 is pushed and then the * pops 4 and 3 and pushes 12.
