GATE Computer ScienceVery hardGATE CS (PYQ pattern)

Let G2: S → A A; A → a A | b and G3: S → A A A; A → a A | b. For each, how many more states does the canonical LR(1) automaton have than the LALR(1) automaton?

  1. A. 3 for G2 and 3 for G3
  2. B. 3 for G2 and 6 for G3
  3. C. 3 for G2 and 4 for G3
  4. D. 4 for G2 and 5 for G3

Correct answer: A. 3 for G2 and 3 for G3

The A-cores (after a, A → b·, A → a A·) split by the lookahead of the A they belong to. In G2 the first A is followed by FIRST(A) = {a, b} and the second by $: two copies of 3 cores, 3 extra. In G3 the first and second A are both followed by {a, b}, so they share the same LR(1) states; only the last A (lookahead $) adds copies: still 3 extra (11 versus 8 states).

More Compiler Design questions

Exams that ask this topic