Skip to main content

5.4 CLR(1) & LALR(1) Parsing: LR(1) Items, State Merging & Parser Hierarchy

πŸ“šModule 01CLR(1) & LALR(1) Parsing Architecture⏱️5 min read
🎯High-Yield For:Semester Exams β€’ GATE CSE β€’ Technical Interviews

πŸ’‘ Core Intuition: The 3-Second Mental Model​

🍳 The Everyday Analogy: The High-Precision Sniper Scope vs Compact Monocular​

In Module 05 Topic 3, we discovered why SLR(1) failed on the pointer-assignment grammar (Sβ†’L=R∣RS \to L = R \mid R): its global FOLLOW(R)\text{FOLLOW}(R) set was too coarse, importing tokens from other unrelated derivations.

To overcome this, modern bottom-up parsing splits into two distinct engineering paths:

  1. Canonical LR (CLR(1)) β€” The High-Precision Sniper Scope:
    • Every single parsing item carries its own dedicated, context-sensitive lookahead token attached directly to the rule: [Aβ†’Ξ±β‹…Ξ²,Β a][A \to \alpha \cdot \beta, \ a].
    • It possesses the absolute maximum parsing power among all deterministic single-lookahead parsers.
    • The Catch: It generates an astronomical number of states (thousands of states for real languages like C++ or Rust), causing massive table bloat.
  2. Lookahead LR (LALR(1)) β€” The Compact Monocular:
    • An ingenious compression breakthrough: it identifies all CLR(1) states that have the exact same production core (the dot positions) and merges them into a single state, combining their lookahead sets.
    • The Miracle of LALR(1): It shrinks the state space back down to the exact compact size of an LR(0) table, while preserving virtually all of CLR(1)'s parsing power! This is why production compiler tools like Yacc and GNU Bison are built on LALR(1).

πŸ’» Bridging to Computer Science: Context-Sensitive Lookaheads & Core Merging​

In LR parsing theory:

  • CLR(1) tracks the exact right-sentential context: when a production Bβ†’Ξ³B \to \gamma is introduced during closure of Aβ†’Ξ±β‹…BΞ²A \to \alpha \cdot B \beta, its lookaheads are computed from FIRST(Ξ²a)\text{FIRST}(\beta a), propagating the outer lookahead aa only when Ξ²\beta is nullable.
  • LALR(1) leverages the fact that two states sharing the same LR(0) items make the exact same shift decisions. By merging core-identical states, LALR(1) guarantees that no Shift/Reduce conflicts can ever be introduced; only Reduce/Reduce conflicts can occasionally arise.

πŸ—οΈ Architectural Foundations: CLR(1) vs LALR(1)​

CLR(1) vs LALR(1): The Engineering Tradeoff

Contrasting uncompressed state accuracy against core-merged compactness

Maximum Power

Canonical LR (CLR(1))

🎯
Dominant Architecture / DomainTheoretical Upper Bound, Safety Critical Grammars
  • β€’Carries full context-sensitive lookahead tokens [A -> Ξ± β€’ Ξ², a].
  • β€’Parses any deterministic context-free grammar with 1 lookahead.
  • β€’Zero state merging; zero risk of introducing artificial conflicts.
  • β€’Generates massive state tables (e.g. 2000+ states for C++).
"Absolute precision with zero compromise, at the cost of massive memory."
Production Standard

Lookahead LR (LALR(1))

⚑
Dominant Architecture / DomainYacc, GNU Bison, Production Compiler Front-Ends
  • β€’Merges CLR(1) states that share identical LR(0) cores.
  • β€’Table size is identical to LR(0) and SLR(1) (e.g. ~250 states for C).
  • β€’Cannot create Shift/Reduce conflicts during merging.
  • β€’Can occasionally create Reduce/Reduce conflicts on edge-case grammars.
"Compresses state space by 80-90% with almost zero loss of parsing capability."

πŸ“ Formal Mathematical Foundations: LR(1) Items​

πŸ“Œ The Formal LR(1) Item​

An LR(1) Item consists of an LR(0) item augmented with a terminal lookahead symbol:

[Aβ†’Ξ±β‹…Ξ²,a]whereΒ a∈Σβˆͺ{$}[A \to \alpha \cdot \beta, \quad a] \quad \text{where } a \in \Sigma \cup \{ \$ \}

