GATE Computer ScienceVery hardGATE CS (PYQ pattern)

An LR parser for S → ( L ) | x; L → S | L , S parses "( ( x , x ) , x , ( x ) )". How many reductions does it perform (accept not counted), and what is the largest number of grammar symbols on its stack at any moment?

  1. A. 13 reductions, 8 symbols
  2. B. 11 reductions, 6 symbols
  3. C. 7 reductions, 5 symbols
  4. D. 13 reductions, 6 symbols

Correct answer: D. 13 reductions, 6 symbols

Each reduction builds one interior node of the parse tree: 7 S-nodes (4 for the x's, 2 for inner parentheses, the root) and 6 L-nodes (2 for (x,x), 1 for (x), 3 for the outer list) = 13. Thanks to left recursion each list is reduced as it grows, so the stack peaks just before the inner (x) is reduced: ( L , ( L ) — 6 symbols.

More Compiler Design questions

Exams that ask this topic