Relationship Between SLR and LALR Parser State Counts ( vs )
In LR-family parsing, SLR(1) and LALR(1) both use the same underlying LR(0) state automaton (same viable-prefix sets / kernels), and differ only in how lookahead symbols are used to place reduce actions in the parse table. In particular, the LALR(1) simplification merges/represents LR(1) states with identical LR(0) cores, and this process yields an automaton whose number of states equals the LR(0) (hence equals the SLR(1)) state count. Therefore, for a grammar , if the SLR parser has states and the LALR parser has states, we have: .3
A useful mental model is summarized by the hierarchy:
Here, LALR(1) is “more precise than SLR(1)” in its lookaheads, but it does not introduce additional LR(0) states—so the state count stays the same.2
Correct option: (i) .
Footnotes
-
LALR parser - Wikipedia - States are merged by identical kernel item sets; describes power vs SLR and how merging works. ↩ ↩2
-
LR parser - Wikipedia - States: LALR has the same states as SLR but uses more precise lookahead; also contrasts with canonical LR/CLR behavior. ↩ ↩2
-
SLR(1) vs LALR lookahead sets (University of Pennsylvania CIS 5110 notes) - SLR uses FOLLOW-based lookaheads; LALR uses sharper lookahead sets while relying on the LR(0) characteristic automaton structure. ↩
How LR lookahead methods affect states
No lookahead
LR(0)States are built from LR(0) items (viable prefixes / cores)."
Follow-based reduce placement
SLR(1)Use FOLLOW sets to decide where reductions go, but keep the same LR(0) states."
Merged lookaheads per LR(0) core
LALR(1)Merge LR(1) states with identical LR(0) cores; state count matches LR(0)/SLR."
Canonical LR(1) (splits cores)
CLR(1)May increase number of states because lookahead distinguishes otherwise identical cores."
Key concepts you need
- LR(0) core: LALR merges states with the same LR(0) core.
- Lookahead: affects reduce placement, not LR(0) state construction.
- FOLLOW set: used by SLR to place reductions.
- Merge by kernel: characteristic of LALR.
Why the state counts match ()
- LALR(1) “merges rules that have identical kernel item sets” (LR(0) cores), because LR(0) state construction does not yet know lookaheads.
- LALR and SLR both correspond to using the same LR(0) viable-prefix state automaton; they differ only in lookahead computations for reductions.2
- Multiple sources explicitly state that LALR has the same number of states as SLR (and LR(0)), with LALR being more powerful than SLR but not increasing state count.3
Footnotes
-
LR parser - Wikipedia - States: LALR has the same states as SLR but uses more precise lookahead; also contrasts with canonical LR/CLR behavior. ↩ ↩2
-
SLR(1) vs LALR lookahead sets (University of Pennsylvania CIS 5110 notes) - SLR uses FOLLOW-based lookaheads; LALR uses sharper lookahead sets while relying on the LR(0) characteristic automaton structure. ↩ ↩2
-
LALR parser - Wikipedia - States are merged by identical kernel item sets; describes power vs SLR and how merging works. ↩ ↩2
Exam-style reasoning for the relation between $n_1$ and $n_2$
- 1Step 1
SLR uses FOLLOW sets; LALR uses lookahead computed via merging LR(1) states but based on identical LR(0) cores.
- 2Step 2
LALR merges states with identical LR(0) kernels, so it does not create extra LR(0)-level states.
- 3Step 3
Therefore the number of parser states remains the same: .
Pro Tip
When a question asks only for the number of states, check whether the method changes the LR(0) state automaton. If it only refines lookahead placement, the state count often remains equal (as with SLR vs LALR).
Common confusion to avoid
LALR(1) is more powerful than SLR(1), but “more powerful” here comes from more precise lookaheads, not from introducing more LR(0) states. So you should not assume LALR has more states than SLR.
Relative state-count behavior across LR variants
State counts depend on how lookahead distinguishes (or merges) LR(0) cores.
Quick FAQ
Knowledge Check
Consider a grammar whose SLR parser has states and LALR parser has states. The relation between and is:
Explore Related Topics
Regular Expression Equivalence: Which Pairs Generate the Same Language?
Ambiguous Grammars in Formal Language Theory: Choosing the Correct Option
Hierarchy and Power of Bottom-Up Parsers: SLR, LALR, and CLR
Bottom‑up LR parsers form a strict hierarchy of power: LR(0) < SLR < LALR < CLR, with each level able to handle all grammars of the lower levels.
- SLR uses LR(0) items and resolves conflicts with FOLLOW sets; it is the weakest but simplest LR parser.
- LALR builds full LR(1) items then merges states that share the same core, keeping the same number of states as SLR while adding look‑ahead precision.
- CLR (canonical LR) retains all LR(1) states and look‑aheads, giving it the highest grammar coverage at the cost of many more states.
- Merging CLR states to form LALR can introduce reduce‑reduce conflicts (never shift‑reduce), making LALR strictly less powerful than CLR.
- In practice, tools like Yacc/Bison prefer LALR because it balances power with manageable memory usage.