Sub topic 16
CLR(1) and LALR(1) parsers
This chapter assumes you can build LR(0) item sets, compute FIRST and FOLLOW, and fill an SLR(1) table. SLR(1) uses FOLLOW(A) as the lookahead set for every reduction by A -> α. That is too coarse, and CLR(1) and LALR(1) fix it by tracking lookaheads more precisely.
Why SLR(1) is not enough
FOLLOW(A) collects every terminal that can follow A anywhere in a sentential form. In a particular parser state, only some of those terminals can legally follow the handle. If a state also has a shift on one of the extra terminals, SLR(1) reports a shift-reduce conflict that is not real. An LR(1) item carries the exact lookahead needed in that state.
LR(1) items and closure
An LR(1) item has the form [A -> α . β, a]. It means the parser has seen α, expects β next, and may reduce only if the next input symbol is a (a terminal or $).
The closure rule differs from LR(0) in how lookaheads are computed. For an item [A -> α . B β, a], add [B -> . γ, b] for every production B -> γ and every b in FIRST(βa). The goto operation is unchanged: advance the dot over the symbol and take the closure.
The start state is the closure of [S' -> . S, $]. The collection of all item sets reachable this way is the canonical LR(1) collection.
Worked example
Use this grammar, with productions numbered for reductions:
S -> C CC -> c CC -> d
The CLR(1) collection has ten states.
| State | Items (lookahead in brackets) |
|---|---|
| I0 | `S'->.S [$]`, `S->.CC [$]`, `C->.cC [c/d]`, `C->.d [c/d]` |
| I1 | `S'->S. [$]` |
| I2 | `S->C.C [$]`, `C->.cC [$]`, `C->.d [$]` |
| I3 | `C->c.C [c/d]`, `C->.cC [c/d]`, `C->.d [c/d]` |
| I4 | `C->d. [c/d]` |
| I5 | `S->CC. [$]` |
| I6 | `C->c.C [$]`, `C->.cC [$]`, `C->.d [$]` |
| I7 | `C->d. [$]` |
| I8 | `C->cC. [c/d]` |
| I9 | `C->cC. [$]` |
In I0, the lookahead of C -> .cC is FIRST(C $) = {c, d}. In I2, the first C is already consumed, so the lookahead is just $.
Look at I3 and I6. They have the same core, meaning the same items with lookaheads removed. They differ only in lookahead: I3 is used while reading the first C, I6 while reading the second.
CLR(1) table rules
- If
[A -> α . a β, b]is in state i and goto(i, a) = j, set ACTION[i, a] = shift j. - If
[A -> α ., a]is in state i with A ≠ S', set ACTION[i, a] = reduceA -> α. Only the lookaheadagets this entry, not all of FOLLOW(A). - If
[S' -> S ., $]is in state i, set ACTION[i, $] = accept. - Goto on nonterminals fills the GOTO table. Empty cells are errors.
A grammar is CLR(1) if this procedure never writes two different entries to one cell.
LALR(1): merging states with equal cores
CLR(1) tables can be large, because the number of states is often several times the LR(0) count. LALR(1) merges all states with the same core and takes the union of their lookaheads.
The ten CLR states become seven LALR states: 0, 1, 2, 36, 47, 5, 89. The LALR table is:
| State | c | d | $ | S | C |
|---|---|---|---|---|---|
| 0 | s36 | s47 | 1 | 2 | |
| 1 | acc | ||||
| 2 | s36 | s47 | 5 | ||
| 36 | s36 | s47 | 89 | ||
| 47 | r3 | r3 | r3 | ||
| 5 | r1 | ||||
| 89 | r2 | r2 | r2 |
An LALR table has exactly as many states as the LR(0) or SLR(1) table for the same grammar. This is why real tools such as Yacc and Bison use LALR(1).
What merging can and cannot break
Shift actions depend only on the core of a state, so merging never creates a shift-reduce conflict that CLR(1) did not already have. Merging can create a reduce-reduce conflict, because the union of lookaheads may make two reductions overlap.
Consider this grammar:
S -> a A d | b B d | a B e | b A e
A -> c
B -> cCLR(1) has two states after reading c. After a c the state is {A->c. [d], B->c. [e]}. After b c it is {A->c. [e], B->c. [d]}. Neither has a conflict. Their cores are identical, so LALR merges them into {A->c. [d/e], B->c. [d/e]}. Now input d or e allows both reductions, which is a reduce-reduce conflict. This grammar is CLR(1) but not LALR(1).
On erroneous input, LALR may perform extra reductions before it notices the error. It never shifts an invalid symbol, so the error is still caught before the next shift.
Comparing the parsers
| Parser | States | Lookahead for reduce | Power |
|---|---|---|---|
| LR(0) | fewest | none (all terminals) | weakest |
| SLR(1) | same as LR(0) | FOLLOW(A) | stronger than LR(0) |
| LALR(1) | same as LR(0) | merged exact lookaheads | stronger than SLR(1) |
| CLR(1) | most | exact lookaheads | strongest |
The grammar classes nest as LR(0) ⊂ SLR(1) ⊂ LALR(1) ⊂ CLR(1). LL(1) is a subset of LR(1), but it is not comparable with LR(0), SLR(1) or LALR(1) as a whole.
Exercise
For the grammar S -> C C, C -> c C | d, how many states do the CLR(1) and LALR(1) parsers have, and in which LALR state is production 3 reduced on c?
Show answer
CLR(1) has 10 states and LALR(1) has 7. Production C -> d is reduced in the merged state I47, which holds C->d. with lookaheads c, d and $. The entry ACTION[47, c] is r3.
Exercise
Why can merging same-core CLR(1) states create a reduce-reduce conflict but never a new shift-reduce conflict?
Show answer
Shift actions depend only on the item cores, and merged states share a core, so the shifts are unchanged. Reductions depend on lookaheads, and the union of two lookahead sets can make two different reductions apply to the same terminal.