Consider the grammar:
Let the number of states in SLR (1), LR(1) and LALR(1) parsers for the grammar be and respectively. The following relationship holds good:
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:
- 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) () is always exactly equal to the number of states in LALR(1) ().
Thus, . - LR(1) States: An LR(1) parser keeps states with the same core but different lookaheads separate. For the given grammar , nested parentheses introduce different lookaheads for the same core items. For example:
- After seeing the first
(, the lookahead for is$. - After seeing the second
(, the lookahead for is).
- After seeing the first
Combining these, we get the relationship: .
Free practice · Your answers on this page are not saved.
