GATE Computer ScienceHardGATE CS (PYQ pattern)

Productions are numbered (1) S → ( L ); (2) S → x; (3) L → S; (4) L → L , S. A shift-reduce parser parses "( x , ( x ) )". In which order are the productions used for reductions?

  1. A. 2, 3, 2, 1, 3, 4, 1
  2. B. 2, 3, 2, 3, 1, 4, 1
  3. C. 2, 3, 4, 2, 3, 1, 1
  4. D. 2, 2, 3, 3, 1, 4, 1

Correct answer: B. 2, 3, 2, 3, 1, 4, 1

x → S (2), S → L (3). Then , ( x ): x → S (2), S → L (3) inside the inner parentheses, ( L ) → S (1). Now the stack is ( L , S, so L , S → L (4), and finally ( L ) → S (1). This is the rightmost derivation in reverse.

More Compiler Design questions

Exams that ask this topic