Consider the grammar S → (S) | a Let the number of states in SLR(1), LR(1) and…
2005
Consider the grammar
S → (S) | aLet the number of states in SLR(1), LR(1) and LALR(1) parsers for the grammar be n1, n2 and n3 respectively. The following relationship holds good
- A.
n1 < n2 < n3
- B.
n1 = n3 < n2
- C.
n1 = n2 = n3
- D.
n1 ≥ n3 ≥ n2
Attempted by 45 students.
Show answer & explanation
Correct answer: B
Key result: for the grammar S → (S) | a we get n1 = n3 < n2 (SLR(1) and LALR(1) have 6 states; canonical LR(1) has 9 states).
Reasoning (sketch):
SLR(1) (LR(0) cores): Build LR(0) item sets for the augmented grammar S' → S, S → (S), S → a. The distinct LR(0) item sets are: the initial set with S→.(...)/S→.a, states for reading '(' and 'a', states for the completed productions (S→a.) and (S→(S).) and the accept state. Counting these yields 6 distinct LR(0) states; hence SLR(1) has 6 states.
Canonical LR(1): When lookaheads are attached, several LR(0) cores split into multiple LR(1) states because items have different lookahead sets (for example, items after '(' carry different lookaheads ('$' vs ')'), and the reductions S→a and S→(S) appear with different lookaheads). This produces additional distinct LR(1) sets (distinct by lookahead) and the full canonical LR(1) automaton for this grammar has 9 states.
LALR(1): LALR(1) merges canonical LR(1) states that have the same LR(0) core. For this grammar those LR(1) states with identical cores but different lookaheads merge without introducing conflicts, so the LALR(1) automaton collapses down to the LR(0) core count of 6 states.
Conclusion: n1 = n3 = 6 and n2 = 9, therefore n1 = n3 < n2.