GATE Computer ScienceHardGATE CS (PYQ pattern)

G1: L → L , x | x and G2: L → x , L | x generate the same language. A shift-reduce (LR) parser parses "x , x , x , x , x" (five x's) with each grammar. What is the largest number of grammar symbols on the stack at any moment with G1 and with G2?

  1. A. 9 with G1 and 3 with G2
  2. B. 3 with G1 and 5 with G2
  3. C. 5 with G1 and 9 with G2
  4. D. 3 with G1 and 9 with G2

Correct answer: D. 3 with G1 and 9 with G2

Left recursion lets G1 reduce as it goes: the stack never exceeds L , x (3 symbols). With right recursion nothing can be reduced until the last x is seen, so all 9 input symbols are on the stack together before the chain of L → x , L reductions begins.

More Compiler Design questions

Exams that ask this topic