GATE / CS ARCHIVE

GATE CSE 2004 / QUESTION 38

Postfix with right-associative exponentiation

Data Structures · Stacks & Queues · Full GATE CSE 2004 paper

GATE 2004, Q38 2 marks · MCQ
Report issue

Stacks & Queues

Question 38, GATE 2004: Operators +,−,× are left-associative and ^ is right-associative. Precedence from highest to lowest is ^,×,+,−. Find postfix for a+b×c−d^e^f. Tokens in each alternative are separated for readability.
Options:
(A) a b c × + d e f ^ ^ −
(B) a b c × + d e ^ f ^ −
(C) a b + c × d − e ^ f ^
(D) − + a × b c ^ ^ d e f
Tap to enlarge
Your answer
Answer & solution

Answer: A (no official key was published)

Step through it (13 steps)

1. The grouping is(a+(b×c))−(de^f). Write each subtree’s operands before its operator: a b c × + d e f ^ ^ −.

2. Thus A. Right associativity places the ef operation before raising d to that result.

3. Teaching check: The parse first builds b×c and ef, then de^f, then a+(b×c), and finally subtraction. Postorder traversal emits operands before operators. The alternative with d e ^ f ^ instead encodes (de)f and violates right associativity.

Original paper, page 5