GATE Computer ScienceModerateGATE CS (PYQ pattern)

The grammar S → ( S ) S | ε is both LL(1) and SLR(1). On the input "( ( ) ) ( )", how many times does the LL(1) parser expand S → ε, and how many times does the SLR(1) parser reduce by S → ε?

  1. A. 4 and 4
  2. B. 4 and 3
  3. C. 5 and 5
  4. D. 3 and 4

Correct answer: A. 4 and 4

The grammar is unambiguous, so both parsers build the same parse tree; each uses S → ε once per ε-leaf. The tree is S → ( S1 ) S2 with S1 → ( ε ) ε and S2 → ( ε ) ε: four ε-leaves, hence 4 and 4 — the LL parser in leftmost order, the LR parser in reverse rightmost order.

More Compiler Design questions

Exams that ask this topic