GATE / CS ARCHIVE

GATE CSE 2016 SET 1 / QUESTION 46

GATE CSE 2016 Set 1 Q46

Compiler Design · Semantic Analysis & Type Checking · Full GATE CSE 2016 Set 1 paper

GATE 2016 Set 1, Q46 2 marks · MCQ
Report issue

Semantic Analysis & Type Checking

Question 46, GATE 2016: A syntax-directed translation scheme has S→aA {print 1}, S→a {print 2}, A→Sb {print 3}. A bottom-up parser processes input aab. What is the output? A:1 3 2. B:2 2 3. C:2 3 1. D:syntax error.
Tap to enlarge
Your answer
Answer & solution

Official answer: C

Step through it (8 steps)

Bottom-up parsing of aab first reduces the second a by S→a, printing 2. Then Sb reduces to A, printing 3. Finally aA reduces to S, printing 1. The action order is 2, 3, 1, option C.

Reduction order follows a rightmost derivation in reverse, not the textual order in which productions are listed. A valid derivation is S⇒aA⇒aSb⇒aab. Reversing those steps first collapses the second a into S, then Sb into A, then aA into S. Each semantic action is at the end of its production, so it runs at the corresponding reduction and emits 2,3,1. The successful derivation also rules out syntax error.

Original paper, page 18 · Answer key