The lookahead aa acts as a strict execution constraint:

  • For a shift item ([Aβ†’Ξ±β‹…bΞ²,a][A \to \alpha \cdot b \beta, a]), lookahead aa has no effect; the parser shifts when it sees terminal bb.
  • For a completed reduce item ([Aβ†’Ξ³β‹…,a][A \to \gamma \cdot, a]), the parser executes Reduce Aβ†’Ξ³A \to \gamma if and only if the incoming lookahead token is exactly aa!

πŸ”„ The LR(1) CLOSURE Function​

Given a set of LR(1) items II:

  1. Basis: Add every item in II to CLOSURE(I)\text{CLOSURE}(I).

  2. Induction: If [Aβ†’Ξ±β‹…BΞ²,Β a]∈CLOSURE(I)[A \to \alpha \cdot B \beta, \ a] \in \text{CLOSURE}(I) and BB is a non-terminal, then for every production Bβ†’Ξ³B \to \gamma in the grammar and for every terminal b∈FIRST(Ξ²a)b \in \text{FIRST}(\beta a):

    add [B→⋅γ, b] to CLOSURE(I)\text{add } [B \to \cdot \gamma, \ b] \text{ to } \text{CLOSURE}(I)
  3. Repeat step 2 until no more items can be added.

The Lookahead Cascade Rule: In [A→α⋅Bβ,a][A \to \alpha \cdot B \beta, a], the lookahead bb for the derived rule B→⋅γB \to \cdot \gamma is computed strictly from FIRST(βa)\text{FIRST}(\beta a):

  • If Ξ²β‰ Ο΅\beta \neq \epsilon and does not derive Ο΅\epsilon, b∈FIRST(Ξ²)b \in \text{FIRST}(\beta). (The outer lookahead aa is ignored!).
  • If Ξ²β‡’βˆ—Ο΅\beta \Rightarrow^* \epsilon (or Ξ²\beta is empty), the outer lookahead aa propagates through into BB!

πŸš€ The LR(1) GOTO Function​

Let II be a set of LR(1) items, and X∈(VβˆͺΞ£)X \in (V \cup \Sigma):

GOTO(I,X)=CLOSURE({[Aβ†’Ξ±Xβ‹…Ξ²,Β a]∣[Aβ†’Ξ±β‹…XΞ²,Β a]∈I})\text{GOTO}(I, X) = \text{CLOSURE}\big( \{ [A \to \alpha X \cdot \beta, \ a] \mid [A \to \alpha \cdot X \beta, \ a] \in I \} \big)

🧩 The LALR(1) State Merging Algorithm​

Let CCLR={I0,I1,…,Ik}\mathcal{C}_{\text{CLR}} = \{ I_0, I_1, \dots, I_k \} be the canonical collection of LR(1) states.

🎯 Definition of the Core​

The Core of an LR(1) item [A→α⋅β, a][A \to \alpha \cdot \beta, \ a] is simply the LR(0) item A→α⋅βA \to \alpha \cdot \beta, discarding the lookahead aa.
Two states IiI_i and IjI_j are core-identical if they contain the exact same set of LR(0) cores:

Core(Ii)=Core(Ij)\text{Core}(I_i) = \text{Core}(I_j)

πŸ”„ State Merging​

Find all groups of states in CCLR\mathcal{C}_{\text{CLR}} that share identical cores. Replace each group with a single merged state whose lookahead set is the union of the individual lookahead sets:

Iij=IiβˆͺIj={[Aβ†’Ξ±β‹…Ξ²,Β a∣b]∣[Aβ†’Ξ±β‹…Ξ²,a]∈Ii∧[Aβ†’Ξ±β‹…Ξ²,b]∈Ij}I_{ij} = I_i \cup I_j = \{ [A \to \alpha \cdot \beta, \ a \mid b] \mid [A \to \alpha \cdot \beta, a] \in I_i \land [A \to \alpha \cdot \beta, b] \in I_j \}

πŸ›οΈ The State Count Invariance Theorem​

A cornerstone theorem of compiler theory governs bottom-up parsing table dimensions:

πŸ“The State Count Invariance Theoremtheorem
#states(LR(0))=#states(SLR(1))=#states(LALR(1))≀#states(CLR(1))\#\text{states}(\text{LR}(0)) = \#\text{states}(\text{SLR}(1)) = \#\text{states}(\text{LALR}(1)) \le \#\text{states}(\text{CLR}(1))
  • An LR(0) parser has NN states.
  • An SLR(1) parser has identically NN states (only table cell contents differ).
  • An LALR(1) parser has identically NN states (because merging all core-identical states collapses them back to the exact LR(0) state graph!).
  • A CLR(1) parser has MM states, where Mβ‰₯NM \ge N (typically 2Γ—2\times to 10Γ—10\times larger than NN).

