Lesson 12 +35 XP

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

NotationOperator positionExample of A + BExample of (A + B) × C
InfixBetween the operandsA + B(A + B) * C
Prefix (Polish)Before the operands+ A B* + A B C
Postfix (Reverse Polish)After the operandsA 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

OperatorPrecedenceAssociativity
^ (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.

  1. Fully parenthesize the expression following precedence and associativity.
  2. For postfix, move each operator to the place of its closing bracket; for prefix, to its opening bracket.
  3. 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.

PSEUDO
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 "("
Exit

How 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

SymbolStackPostfix so far
AA
++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
endA 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)

SymbolStackPostfix 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 +
endA B C + - D × E F + ^

Worked example 3: A + (B * C - (D / E ^ F) * G) * H

SymbolStackPostfix so far
AA
++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
endA 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):

  1. Reverse the infix expression, swapping ( with ).
  2. 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 + - * /).
  3. 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 VALUE

Order 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 +

SymbolActionStack (bottom → top)
6push6
2push6, 2
3push6, 2, 3
+2 + 3 = 56, 5
-6 − 5 = 11
3push1, 3
8push1, 3, 8
2push1, 3, 8, 2
/8 / 2 = 41, 3, 4
+3 + 4 = 71, 7
×1 × 7 = 77
2push7, 2
^7 ^ 2 = 4949
3push49, 3
+49 + 3 = 5252

Result = 52.

Worked example: 5 6 2 + * 12 4 / -

SymbolActionStack
5, 6, 2push5, 6, 2
+6 + 2 = 85, 8
×5 × 8 = 4040
12, 4push40, 12, 4
/12 / 4 = 340, 3
-40 − 3 = 3737

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.

💡 Tip

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.

🎯 END-TERM EXAM PREP

📝 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 in A 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_POSTFIX and the trace table; answer 52.
  • Q3. Convert (A + B) * C + D + E * F - G into prefix and postfix. (5 Marks)
  • Write: the fully parenthesized form, then both results.
  • Q4. Why are postfix expressions preferred by compilers? (2.5 Marks)
Lesson 12 +35 XP

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

NotationOperator positionExample of A + BExample of (A + B) × C
InfixBetween the operandsA + B(A + B) * C
Prefix (Polish)Before the operands+ A B* + A B C
Postfix (Reverse Polish)After the operandsA 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

OperatorPrecedenceAssociativity
^ (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.

  1. Fully parenthesize the expression following precedence and associativity.
  2. For postfix, move each operator to the place of its closing bracket; for prefix, to its opening bracket.
  3. 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.

PSEUDO
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 "("
Exit

How 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

SymbolStackPostfix so far
AA
++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
endA 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)

SymbolStackPostfix 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 +
endA B C + - D × E F + ^

Worked example 3: A + (B * C - (D / E ^ F) * G) * H

SymbolStackPostfix so far
AA
++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
endA 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):

  1. Reverse the infix expression, swapping ( with ).
  2. 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 + - * /).
  3. 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 VALUE

Order 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 +

SymbolActionStack (bottom → top)
6push6
2push6, 2
3push6, 2, 3
+2 + 3 = 56, 5
-6 − 5 = 11
3push1, 3
8push1, 3, 8
2push1, 3, 8, 2
/8 / 2 = 41, 3, 4
+3 + 4 = 71, 7
×1 × 7 = 77
2push7, 2
^7 ^ 2 = 4949
3push49, 3
+49 + 3 = 5252

Result = 52.

Worked example: 5 6 2 + * 12 4 / -

SymbolActionStack
5, 6, 2push5, 6, 2
+6 + 2 = 85, 8
×5 × 8 = 4040
12, 4push40, 12, 4
/12 / 4 = 340, 3
-40 − 3 = 3737

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.

💡 Tip

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.

🎯 END-TERM EXAM PREP

📝 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 in A 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_POSTFIX and the trace table; answer 52.
  • Q3. Convert (A + B) * C + D + E * F - G into prefix and postfix. (5 Marks)
  • Write: the fully parenthesized form, then both results.
  • Q4. Why are postfix expressions preferred by compilers? (2.5 Marks)
