Compiler Design · CLR(1) and LALR(1) Parsing
Introduction to LALR(1) and CLR(1) Parsers
Overview
LALR(1) and CLR(1) (Canonical LR with one lookahead) are two LR parsers whose parsing tables are built from the canonical collection of LR(1) items.
They matter because all LR parsers share one parsing algorithm, so the only real design decision is how the parsing table — specifically the reduce move — is constructed.
Key concepts
- LR parser family: For all LR parsers, the parsing procedure and the parsing algorithm are the same. Only the construction of the parsing table varies.
- LR(0): During table construction, the reduce move is placed in the entire row (for every terminal).
- SLR(1): The reduce move is placed only in the FOLLOW of the left-hand-side non-terminal of the reducing production.
- CLR(1) and LALR(1): These are not built from the canonical collection of LR(0) items; they are built from the canonical collection of LR(1) items.
- LR(1) item: An LR(0) item together with a lookahead. The lookahead controls where the reduce move is placed.
How it works
- Keep the parsing algorithm identical to every other LR parser.
- Choose the item collection for table construction: LR(0) items for LR(0) and SLR(1); LR(1) items for CLR(1) and LALR(1).
- Place the reduce move according to the parser type:
- LR(0): entire row (all terminals).
- SLR(1): only in the FOLLOW of the LHS non-terminal of the reducing production.
- CLR(1) and LALR(1): determined by the lookaheads on the LR(1) item.
Drawing the diagram…
Comparison
| Parser | Item collection used for table construction | Where the reduce move is placed |
|---|---|---|
| LR(0) | Canonical collection of LR(0) items | Entire row (all terminals) |
| SLR(1) | Canonical collection of LR(0) items | Only in FOLLOW of the LHS non-terminal |
| CLR(1) | Canonical collection of LR(1) items | Determined by the lookaheads on the item |
| LALR(1) | Canonical collection of LR(1) items | Determined by the lookaheads on the item |
Revision summary
- The parsing algorithm never changes across LR parsers; only the parsing-table construction changes.
- LR(0) spreads the reduce move over the whole row — the least restrictive placement.
- SLR(1) narrows the reduce move to the FOLLOW set of the production's LHS.
- CLR(1) and LALR(1) switch the item collection from LR(0) items to LR(1) items.
- An LR(1) item = LR(0) item + lookahead; the lookahead decides the reduce placement.
- Trap: do not build CLR(1)/LALR(1) tables from LR(0) items — that is the LR(0)/SLR(1) route.
Previous-year exam insights
- The LR parser hierarchy by power of grammars accepted is: LR(0) ⊂ SLR(1) ⊂ LALR(1) ⊂ CLR(1) = LR(1);
every LALR(1) grammar is CLR(1), and every SLR(1) grammar is LALR(1), but not conversely.
Asked inUGC NET 2013 - LALR(1) tables are obtained from the canonical collection of LR(1) items by merging states that have the same core (same set of LR(0) items), keeping the union of their lookaheads; this can introduce reduce-reduce conflicts that CLR(1) does not have, but never shift-reduce conflicts.
- LR(k) with k > 1 adds no power over LR(1) at the language level: every language that has an LR(k) grammar has an LR(1) grammar, so any LR(k) grammar can be transformed into an equivalent LR(1) grammar.
A particular grammar can be LR(2) without being LR(1); it is the language, not the original grammar, that is guaranteed to be LR(1).
Asked inUGC NET 2014 - In SLR(1) the lookahead information is not attached to items; reduction uses only the FOLLOW set of the LHS non-terminal, whereas in CLR(1) and LALR(1) lookaheads are attached to each item and encode the left context of the parser.
Asked inUGC NET 2013 - Number of states: LR(0)/SLR(1) and LALR(1) tables have the same number of states (LALR merges LR(1) states with identical cores), while CLR(1) generally has many more states than LALR(1).
- A conflict in the table (shift-reduce or reduce-reduce) means the grammar is not of that class; CLR(1) is the most powerful LR parser and a grammar that is CLR(1) but not LALR(1) has a reduce-reduce conflict only after state merging.