Assume that the SLR parser for a grammar G has states and the LALR parser for G has states. The relationship between and is
View worked solution
Answer: B
To find the relationship between the number of states in an SLR parser () and an LALR parser () for any grammar , we can analyze how their state machines are constructed:
- SLR(1) Parser: The SLR(1) parser is constructed directly from the LR(0) collection of items. The lookahead sets are only used during the table construction phase (specifically for reduction actions using the sets). Thus, the number of states in the SLR(1) parser () is exactly equal to the number of states in the LR(0) automaton.
- LALR(1) Parser: The LALR(1) parser is constructed by taking the CLR(1) parser (which uses LR(1) items) and merging states that have the same core (i.e., the same set of LR(0) items, differing only in their lookahead sets). Since every state in the LR(0) automaton represents a unique core, merging all CLR(1) states with identical cores results in a state machine that has the exact same state structure as the LR(0) automaton. Thus, the number of states in the LALR(1) parser () is also exactly equal to the number of states in the LR(0) automaton.
Since both and are equal to the number of states in the LR(0) automaton for the grammar , we have:
Therefore, is necessarily equal to .
Free practice · Your answers on this page are not saved.