πŸ’₯ The LALR(1) Conflict Invariants​

What happens when we merge core-identical states in LALR(1)?

πŸ›‘οΈ Theorem 1: Merging CANNOT Create Shift/Reduce Conflicts​

If a grammar is free of Shift/Reduce conflicts in CLR(1), it can NEVER have a Shift/Reduce conflict in LALR(1)!

πŸ“Theorem 1: Shift/Reduce Immunity Under State Mergingtheorem
G∈CLR(1)β€…β€ŠβŸΉβ€…β€ŠGΒ isΒ guaranteedΒ freeΒ ofΒ Shift/ReduceΒ conflictsΒ inΒ LALR(1)G \in \text{CLR}(1) \implies G \text{ is guaranteed free of Shift/Reduce conflicts in LALR}(1)
  • Analytical Proof: A shift action depends only on the core item (Aβ†’Ξ±β‹…aΞ²A \to \alpha \cdot a \beta), completely independent of the lookahead. Since the cores of merged states are identical, merging introduces no new shift transitions. If a reduce item's lookahead did not collide with the shift terminal aa in the separate CLR(1) states, it cannot collide in the merged state because the unioned lookaheads originated from states that already had disjoint lookaheads.

⚠️ Theorem 2: Merging CAN Create Reduce/Reduce Conflicts​

If two states IiI_i and IjI_j share the same core but have different lookaheads for two distinct productions:

πŸ“Theorem 2: Potential for Reduce/Reduce Merging Collisionstheorem
IiΒ andΒ IjΒ disjointΒ inΒ CLR(1)β†’UnionΒ LookaheadsIntersectionΒ onΒ terminalΒ aβ€…β€ŠβŸΉβ€…β€ŠR/RΒ ConflictΒ inΒ IijI_i \text{ and } I_j \text{ disjoint in CLR}(1) \xrightarrow{\text{Union Lookaheads}} \text{Intersection on terminal } a \implies \text{R/R Conflict in } I_{ij}
  • State IiI_i: Contains [Aβ†’Ξ³β‹…,a][A \to \gamma \cdot, a] and [Bβ†’Ξ΄β‹…,b][B \to \delta \cdot, b] (disjoint lookaheads aβ‰ bβ€…β€ŠβŸΉβ€…β€Ša \neq b \implies no conflict in CLR(1)!).

  • State IjI_j: Contains [Aβ†’Ξ³β‹…,c][A \to \gamma \cdot, c] and [Bβ†’Ξ΄β‹…,a][B \to \delta \cdot, a] (disjoint lookaheads cβ‰ aβ€…β€ŠβŸΉβ€…β€Šc \neq a \implies no conflict in CLR(1)!).

  • When merged into IijI_{ij}:

    Iij={[Aβ†’Ξ³β‹…,Β a∣c],[Bβ†’Ξ΄β‹…,Β a∣b]}I_{ij} = \{ [A \to \gamma \cdot, \ a \mid c], \quad [B \to \delta \cdot, \ a \mid b] \}

    Now, under column aa, both AA and BB demand a reduction! A fatal Reduce/Reduce Conflict has been introduced by state merging.


πŸ‘‘ The Universal Parser Expressive Power Hierarchy​

The Universal Formal Parser Expressive Hierarchy

Hierarchical superset containment of deterministic grammar classes

Universal Boundary

Unambiguous Context-Free Grammars

🌐

All non-ambiguous context-free languages that can be parsed deterministically.

Natural Language SubsetsComplex Domain Specific Languages
↓Deterministic subset
Most Powerful LR

Canonical LR: CLR(1)

🎯

Full uncompressed LR(1) items. Parses all deterministic CFGs with 1 lookahead.

No Core MergingZero R/R Merging ConflictsLargest State Tables
↓Core-merged subset
Industry Standard

Lookahead LR: LALR(1)

⚑

Compressed state tables (same size as LR(0)). Powers Yacc and GNU Bison.

Same States as LR(0)No S/R from MergingCan have R/R Conflicts
↓FOLLOW-filtered subset
Simple Lookahead

