Loading lessons...
Expression Conversion & Evaluation (Infix, Postfix, Prefix)
Expression Conversion & Evaluation (Infix, Postfix, Prefix)
People write A + B × C and rely on precedence rules and brackets to know what to do first. A computer prefers a form with no brackets and no precedence rules - postfix - which it can evaluate in one left-to-right pass with a stack.
This lesson shows the three notations, how to convert between them by hand and with a stack, and how to evaluate a postfix expression. These are among the most frequently asked numericals in IPU papers.
1. Three Notations
| Notation | Operator position | Example of A + B | Example of (A + B) × C |
|---|---|---|---|
| Infix | Between the operands | A + B | (A + B) * C |
| Prefix (Polish) | Before the operands | + A B | * + A B C |
| Postfix (Reverse Polish) | After the operands | A B + | A B + C * |
Why postfix and prefix?
- They need no parentheses: the position of each operator already fixes the order of evaluation.
- They need no precedence rules while evaluating.
- A postfix expression is evaluated in one left-to-right scan using a single stack - this is how compilers and calculators work.
2. Precedence and Associativity
| Operator | Precedence | Associativity |
|---|---|---|
^ (exponent, also written $ or ↑) | Highest (3) | Right to left: A ^ B ^ C = A ^ (B ^ C) |
*, /, % | Middle (2) | Left to right: A − B − C = (A − B) − C |
+, - | Lowest (1) | Left to right |
Brackets override everything.
3. Manual Conversion (Full Parenthesization)
This is the quickest way to check an answer.
- Fully parenthesize the expression following precedence and associativity.
- For postfix, move each operator to the place of its closing bracket; for prefix, to its opening bracket.
- Remove all brackets.
Example: (A + B) * C + D + E * F - G
- Fully parenthesized:
((((A + B) * C) + D) + (E * F)) - G - Postfix:
A B + C * D + E F * + G - - Prefix:
- + + * + A B C D * E F G
4. Infix to Postfix Using a Stack
Idea: operands go straight to the output in the order they appear. Operators wait on a stack until we know that no operator of higher precedence still needs to come out before them.
Algorithm INFIX_TO_POSTFIX(Q, P)
// Q = infix expression, P = postfix output
PUSH "(" onto STACK and add ")" to the end of Q
for each symbol X of Q, from left to right, do
if X is an operand then
add X to P
else if X = "(" then
PUSH X
else if X is an operator then
while top of STACK is an operator with higher precedence than X,
or equal precedence and X is left-associative do
add POP() to P
PUSH X
else if X = ")" then
while top of STACK ≠ "(" do
add POP() to P
POP() // discard the "("
ExitHow it works
- An operator is pushed only after every operator on the stack that must be applied before it has been popped to the output.
(acts as a floor: nothing below it is popped until its matching)arrives.^is right-associative, so an incoming^does not pop another^.- At the end, the extra
)empties the stack.
In the trace tables below, × stands for the * operator.
Worked example 1: A + B * C - D / E
| Symbol | Stack | Postfix so far |
|---|---|---|
| A | A | |
| + | + | A |
| B | + | A B |
| × | + × | A B |
| C | + × | A B C |
| - | - | A B C × + |
| D | - | A B C × + D |
| / | - / | A B C × + D |
| E | - / | A B C × + D E |
| end | A B C × + D E / - |
When - arrives, * (higher) and + (equal, left-associative) are both popped before - is pushed.
Worked example 2: ((A - (B + C)) * D) ^ (E + F)
| Symbol | Stack | Postfix so far |
|---|---|---|
| ( | ( | |
| ( | ( ( | |
| A | ( ( | A |
| - | ( ( - | A |
| ( | ( ( - ( | A |
| B | ( ( - ( | A B |
| + | ( ( - ( + | A B |
| C | ( ( - ( + | A B C |
| ) | ( ( - | A B C + |
| ) | ( | A B C + - |
| × | ( × | A B C + - |
| D | ( × | A B C + - D |
| ) | A B C + - D × | |
| ^ | ^ | A B C + - D × |
| ( | ^ ( | A B C + - D × |
| E | ^ ( | A B C + - D × E |
| + | ^ ( + | A B C + - D × E |
| F | ^ ( + | A B C + - D × E F |
| ) | ^ | A B C + - D × E F + |
| end | A B C + - D × E F + ^ |
Worked example 3: A + (B * C - (D / E ^ F) * G) * H
| Symbol | Stack | Postfix so far |
|---|---|---|
| A | A | |
| + | + | A |
| ( | + ( | A |
| B | + ( | A B |
| × | + ( × | A B |
| C | + ( × | A B C |
| - | + ( - | A B C × |
| ( | + ( - ( | A B C × |
| D | + ( - ( | A B C × D |
| / | + ( - ( / | A B C × D |
| E | + ( - ( / | A B C × D E |
| ^ | + ( - ( / ^ | A B C × D E |
| F | + ( - ( / ^ | A B C × D E F |
| ) | + ( - | A B C × D E F ^ / |
| × | + ( - × | A B C × D E F ^ / |
| G | + ( - × | A B C × D E F ^ / G |
| ) | + | A B C × D E F ^ / G × - |
| × | + × | A B C × D E F ^ / G × - |
| H | + × | A B C × D E F ^ / G × - H |
| end | A B C × D E F ^ / G × - H × + |
Complexity: each symbol is pushed and popped at most once: O(n).
5. Infix to Prefix
Method (reverse-convert-reverse):
- Reverse the infix expression, swapping
(with). - Convert the result to postfix, with one change: for operators of equal precedence, pop only if the incoming operator is right-associative (i.e. do not pop equal-precedence
+ - * /). - Reverse the output - this is the prefix expression.
Example: (A + B) * C
- Reverse with swapped brackets:
C * (B + A) - Postfix of that:
C B A + * - Reverse:
* + A B C
Example: ((A - (B + C)) * D) ^ (E + F) has prefix ^ * - A + B C D + E F (check: the last operator applied, ^, comes first).
6. Evaluating a Postfix Expression
Idea: scan left to right. Push operands. When an operator appears, its operands are the top two stack entries: pop them, apply the operator, push the result.
Algorithm EVAL_POSTFIX(P)
for each symbol X of P, from left to right, do
if X is an operand then
PUSH X
else // X is an operator
B ← POP() // right operand (popped first)
A ← POP() // left operand
PUSH (A X B) // apply the operator
VALUE ← POP() // the only item left is the answer
return VALUEOrder matters: the first value popped is the right operand. For 8 2 / we pop B = 2, then A = 8, and compute 8 / 2 = 4, not 2 / 8.
Worked example: 6 2 3 + - 3 8 2 / + * 2 ^ 3 +
| Symbol | Action | Stack (bottom → top) |
|---|---|---|
| 6 | push | 6 |
| 2 | push | 6, 2 |
| 3 | push | 6, 2, 3 |
| + | 2 + 3 = 5 | 6, 5 |
| - | 6 − 5 = 1 | 1 |
| 3 | push | 1, 3 |
| 8 | push | 1, 3, 8 |
| 2 | push | 1, 3, 8, 2 |
| / | 8 / 2 = 4 | 1, 3, 4 |
| + | 3 + 4 = 7 | 1, 7 |
| × | 1 × 7 = 7 | 7 |
| 2 | push | 7, 2 |
| ^ | 7 ^ 2 = 49 | 49 |
| 3 | push | 49, 3 |
| + | 49 + 3 = 52 | 52 |
Result = 52.
Worked example: 5 6 2 + * 12 4 / -
| Symbol | Action | Stack |
|---|---|---|
| 5, 6, 2 | push | 5, 6, 2 |
| + | 6 + 2 = 8 | 5, 8 |
| × | 5 × 8 = 40 | 40 |
| 12, 4 | push | 40, 12, 4 |
| / | 12 / 4 = 3 | 40, 3 |
| - | 40 − 3 = 37 | 37 |
Result = 37.
Complexity: one scan, each operand pushed and popped once: O(n).
7. Evaluating a Prefix Expression
Scan right to left; push operands; on an operator pop A (first popped = left operand), then B, and push A op B.
Example: - + 2 3 * 4 1 → scanning from the right: push 1, 4; * gives 4 × 1 = 4; push 3, 2; + gives 2 + 3 = 5; - gives 5 − 4 = 1.
Key points to remember
- Postfix and prefix need no brackets and no precedence rules.
- Infix → postfix: operands go out directly; an operator first pops all stack operators of higher (or equal, left-associative) precedence; ) pops until (.
- ^ is right-associative: an incoming ^ does not pop a ^.
- Postfix evaluation: push operands; on an operator pop B then A and push A op B.
- Check every conversion with full parenthesization.
📝 IPU End-Term Exam Corner (5-10 Marks)
- Q1. Write the algorithm to convert an infix expression to postfix using a stack. Convert
((A - (B + C)) * D) ^ (E + F). (10 Marks) - Write: the precedence/associativity table,
INFIX_TO_POSTFIX, and the full three-column trace ending inA B C + - D * E F + ^. - Q2. Evaluate the postfix expression
6 2 3 + - 3 8 2 / + * 2 ^ 3 +using a stack. (5 Marks) - Write:
EVAL_POSTFIXand the trace table; answer 52. - Q3. Convert
(A + B) * C + D + E * F - Ginto prefix and postfix. (5 Marks) - Write: the fully parenthesized form, then both results.
- Q4. Why are postfix expressions preferred by compilers? (2.5 Marks)