Hierarchy and Power of Bottom-Up Parsers: SLR, LALR, and CLR
Bottom-up parsing is a widely used technique in compiler design. The most popular family of bottom-up parsers is the LR parser family. These parsers use a Parsing Table to manage state transitions.
The power of an LR parser refers to the class of grammars it can successfully parse without encountering Conflicts. The hierarchy of power is strictly defined as:
The Parser Hierarchy
The following diagram illustrates the inclusion relationship between these grammar classes. If a grammar is SLR, it is also LALR and CLR. However, the reverse is not necessarily true , .
Footnotes
-
GeeksforGeeks: SLR, CLR and LALR Parsers - Comparison of bottom-up parsing techniques and their hierarchies. ↩
-
TutorialsPoint: Difference between SLR, CLR, and LALR - Detailed breakdown of parser size and power. ↩
Comparison of LR(0), SLR(1), LALR(1), and CLR(1) Parsers
Comparison of Parser States and Power
Relative comparison based on a typical grammar with 'n' LR(0) states.
Determining Grammar Compatibility
- 1Step 1
Begin by creating the canonical collection of LR(0) items. If the parsing table has no conflicts, the grammar is LR(0).
- 2Step 2
If LR(0) has conflicts, use the FOLLOW set to resolve them. If successful, it is SLR.
- 3Step 3
If SLR fails, construct the canonical collection of LR(1) items using Lookahead. This creates the most states but resolves the most conflicts .
Footnotes
-
Prepp: Parser Power Comparison - Verification of the power hierarchy among LR parsers. ↩
-
- 4Step 4
To optimize CLR, merge states that have the same Core. If no reduce-reduce conflicts arise during merging, the grammar is LALR .
Footnotes
-
Codemia: LR, SLR, and LALR Parsers - Explains how state merging impacts conflict frequency in LALR. ↩
-
Uses items. It places 'reduce' actions in the parsing table only for terminals in for a production . It is the simplest but weakest LR parser.
The LALR Sweet Spot
In practice, most parser generators (like Yacc or Bison) use LALR. It provides a perfect balance: it is more powerful than SLR but avoids the 'state explosion' problem of CLR, which can sometimes produce thousands of states for a standard programming language .
Footnotes
-
TutorialsPoint: Difference between SLR, CLR, and LALR - Detailed breakdown of parser size and power. ↩
Reduce-Reduce Conflicts
When merging CLR states to create an LALR parser, you can NEVER introduce a shift-reduce conflict if one didn't exist in CLR. However, you CAN introduce a reduce-reduce conflict. This is why LALR is strictly less powerful than CLR .
Footnotes
-
Codemia: LR, SLR, and LALR Parsers - Explains how state merging impacts conflict frequency in LALR. ↩
Deep Dive: Parser Characteristics
Knowledge Check
Which one of the following statements is true?
Explore Related Topics
Which Grammar Type Is the Most Powerful? Understanding Type-0, Type-1, Type-2, and Type-3 Grammars
The most expressive grammar in the Chomsky hierarchy is the unrestricted Type‑0 grammar, which generates all recursively enumerable languages and matches the power of a Turing machine.
- The hierarchy is strict: , so each higher type can generate everything the lower types can, plus more.
- Type‑3 (regular) → finite automaton; Type‑2 (context‑free) → pushdown automaton; Type‑1 (context‑sensitive) → linear‑bounded automaton; Type‑0 (unrestricted) → Turing machine.
- Example languages: is context‑free but not regular; is context‑sensitive but not context‑free.
- “More powerful” refers to expressive capacity (ability to generate a larger class of languages), not ease of parsing or practical use.
Relationship Between SLR and LALR Parser State Counts ($n_1$ vs $n_2$)
Constructing an LALR(1) Parsing Table for a Given Grammar (and Proving It Is Not SLR(1))