Simple LR: SLR(1)

πŸ”

Uses global FOLLOW sets on LR(0) states to filter reductions.

Fails on Assignment GrammarsIdentical States to LR(0)
↓Zero-lookahead subset
Base State Machine

Base LR: LR(0)

🧱

Row-wide unconditional reductions. Weakest bottom-up parser.

Blind ReductionsFails on Expression Grammars

βš–οΈ Formal Inclusion Relations​

LR(0)βŠ‚SLR(1)βŠ‚LALR(1)βŠ‚CLR(1)βŠ‚UnambiguousΒ CFG\text{LR}(0) \subset \text{SLR}(1) \subset \text{LALR}(1) \subset \text{CLR}(1) \subset \text{Unambiguous CFG}

Where Does LL(1) Fit in the Hierarchy?​

  • LL(1)βŠ‚LALR(1)βŠ‚CLR(1)\text{LL}(1) \subset \text{LALR}(1) \subset \text{CLR}(1): Every LL(1) grammar is always both LALR(1) and CLR(1).
  • LL(1)\text{LL}(1) and SLR(1)\text{SLR}(1) are INCOMPARABLE:
    • There exist grammars that are LL(1)\text{LL}(1) but NOT SLR(1)\text{SLR}(1) (e.g. Sβ†’aAb∣bAa,Β Aβ†’Ο΅S \to a A b \mid b A a, \ A \to \epsilon).
    • There exist grammars that are SLR(1)\text{SLR}(1) but NOT LL(1)\text{LL}(1) (e.g. any left-recursive expression grammar Eβ†’E+T∣TE \to E + T \mid T).

🏭 Real-World Production Context: How Industry Builds This​

In industrial compiler engines (such as GNU Bison, Yacc, and Menhir):

Production parser generators never construct the full CLR(1) state space and merge it afterward!

  • Building CLR(1) states for a full C or C++ grammar would allocate hundreds of thousands of items and exhaust memory.
  • Instead, generators use the DeRemer and Pennello Algorithm: they construct the compact NN-state LR(0) automaton directly and propagate lookahead sets over the transition digraph using graph reachability.

When Bison encounters a Reduce/Reduce conflict:

  • It outputs a compiler warning indicating the grammar is not LALR(1).
  • It breaks the tie by choosing the production that appears earlier in the grammar source file.

πŸ“ Numerical Applications & Worked Examples​

Example 1: CLR(1) State Construction and LALR(1) Core Merging​

Consider the grammar:

Sβ†’CCCβ†’cC∣d\begin{aligned} S &\to C C \\ C &\to c C \mid d \end{aligned}
  1. Construct the complete canonical collection of CLR(1) states.
  2. Group core-identical states and show how they merge into LALR(1) states.
  3. Compare the state count of CLR(1) vs LALR(1).

Step-by-Step Analytical Solution:

Step 1: Augmented Grammar with Initial Lookahead

(0)S′→S, $(1)S→CC(2)C→cC(3)C→d\begin{aligned} (0) \quad &S' \to S, \ \$ \\ (1) \quad &S \to C C \\ (2) \quad &C \to c C \\ (3) \quad &C \to d \end{aligned}

Step 2: Compute CLR(1) States