or `↑`) | Highest (3) | **Right to left**: A ^ B ^ C = A ^ (B ^ C) |\n| `*`, `/`, `%` | Middle (2) | Left to right: A − B − C = (A − B) − C |\n| `+`, `-` | Lowest (1) | Left to right |\n\nBrackets override everything.\n\n## 3. Manual Conversion (Full Parenthesization)\n\nThis is the quickest way to check an answer.\n\n1. **Fully parenthesize** the expression following precedence and associativity.\n2. For **postfix**, move each operator to the place of its **closing** bracket; for **prefix**, to its **opening** bracket.\n3. Remove all brackets.\n\n**Example:** `(A + B) * C + D + E * F - G`\n- Fully parenthesized: `((((A + B) * C) + D) + (E * F)) - G`\n- **Postfix:** `A B + C * D + E F * + G -`\n- **Prefix:** `- + + * + A B C D * E F G`\n\n## 4. Infix to Postfix Using a Stack\n\n**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.\n\n```pseudo\nAlgorithm INFIX_TO_POSTFIX(Q, P)\n// Q = infix expression, P = postfix output\nPUSH \"(\" onto STACK and add \")\" to the end of Q\nfor each symbol X of Q, from left to right, do\n if X is an operand then\n add X to P\n else if X = \"(\" then\n PUSH X\n else if X is an operator then\n while top of STACK is an operator with higher precedence than X,\n or equal precedence and X is left-associative do\n add POP() to P\n PUSH X\n else if X = \")\" then\n while top of STACK ≠ \"(\" do\n add POP() to P\n POP() // discard the \"(\"\nExit\n```\n\n**How it works**\n- An operator is pushed only after every operator on the stack that must be applied **before** it has been popped to the output.\n- `(` acts as a floor: nothing below it is popped until its matching `)` arrives.\n- `^` is right-associative, so an incoming `^` does **not** pop another `^`.\n- At the end, the extra `)` empties the stack.\n\nIn the trace tables below, × stands for the `*` operator.\n\n### Worked example 1: `A + B * C - D / E`\n\n| Symbol | Stack | Postfix so far |\n|---|---|---|\n| A | | A |\n| + | + | A |\n| B | + | A B |\n| × | + × | A B |\n| C | + × | A B C |\n| - | - | A B C × + |\n| D | - | A B C × + D |\n| / | - / | A B C × + D |\n| E | - / | A B C × + D E |\n| end | | **A B C × + D E / -** |\n\nWhen `-` arrives, `*` (higher) and `+` (equal, left-associative) are both popped before `-` is pushed.\n\n### Worked example 2: `((A - (B + C)) * D) ^ (E + F)`\n\n| Symbol | Stack | Postfix so far |\n|---|---|---|\n| ( | ( | |\n| ( | ( ( | |\n| A | ( ( | A |\n| - | ( ( - | A |\n| ( | ( ( - ( | A |\n| B | ( ( - ( | A B |\n| + | ( ( - ( + | A B |\n| C | ( ( - ( + | A B C |\n| ) | ( ( - | A B C + |\n| ) | ( | A B C + - |\n| × | ( × | A B C + - |\n| D | ( × | A B C + - D |\n| ) | | A B C + - D × |\n| ^ | ^ | A B C + - D × |\n| ( | ^ ( | A B C + - D × |\n| E | ^ ( | A B C + - D × E |\n| + | ^ ( + | A B C + - D × E |\n| F | ^ ( + | A B C + - D × E F |\n| ) | ^ | A B C + - D × E F + |\n| end | | **A B C + - D × E F + ^** |\n\n### Worked example 3: `A + (B * C - (D / E ^ F) * G) * H`\n\n| Symbol | Stack | Postfix so far |\n|---|---|---|\n| A | | A |\n| + | + | A |\n| ( | + ( | A |\n| B | + ( | A B |\n| × | + ( × | A B |\n| C | + ( × | A B C |\n| - | + ( - | A B C × |\n| ( | + ( - ( | A B C × |\n| D | + ( - ( | A B C × D |\n| / | + ( - ( / | A B C × D |\n| E | + ( - ( / | A B C × D E |\n| ^ | + ( - ( / ^ | A B C × D E |\n| F | + ( - ( / ^ | A B C × D E F |\n| ) | + ( - | A B C × D E F ^ / |\n| × | + ( - × | A B C × D E F ^ / |\n| G | + ( - × | A B C × D E F ^ / G |\n| ) | + | A B C × D E F ^ / G × - |\n| × | + × | A B C × D E F ^ / G × - |\n| H | + × | A B C × D E F ^ / G × - H |\n| end | | **A B C × D E F ^ / G × - H × +** |\n\n**Complexity:** each symbol is pushed and popped at most once: $O(n)$.\n\n## 5. Infix to Prefix\n\n**Method (reverse–convert–reverse):**\n1. **Reverse** the infix expression, swapping `(` with `)`.\n2. 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 `+ - * /`).\n3. **Reverse** the output - this is the prefix expression.\n\n**Example:** `(A + B) * C`\n- Reverse with swapped brackets: `C * (B + A)`\n- Postfix of that: `C B A + *`\n- Reverse: **`* + A B C`**\n\n**Example:** `((A - (B + C)) * D) ^ (E + F)` has prefix **`^ * - A + B C D + E F`** (check: the last operator applied, `^`, comes first).\n\n## 6. Evaluating a Postfix Expression\n\n**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.\n\n```pseudo\nAlgorithm EVAL_POSTFIX(P)\nfor each symbol X of P, from left to right, do\n if X is an operand then\n PUSH X\n else // X is an operator\n B ← POP() // right operand (popped first)\n A ← POP() // left operand\n PUSH (A X B) // apply the operator\nVALUE ← POP() // the only item left is the answer\nreturn VALUE\n```\n\n```c\nint evalPostfix(const char *p) { /* single-digit operands */\n int st[100], top = -1;\n for (; *p; p++) {\n if (isdigit(*p)) st[++top] = *p - '0';\n else if (*p != ' ') {\n int b = st[top--], a = st[top--];\n switch (*p) {\n case '+': st[++top] = a + b; break;\n case '-': st[++top] = a - b; break;\n case '*': st[++top] = a * b; break;\n case '/': st[++top] = a / b; break;\n case '^': st[++top] = (int)pow(a, b); break;\n }\n }\n }\n return st[top];\n}\n```\n\n**Order 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.\n\n### Worked example: `6 2 3 + - 3 8 2 / + * 2 ^ 3 +`\n\n| Symbol | Action | Stack (bottom → top) |\n|---|---|---|\n| 6 | push | 6 |\n| 2 | push | 6, 2 |\n| 3 | push | 6, 2, 3 |\n| + | 2 + 3 = 5 | 6, 5 |\n| - | 6 − 5 = 1 | 1 |\n| 3 | push | 1, 3 |\n| 8 | push | 1, 3, 8 |\n| 2 | push | 1, 3, 8, 2 |\n| / | 8 / 2 = 4 | 1, 3, 4 |\n| + | 3 + 4 = 7 | 1, 7 |\n| × | 1 × 7 = 7 | 7 |\n| 2 | push | 7, 2 |\n| ^ | 7 ^ 2 = 49 | 49 |\n| 3 | push | 49, 3 |\n| + | 49 + 3 = 52 | 52 |\n\n**Result = 52.**\n\n### Worked example: `5 6 2 + * 12 4 / -`\n\n| Symbol | Action | Stack |\n|---|---|---|\n| 5, 6, 2 | push | 5, 6, 2 |\n| + | 6 + 2 = 8 | 5, 8 |\n| × | 5 × 8 = 40 | 40 |\n| 12, 4 | push | 40, 12, 4 |\n| / | 12 / 4 = 3 | 40, 3 |\n| - | 40 − 3 = 37 | 37 |\n\n**Result = 37.**\n\n**Complexity:** one scan, each operand pushed and popped once: $O(n)$.\n\n## 7. Evaluating a Prefix Expression\n\nScan **right to left**; push operands; on an operator pop A (first popped = **left** operand), then B, and push A op B.\n\n**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**.\n\n\u003e [!TIP]\n\u003e **Key points to remember**\n\u003e - Postfix and prefix need no brackets and no precedence rules.\n\u003e - Infix → postfix: operands go out directly; an operator first pops all stack operators of higher (or equal, left-associative) precedence; `)` pops until `(`.\n\u003e - `^` is right-associative: an incoming `^` does not pop a `^`.\n\u003e - Postfix evaluation: push operands; on an operator pop B then A and push A op B.\n\u003e - Check every conversion with full parenthesization.\n\n## 📝 IPU End-Term Exam Corner (5–10 Marks)\n- **Q1. Write the algorithm to convert an infix expression to postfix using a stack. Convert `((A - (B + C)) * D) ^ (E + F)`. (10 Marks)**\n - *Write:* the precedence/associativity table, `INFIX_TO_POSTFIX`, and the full three-column trace ending in `A B C + - D * E F + ^`.\n- **Q2. Evaluate the postfix expression `6 2 3 + - 3 8 2 / + * 2 ^ 3 +` using a stack. (5 Marks)**\n - *Write:* `EVAL_POSTFIX` and the trace table; answer 52.\n- **Q3. Convert `(A + B) * C + D + E * F - G` into prefix and postfix. (5 Marks)**\n - *Write:* the fully parenthesized form, then both results.\n- **Q4. Why are postfix expressions preferred by compilers? (2.5 Marks)**\n","createdAt":"2026-12-05T04:58:13.900Z","exercises":[{"type":"mcq","prompt":"What is the postfix form of A + B * C - D / E?","options":["A B + C * D E / -","A B C * + D E / -","A B C * D E / - +","+ A * B C - / D E"],"answer":"A B C * + D E / -","explanation":"B * C is applied first, then A + (B*C), then D / E, and finally the subtraction.","alternatives":[],"_id":"6b13a33bac2e6c8599010d98","pairs":[]},{"type":"fill","prompt":"The value of the postfix expression 6 2 3 + - 3 8 2 / + * 2 ^ 3 + is ____.","options":[],"answer":"52","explanation":"(6 − 5) × (3 + 4) = 7; 7^2 = 49; 49 + 3 = 52.","alternatives":[],"_id":"6b13a33bac2e6c8599010d99","pairs":[]},{"type":"mcq","prompt":"What is the prefix form of (A + B) * C?","options":["+ A B * C","* + A B C","A B + C *","* A + B C"],"answer":"* + A B C","explanation":"The multiplication is applied last, so it comes first in prefix.","alternatives":[],"_id":"6b13a33bac2e6c8599010d9a","pairs":[]},{"type":"mcq","prompt":"During postfix evaluation of `8 2 /`, the result is:","options":["0.25","4","16","10"],"answer":"4","explanation":"The first value popped (2) is the right operand: 8 / 2 = 4.","alternatives":[],"_id":"6b13a33bac2e6c8599010d9b","pairs":[]},{"type":"mcq","prompt":"Why does an incoming ^ not pop a ^ already on the stack?","options":["^ has the lowest precedence","^ is right-associative","^ is never stored on the stack","Brackets are required around ^"],"answer":"^ is right-associative","explanation":"A ^ B ^ C means A ^ (B ^ C), so the later ^ must be applied first.","alternatives":[],"_id":"6b13a33bac2e6c8599010d9c","pairs":[]},{"type":"fill","prompt":"The value of the postfix expression 5 6 2 + * 12 4 / - is ____.","options":[],"answer":"37","explanation":"5 × (6 + 2) = 40; 12 / 4 = 3; 40 − 3 = 37.","alternatives":[],"_id":"6b13a33bac2e6c8599010d9d","pairs":[]}],"order":12,"section":"Unit I: Foundations, Arrays & Lists","title":"Expression Conversion & Evaluation (Infix, Postfix, Prefix)","updatedAt":"2026-12-05T05:38:03.815Z","xp":35}}