GATE / CS ARCHIVE

GATE CSE 2006 / QUESTION 59

Translation scheme with nonstandard precedence

Compiler Design · Semantic Analysis & Type Checking · Full GATE CSE 2006 paper

GATE 2006, Q59 2 marks · MCQ
Report issue

Semantic Analysis & Type Checking

Question 59, GATE 2006: A syntax-directed translation has productions:
S→ER
R→*E {print('*')} R | ε
E→F+E {print('+')} | F
F→(S) | id {print(id.value)}
Here id denotes an integer. What is printed on input 2*3+4?
Options:
(A) 2*3+4
(B) 2*+34
(C) 23*4+
(D) 234+*
Tap to enlarge
Your answer
Answer & solution

Answer: D (no official key was published)

1. S→ER and R→*E{print(*)}R|ε put multiplication below the expression E; E→F+E{print(+)}|F groups the addition on the right. For 2*3+4, first print 2, then parse E for 3+4, printing 3,4,+, then print * when that E finishes.

2. The output is 234+*, option D. Applying ordinary arithmetic precedence would incorrectly give 23*4+.

3. Teaching check: Semantic print actions fire at their positions in the grammar, not according to the usual arithmetic-precedence rules. The E nonterminal consumes the whole 3+4 before R’s multiplication-print action runs. The result is therefore postfix for 2*(3+4).

Original paper, page 9