Infix to Postfix Conversion and Evaluation for GATE: Stack Applications Solved

Learn the two stack procedures behind expression conversion and evaluation. Complete traces show what happens to every operator, operand and parenthesis.

KnowledgeGate Team

Exam prep & CS education

Updated 26 Aug 20265 min read

Infix-to-postfix questions become easy only when you stop trying to see the answer in your head. You must apply the precedence and associativity rule to every token, while keeping the operator stack separate from the output. Evaluation brings a different trap: the first value popped is the right operand, not the left one.

Both procedures are mechanical. The same stack discipline takes an expression from infix notation to its numerical result.

Why postfix notation is useful

In infix notation, an operator sits between its operands. The expression A + B * C needs a precedence rule to tell us that multiplication happens before addition. Parentheses can override that rule, as in (A + B) * C.

In postfix notation, an operator appears after its operands. The two expressions become:

  • A B C * + for A + B * C

  • A B + C * for (A + B) * C

Postfix needs neither parentheses nor a precedence decision during evaluation. A machine reads it from left to right and uses one operand stack.

Keep the two jobs distinct. Conversion stores operators on a stack and sends operands to the output. Evaluation stores operands and applies each operator as soon as it appears. If that distinction is still fuzzy, revise the basic stack operations in Stacks and Queues Explained before attempting longer expressions.

The operator ^ has the highest precedence and is right associative. Multiplication and division come next and are left associative. Addition and subtraction come last and are also left associative.

The infix-to-postfix conversion rule

Scan the infix expression one token at a time.

  1. If the token is an operand, append it directly to the output.

  2. If it is an operator o, pop operators from the stack while the top has higher precedence, or while it has equal precedence and o is left associative. Append every popped operator to the output, then push o.

  3. If it is a left parenthesis, push it.

  4. If it is a right parenthesis, pop operators to the output until the matching left parenthesis appears. Discard that parenthesis pair.

  5. At the end, pop every remaining operator to the output.

The equal-precedence clause carries associativity. When the incoming operator is left associative, an equal-precedence operator already on the stack must run first. When the incoming operator is the right-associative ^, an existing ^ stays on the stack.

Worked conversion: A + B * ( C - D )

Convert A + B * ( C - D ) token by token. The stack below is written from bottom to top.

Token

Action

Operator stack

Output

A

Append operand

empty

A

+

Push operator

+

A

B

Append operand

+

A B

*

+ has lower precedence, so push

+ *

A B

(

Push parenthesis

+ * (

A B

C

Append operand

+ * (

A B C

-

Top is (, so push

+ * ( -

A B C

D

Append operand

+ * ( -

A B C D

)

Pop -, then discard (

+ *

A B C D -

End

Pop *, then +

empty

A B C D - * +

The postfix expression is A B C D - * +. Its order says: subtract D from C, multiply that result by B, then add A.

Token-by-token conversion table for A + B * ( C - D ), ending in the postfix output A B C D - * +.

Notice what the right parenthesis did. It did not enter the output. It merely discharged operators until the matching left parenthesis, which was also discarded.

Worked evaluation with actual numbers

Now use the same expression shape with values: 5 + 3 * (8 - 4). Its postfix form is 5 3 8 4 - * +.

During evaluation, push every operand. On an operator, pop twice. Call the first popped value right and the second left, calculate left operator right, and push the result.

Token

Action

Operand stack, bottom to top

5

Push 5

5

3

Push 3

5, 3

8

Push 8

5, 3, 8

4

Push 4

5, 3, 8, 4

-

Pop 4, then 8; compute 8 - 4 = 4

5, 3, 4

*

Pop 4, then 3; compute 3 * 4 = 12

5, 12

+

Pop 12, then 5; compute 5 + 12 = 17

17

The final value is 17. A direct check gives 5 + 3 * (8 - 4) = 5 + 3 * 4 = 5 + 12 = 17, so the conversion and evaluation agree.

Operand stack after each token of 5 3 8 4 - * +, with two pops per operator and the final value 17.

This left-versus-right rule matters most for subtraction and division. The postfix fragment 8 4 - means 8 - 4, not 4 - 8. Likewise, 20 5 / means 20 / 5.

Traps that cost marks

Most wrong answers come from one of four mistakes.

  • Treating exponent as left associative. a ^ b ^ c means a ^ (b ^ c). When another ^ arrives, do not pop an equal-precedence ^ already on the stack.

  • Popping through a left parenthesis. No ordinary operator can remove (. Only its matching right parenthesis causes it to be discarded.

  • Reversing the operands in evaluation. The first pop supplies the right operand. Write right = pop() and left = pop() if the operation is not commutative.

  • Reversing prefix character by character. The standard shortcut reverses the token sequence, swaps left and right parentheses, converts the modified expression, and reverses the result. It is a token procedure, not a careless string flip.

Practise mixed expressions in the Data Structures Stacks and Queues MCQs, especially examples containing consecutive exponent operators and nested parentheses.

How GATE frames the question

GATE can ask for the postfix or prefix form of an infix expression, the value of a postfix expression, or the maximum stack depth during either procedure. A matching-parentheses question is the same operator-stack idea with fewer token types.

For the current subject weightage and paper pattern, use the official GATE 2027 portal from IIT Madras. The conversion and evaluation algorithms themselves do not change. Use a broad stack-application practice set instead of memorising one trace.

Short version and next step

For conversion, send operands straight to output and hold operators on the stack until precedence, associativity, a closing parenthesis, or the end tells you to pop. For evaluation, push operands and replace each operator plus its two operands with one result. Always remember that the first pop is the right operand.

Once you can convert and evaluate without guessing, move to timed sets in the GATE Test Series. Use GATE Guidance by Sanchit Sir to connect stack applications to the rest of Data Structures, then use the wider GATE category to plan the neighbouring topics.