GATE Computer ScienceHardGATE CS (PYQ pattern)

An LR parser for E → id | E [ E ] | ( E ) parses "( id ) [ id ]". Each shift and each reduce is one move (the final accept is not a move). What is on the stack (grammar symbols, bottom to top) right after move 8, and how many moves are made in all?

  1. A. E [ E; 10 moves
  2. B. E [ E; 9 moves
  3. C. ( E ) [ E; 10 moves
  4. D. E [ id; 10 moves

Correct answer: A. E [ E; 10 moves

Moves: 1 shift (, 2 shift id, 3 reduce E → id, 4 shift ), 5 reduce E → ( E ) (the stack is now just E), 6 shift [, 7 shift id, 8 reduce E → id giving E [ E, 9 shift ], 10 reduce E → E [ E ]. Five shifts (one per token) and five reductions (one per E-node of the tree) make 10.

More Compiler Design questions

Exams that ask this topic