Starting with initial kernel [Sβ€²β†’β‹…S][S' \to \cdot S] with lookahead $:

I0=CLOSURE({[Sβ€²β†’β‹…S,Β $]})={[Sβ€²β†’β‹…S,Β $][Sβ†’β‹…CC,Β $][Cβ†’β‹…cC,Β c∣d][Cβ†’β‹…d,Β c∣d]I_0 = \text{CLOSURE}(\{ [S' \to \cdot S, \ \$] \}) = \begin{cases} [S' \to \cdot S, \ \$] \\ [S \to \cdot C C, \ \$] \\ [C \to \cdot c C, \ c \mid d] \\ [C \to \cdot d, \ c \mid d] \end{cases}

Transitions across grammar symbols:

I1=GOTO(I0,S)={[Sβ€²β†’Sβ‹…,Β $]}(AcceptΒ State)I2=GOTO(I0,C)={[Sβ†’Cβ‹…C,Β $],Β [Cβ†’β‹…cC,Β $],Β [Cβ†’β‹…d,Β $]}I3=GOTO(I0,c)={[Cβ†’cβ‹…C,Β c∣d],Β [Cβ†’β‹…cC,Β c∣d],Β [Cβ†’β‹…d,Β c∣d]}I4=GOTO(I0,d)={[Cβ†’dβ‹…,Β c∣d]}I5=GOTO(I2,C)={[Sβ†’CCβ‹…,Β $]}I6=GOTO(I2,c)={[Cβ†’cβ‹…C,Β $],Β [Cβ†’β‹…cC,Β $],Β [Cβ†’β‹…d,Β $]}I7=GOTO(I2,d)={[Cβ†’dβ‹…,Β $]}I8=GOTO(I3,C)={[Cβ†’cCβ‹…,Β c∣d]}I9=GOTO(I6,C)={[Cβ†’cCβ‹…,Β $]}\begin{aligned} I_1 &= \text{GOTO}(I_0, S) = \{ [S' \to S \cdot, \ \$] \} \quad \text{(Accept State)} \\ I_2 &= \text{GOTO}(I_0, C) = \{ [S \to C \cdot C, \ \$], \ [C \to \cdot c C, \ \$], \ [C \to \cdot d, \ \$] \} \\ I_3 &= \text{GOTO}(I_0, c) = \{ [C \to c \cdot C, \ c \mid d], \ [C \to \cdot c C, \ c \mid d], \ [C \to \cdot d, \ c \mid d] \} \\ I_4 &= \text{GOTO}(I_0, d) = \{ [C \to d \cdot, \ c \mid d] \} \\ I_5 &= \text{GOTO}(I_2, C) = \{ [S \to C C \cdot, \ \$] \} \\ I_6 &= \text{GOTO}(I_2, c) = \{ [C \to c \cdot C, \ \$], \ [C \to \cdot c C, \ \$], \ [C \to \cdot d, \ \$] \} \\ I_7 &= \text{GOTO}(I_2, d) = \{ [C \to d \cdot, \ \$] \} \\ I_8 &= \text{GOTO}(I_3, C) = \{ [C \to c C \cdot, \ c \mid d] \} \\ I_9 &= \text{GOTO}(I_6, C) = \{ [C \to c C \cdot, \ \$] \} \end{aligned}

Total CLR(1) States = 10 states (I0I_0 through I9I_9).

Step 3: Core-Identical State Merging into LALR(1) Examine the LR(0) cores of the 10 CLR(1) states:

  • States I3I_3 and I6I_6 share identical cores: {Cβ†’cβ‹…C,Β Cβ†’β‹…cC,Β Cβ†’β‹…d}\{ C \to c \cdot C, \ C \to \cdot c C, \ C \to \cdot d \}
    • Lookaheads of I3I_3: { c, d }
    • Lookaheads of I6I_6: { $ }
    • Merge into I36I_{36} with unioned lookaheads: { c, d, $ }
I36=I3βˆͺI6={[Cβ†’cβ‹…C,Β c∣d∣$],Β [Cβ†’β‹…cC,Β c∣d∣$],Β [Cβ†’β‹…d,Β c∣d∣$]}I_{36} = I_3 \cup I_6 = \{ [C \to c \cdot C, \ c \mid d \mid \$], \ [C \to \cdot c C, \ c \mid d \mid \$], \ [C \to \cdot d, \ c \mid d \mid \$] \}
  • States I4I_4 and I7I_7 share identical cores: {Cβ†’dβ‹…}\{ C \to d \cdot \}
    • Lookaheads of I4I_4: { c, d }
    • Lookaheads of I7I_7: { $ }
    • Merge into I47I_{47} with unioned lookaheads: { c, d, $ }
I47=I4βˆͺI7={[Cβ†’dβ‹…,Β c∣d∣$]}I_{47} = I_4 \cup I_7 = \{ [C \to d \cdot, \ c \mid d \mid \$] \}
  • States I8I_8 and I9I_9 share identical cores: {Cβ†’cCβ‹…}\{ C \to c C \cdot \}
    • Lookaheads of I8I_8: { c, d }
    • Lookaheads of I9I_9: { $ }
    • Merge into I89I_{89} with unioned lookaheads: { c, d, $ }
I89=I8βˆͺI9={[Cβ†’cCβ‹…,Β c∣d∣$]}I_{89} = I_8 \cup I_9 = \{ [C \to c C \cdot, \ c \mid d \mid \$] \}
  • Unique Core States: I0,I1,I2,I5I_0, I_1, I_2, I_5 have unique cores and remain unmerged.

Final LALR(1) States: I0,I1,I2,I5,I36,I47,I89I_0, I_1, I_2, I_5, I_{36}, I_{47}, I_{89}.
Total LALR(1) States = 7 states (reduced from 10 in CLR(1))!


Example 2: Formal Proof of Why Merging Cannot Introduce Shift/Reduce Conflicts​

Prove formally that if a grammar is conflict-free in CLR(1), the merged LALR(1) parser can never produce a Shift/Reduce conflict.

Step-by-Step Formal Proof:

  1. Suppose for the sake of contradiction that a merged LALR(1) state I12I_{12} suffers a Shift/Reduce conflict on terminal aa.

  2. By definition of a Shift/Reduce conflict, I12I_{12} must contain:

    • A shift item: [Aβ†’Ξ±β‹…aΞ²,Β b][A \to \alpha \cdot a \beta, \ b]
    • A reduce item: [Bβ†’Ξ³β‹…,Β a][B \to \gamma \cdot, \ a]
  3. Since I12I_{12} was formed by merging two core-identical CLR(1) states I1I_1 and I2I_2:

    • Both I1I_1 and I2I_2 share the exact same LR(0) core items.
    • Therefore, the shift item [Aβ†’Ξ±β‹…aΞ²][A \to \alpha \cdot a \beta] was present in both I1I_1 and I2I_2.
    • The reduce item [Bβ†’Ξ³β‹…][B \to \gamma \cdot] was also present in both I1I_1 and I2I_2.
  4. The lookahead aa in the reduce item [Bβ†’Ξ³β‹…,a][B \to \gamma \cdot, a] of the merged state must have come from either I1I_1 or I2I_2 (because Lookaheads(I12)=Lookaheads(I1)βˆͺLookaheads(I2)\text{Lookaheads}(I_{12}) = \text{Lookaheads}(I_1) \cup \text{Lookaheads}(I_2)).

  5. If aa came from I1I_1, then state I1I_1 in the CLR(1) collection contained:

    [Aβ†’Ξ±β‹…aΞ², … ]AND[Bβ†’Ξ³β‹…,Β a][A \to \alpha \cdot a \beta, \ \dots] \quad \text{AND} \quad [B \to \gamma \cdot, \ a]

    This means state I1I_1 already had a Shift/Reduce conflict in CLR(1)!

  6. This contradicts our premise that the grammar was conflict-free in CLR(1).

  7. Hence, merging core-identical states can NEVER introduce a Shift/Reduce conflict. β– \blacksquare


🎯 Exam & Interview Pitfall Check​

Core Conceptual Questions

Question 1: "Can merging CLR(1) states into LALR(1) ever introduce a Shift/Reduce conflict? Can it introduce a Reduce/Reduce conflict?"
Key Focus Points:

  • Shift/Reduce: Never. Core-identical states have identical shift actions. Lookahead unioning cannot cause an S/R conflict that was not already present in CLR(1).
  • Reduce/Reduce: Yes. If two distinct productions have disjoint lookaheads in separate CLR(1) states, unioning their lookaheads can create an overlap on terminal aa, producing an R/R conflict.

Question 2: "What is the exact relationship between the state counts of LR(0), SLR(1), LALR(1), and CLR(1)?"
Key Focus Points:

  • #states(LR(0))=#states(SLR(1))=#states(LALR(1))≀#states(CLR(1))\#\text{states}(\text{LR}(0)) = \#\text{states}(\text{SLR}(1)) = \#\text{states}(\text{LALR}(1)) \le \#\text{states}(\text{CLR}(1)).
  • LR(0), SLR(1), and LALR(1) always have the exact same number of DFA states. CLR(1) has more states whenever different derivation paths produce distinct lookaheads for the same core.
Common Interview Traps: Architectural Trade-Offs

Trap 1: Assuming LL(1) grammars are always SLR(1).
Answer: Classic trap. LL(1) and SLR(1) are incomparable classes! An LL(1) grammar can fail SLR(1) due to global FOLLOW set collisions. However, every LL(1) grammar is guaranteed to be LALR(1) and CLR(1).

Trap 2: Believing an LALR(1) parser can shift on an invalid token.
Answer: False. LALR(1) will never shift an incorrect terminal symbol. Due to state merging, it may perform a few extra harmless reductions before detecting a syntax error, but it halts and flags the error before shifting.


πŸ’¬

Discussion & Doubts