Skip to content
Official PYQ · 2005

GATE CS 2005 Q60 — Parsing (LL, LR)

Explain with AI

Consider the grammar:

S→(S)∣aS \rightarrow (S) \mid a

Let the number of states in SLR (1), LR(1) and LALR(1) parsers for the grammar be n1,n2n_1, n_2 and n3n_3 respectively. The following relationship holds good:

Choose one option
View worked solution

Answer: B

To find the relationship between the number of states in SLR(1), LR(1), and LALR(1) parsers, we use the following standard properties of LR parsers:

  1. SLR(1) and LALR(1) States: The state construction of both SLR(1) and LALR(1) is based on the LR(0) state machine. LALR(1) is formed by merging the states of the LR(1) parser that share the same core (LR(0) items). Therefore, the number of states in SLR(1) (n1n_1) is always exactly equal to the number of states in LALR(1) (n3n_3).
    Thus, n1=n3n_1 = n_3.
  2. LR(1) States: An LR(1) parser keeps states with the same core but different lookaheads separate. For the given grammar S→(S)∣aS \rightarrow (S) \mid a, nested parentheses introduce different lookaheads for the same core items. For example:
    • After seeing the first (, the lookahead for SS is $.
    • After seeing the second (, the lookahead for SS is ).
    This splits the LR(1) states, making n2>n1n_2 > n_1.

Combining these, we get the relationship: n1=n3<n2n_1 = n_3 < n_2.

Free practice · Your answers on this page are not saved.

Published
Report a correction
Keep exploringMore Parsing (LL, LR) questions