GATE Computer ScienceHardGATE CS (PYQ pattern)

A predictive translator uses E → T R; R → + { print('+') } T R | − { print('−') } T R | ε; T → num { print(num.val) }. (The print action in R comes before T.) What does it print for the input 9 − 5 + 2?

  1. A. 9 5 − 2 +
  2. B. 9 − 5 + 2
  3. C. − + 9 5 2
  4. D. 9 5 2 + −

Correct answer: B. 9 − 5 + 2

Actions run in the order they appear in the right-hand sides during the left-to-right derivation. T prints 9; R sees − and prints − before expanding T, which prints 5; then + is printed before 2. The operator is emitted between its operands, so the output is infix. (With the action after T, R → + T { print('+') } R, the familiar postfix 9 5 − 2 + appears.)

More Compiler Design questions

Exams that ask this topic