Introduction

SLR places a reduction A→αA \to \alpha on every token in FOLLOW(AA). FOLLOW is a global set: it collects every token that can follow AA anywhere in the grammar, even when, in the particular state the parser is in, only a few of those tokens can actually come next. The false conflicts that result (the L = R grammar is the standard example) disappear if each item carries the exact tokens that may follow it in that context.

That is the idea of canonical LR(1) parsing (CLR, or simply LR(1)): items with a lookahead terminal. CLR is the most powerful of the one-token LR methods but can have many more states. LALR(1) (lookahead LR) merges CLR states that have the same items apart from the lookaheads. It has exactly as many states as SLR and LR(0), is almost as powerful as CLR, and is the method used by Yacc and Bison. This note builds both, compares all four LR methods, and explains what merging can and cannot break.

Prerequisites

  • LR(0) and SLR(1) Parsing: items, CLOSURE, GOTO, the LR driver and the conflict tests.
  • FIRST sets of strings (see the note FIRST and FOLLOW Sets), used to compute lookaheads.

Core concepts

TermDefinition
LR(1) item[A→α⋅β,  a][A \to \alpha \cdot \beta,\; a]: an LR(0) item (the core) plus a lookahead terminal aa (or $\text{\textdollar})
Meaning of the lookaheadReduce by A→αA \to \alpha in a state containing [A→α⋅,  a][A \to \alpha \cdot,\; a] only when the next token is aa
Canonical LR(1) collectionAll LR(1) item sets reachable from CLOSURE({[S′→⋅S,$]}\{[S' \to \cdot S, \text{\textdollar}]\})
Core of a stateThe set of LR(0) items obtained by dropping all lookaheads
LALR(1) statesThe CLR states with identical cores merged into one, lookaheads united

Lookahead restricts reduce actions only in completed items. In a noncomplete item it also propagates during closure through FIRST⁡(βa)\operatorname{FIRST}(\beta a); it is not merely carried unchanged until completion.

Algorithms

CLOSURE and GOTO with lookaheads

Text
CLOSURE1(I)
    repeat
        for each item [A -> alpha . B beta, a] in I, B a nonterminal
            for each production B -> gamma
                for each terminal b in FIRST(beta a)
                    add [B -> . gamma, b] to I
    until no item is added
    return I

GOTO1(I, X)
    return CLOSURE1({ [A -> alpha X . beta, a] : [A -> alpha . X beta, a] in I })

The lookahead of a new item is FIRST of what follows BB in the item, followed by the item's own lookahead. If β\beta is nullable, the lookahead aa itself is inherited; otherwise only FIRST(β\beta) is used.

The collection is built from CLOSURE1({[S′→⋅S,$]}\{[S' \to \cdot S, \text{\textdollar}]\}) with GOTO1, exactly as in the LR(0) case. Table filling: shift entries as in SLR; for [A→α⋅,a][A \to \alpha \cdot, a] with A≠S′A \ne S', ACTION[i,ai, a] == reduce A→αA \to \alpha, and only on aa; accept on [S′→S⋅,$][S' \to S \cdot, \text{\textdollar}].

LALR by merging

Text
LALR-FROM-CLR(C)
    group the CLR states by core
    for each group, form one state: the union of its LR(1) items
    GOTO of a merged state on X = the merged state containing GOTO of any member
    build the table as for CLR; conflicts in the merged table mean "not LALR(1)"

GOTO is well defined after merging, because states with the same core have successors with the same core. Building every CLR state first can be expensive; practical generators compute LALR lookaheads directly on the LR(0) automaton by propagating them, with the same result.

Worked examples

The grammar S→CCS \to C C, C→cC∣dC \to c C \mid d

The canonical LR(1) collection has 10 states:

StateItemsTransitions
I0I_0[S′→⋅S,$][S' \to \cdot S, \text{\textdollar}], [S→⋅CC,$][S \to \cdot C C, \text{\textdollar}], [C→⋅cC,c/d][C \to \cdot c C, c/d], [C→⋅d,c/d][C \to \cdot d, c/d]SS: 1, CC: 2, cc: 3, dd: 4
I1I_1[S′→S⋅,$][S' \to S \cdot, \text{\textdollar}]none
I2I_2[S→C⋅C,$][S \to C \cdot C, \text{\textdollar}], [C→⋅cC,$][C \to \cdot c C, \text{\textdollar}], [C→⋅d,$][C \to \cdot d, \text{\textdollar}]CC: 5, cc: 6, dd: 7
I3I_3[C→c⋅C,c/d][C \to c \cdot C, c/d], [C→⋅cC,c/d][C \to \cdot c C, c/d], [C→⋅d,c/d][C \to \cdot d, c/d]CC: 8, cc: 3, dd: 4
I4I_4[C→d⋅,c/d][C \to d \cdot, c/d]none
I5I_5[S→CC⋅,$][S \to C C \cdot, \text{\textdollar}]none
I6I_6[C→c⋅C,$][C \to c \cdot C, \text{\textdollar}], [C→⋅cC,$][C \to \cdot c C, \text{\textdollar}], [C→⋅d,$][C \to \cdot d, \text{\textdollar}]CC: 9, cc: 6, dd: 7
I7I_7[C→d⋅,$][C \to d \cdot, \text{\textdollar}]none
I8I_8[C→cC⋅,c/d][C \to c C \cdot, c/d]none
I9I_9[C→cC⋅,$][C \to c C \cdot, \text{\textdollar}]none

(c/dc/d abbreviates two items, one with each lookahead.) Why two copies: a CC derived for the first CC of S→CCS \to C C is followed by the second CC, which begins with c or d; a CC derived for the second one is followed by the end of input. States 3, 4, 8 serve the first CC; states 6, 7, 9 serve the second.

LALR merge. The pairs {I3,I6}\{I_3, I_6\}, {I4,I7}\{I_4, I_7\}, {I8,I9}\{I_8, I_9\} have the same cores. Merging them gives 7 states, the same number as the LR(0) automaton, with lookaheads c/d/$c/d/\text{\textdollar} in the merged complete items. No conflict arises, so the grammar is LALR(1); it is in fact LR(0).

Parsing c d d with the CLR table: shift 3, shift 4, reduce C→dC \to d, reduce C→cCC \to c C, shift 7, reduce C→dC \to d, reduce S→CCS \to C C, accept. The LALR parser does the same moves with state 4 in place of 7.

Error detection. On the invalid input c d (a single CC), the CLR parser stops in I4I_4 on $\text{\textdollar}: $\text{\textdollar} is not a lookahead there, so it reports the error before any reduction. The LALR parser, in the merged state with lookaheads c/d/$c/d/\text{\textdollar}, first reduces C→dC \to d and C→cCC \to c C and only then reports the error. LALR and SLR may perform extra reductions before detecting an error, but never shift an erroneous token.

Classes of grammars LR(0), SLR(1), LALR(1), LR(1) and LL(1) with example grammars
Figure 1. Strict inclusion LR(0) ⊂\subset SLR(1) ⊂\subset LALR(1) ⊂\subset LR(1) of grammar classes, with LL(1) inside LR(1) but cutting across the other three, and one example grammar placed in each region.

Computing lookaheads: I0I_0 of the L = R grammar

Start from [S′→⋅S,$][S' \to \cdot S, \text{\textdollar}] and close:

  1. SS follows the dot with nothing after it, so the lookahead is FIRST($\text{\textdollar}): add [S→⋅L=R,$][S \to \cdot L = R, \text{\textdollar}] and [S→⋅R,$][S \to \cdot R, \text{\textdollar}].
  2. In [S→⋅L=R,$][S \to \cdot L = R, \text{\textdollar}], LL is followed by = R, so the new LL-items get FIRST(=R $= R\, \text{\textdollar}) ={=}= \{=\}: [L→⋅∗R,=][L \to \cdot * R, =], [L→⋅id,=][L \to \cdot \textbf{id}, =].
  3. In [S→⋅R,$][S \to \cdot R, \text{\textdollar}], nothing follows RR: add [R→⋅L,$][R \to \cdot L, \text{\textdollar}].
  4. In [R→⋅L,$][R \to \cdot L, \text{\textdollar}], nothing follows LL: add [L→⋅∗R,$][L \to \cdot * R, \text{\textdollar}], [L→⋅id,$][L \to \cdot \textbf{id}, \text{\textdollar}].
I0={[S′→⋅S,$], [S→⋅L=R,$], [S→⋅R,$], [L→⋅∗R,=/$], [L→⋅id,=/$], [R→⋅L,$]}.I_0 = \{[S' \to \cdot S, \text{\textdollar}],\ [S \to \cdot L = R, \text{\textdollar}],\ [S \to \cdot R, \text{\textdollar}],\ [L \to \cdot * R, =/\text{\textdollar}],\ [L \to \cdot \textbf{id}, =/\text{\textdollar}],\ [R \to \cdot L, \text{\textdollar}]\}.

The item [R→⋅L,$][R \to \cdot L, \text{\textdollar}] has lookahead $\text{\textdollar} only. After reading LL, the state holds [S→L⋅=R,$][S \to L \cdot = R, \text{\textdollar}] and [R→L⋅,$][R \to L \cdot, \text{\textdollar}], so R→LR \to L is reduced on $\text{\textdollar} only, and = is free for the shift. SLR used FOLLOW(RR) ={=,$}= \{=, \text{\textdollar}\} instead, and conflicted.

Where SLR fails but LALR succeeds

For S→L=R∣RS \to L = R \mid R, L→∗R∣idL \to * R \mid \textbf{id}, R→LR \to L, the CLR collection has 14 states and LALR merges them to 10, the LR(0) count. The state that caused the SLR conflict now has items [S→L⋅=R,$][S \to L \cdot = R, \text{\textdollar}] and [R→L⋅,$][R \to L \cdot, \text{\textdollar}]: reduce only on $\text{\textdollar}, shift on =, no conflict. The grammar is LALR(1) and LR(1), but not SLR(1).

Where LALR fails but CLR succeeds

Text
S -> a A d | b B d | a B e | b A e
A -> c
B -> c

The CLR collection has 14 states, including

  • {[A→c⋅,d],  [B→c⋅,e]}\{[A \to c \cdot, d],\; [B \to c \cdot, e]\}, reached on a c, and
  • {[B→c⋅,d],  [A→c⋅,e]}\{[B \to c \cdot, d],\; [A \to c \cdot, e]\}, reached on b c.

Each is conflict-free, so the grammar is LR(1). They have the same core {A→c⋅,B→c⋅}\{A \to c \cdot, B \to c \cdot\}, so LALR merges them into {[A→c⋅,d/e],  [B→c⋅,d/e]}\{[A \to c \cdot, d/e],\; [B \to c \cdot, d/e]\}: on d and on e it can reduce by either production. LALR has 13 states and two reduce-reduce conflicts, so the grammar is not LALR(1). It is not SLR(1) either, since FOLLOW(AA) == FOLLOW(BB) ={d,e}= \{d, e\}.

Lookaheads separating two ε\varepsilon-reductions

For S -> A a A b | B b B a, A -> eps, B -> eps, the initial LR(1) state contains [A→⋅,a][A \to \cdot, a] and [B→⋅,b][B \to \cdot, b]: the lookahead of each is the terminal that follows the first AA or BB in its body. The four tables all have 10 states.

MethodConflicts in state 0
LR(0)reduce-reduce on a, b and $\text{\textdollar} (complete non-augmented production items reduce on every token; the completed augmented start accepts only on the endmarker)
SLR(1)reduce-reduce on a and b, since FOLLOW(AA) == FOLLOW(BB) ={a,b}= \{a, b\}
LALR(1), CLR(1)none: A→εA \to \varepsilon on a only, B→εB \to \varepsilon on b only

Why merging never creates a shift-reduce conflict

Suppose the merged state has a shift-reduce conflict on aa: it contains a complete item [A→α⋅,a][A \to \alpha \cdot, a] and an item [B→β⋅aγ,b][B \to \beta \cdot a \gamma, b]. The complete item came from some CLR state II in the group. All states in the group have the same core, so II also contains B→β⋅aγB \to \beta \cdot a \gamma (with some lookahead), and II shifts on aa. Then II itself already had the conflict, and the grammar was not LR(1). Hence: if a grammar is LR(1), its LALR table can have reduce-reduce conflicts but never shift-reduce conflicts.

Comparison of the LR methods

PropertyLR(0)SLR(1)LALR(1)CLR(1)
ItemsLR(0)LR(0)LR(1), merged by coreLR(1)
Ordinary reduction A→αA\to\alpha, A≠S′A\ne S', onevery tokenFOLLOW(AA)merged lookaheadsexact lookaheads
Number of statesnnnnnn≥n\ge n, often much more
Grammar classsmallestlargerlargerlargest of the four
Error detectionmay reduce firstmay reduce firstmay reduce firstbefore any reduction on the bad token

Here nn is the number of LR(0) states. The LALR lookahead set of a complete item is always a subset of FOLLOW of its head, which is why every SLR(1) grammar is LALR(1).

Grammar classes and languages

  • Grammars: LR(0) ⊂\subset SLR(1) ⊂\subset LALR(1) ⊂\subset LR(1), every inclusion strict, with the examples above as witnesses: the expression grammar (SLR, not LR(0)), the L = R grammar (LALR, not SLR), the a A d grammar (LR(1), not LALR).
  • LL(1) grammars are all LR(1). They are not all LALR(1): for example, S -> ( X | E ] | F ), X -> E ) | F ], E -> A, F -> A, A -> eps is LL(1) and LR(1), but its LALR table has reduce-reduce conflicts. S -> A a A b | B b B a, A -> eps, B -> eps is LL(1) and LALR(1) but not SLR(1).
  • Ambiguous grammars belong to none of these classes.
  • Languages: a language has an SLR(1) grammar if and only if it has an LALR(1) grammar, if and only if it has an LR(1) grammar, if and only if it is a deterministic context-free language (see Deterministic PDAs and DCFLs). The displayed ACTION-table convention uses an end marker to detect acceptance; do not transfer a prefix-property characterization from formal zero-lookahead models without end detection to this convention. So the methods differ in which grammars they accept, not in which languages they can describe with some grammar.

Important properties and observations

  • State counts. For the same grammar: LR(0) == SLR == LALR ≤\le CLR. Examples: 7, 7, 7, 10 for S→CCS \to C C; 10, 10, 10, 14 for the L = R grammar.
  • The expression grammar E→E+T∣TE \to E + T \mid T, T→T∗F∣FT \to T * F \mid F, F→(E)∣idF \to ( E ) \mid \textbf{id} has 12 states for LR(0), SLR and LALR, and 22 for CLR: 10 of the 12 cores occur in two CLR states that differ only in their lookaheads. In each pair, exactly one copy has $\text{\textdollar} among its lookaheads; the other is reached only inside parentheses, where ) plays the role of $\text{\textdollar}. Its only LR(0) conflicts are the shift-reduce entries of I2I_2 and I9I_9 on *; SLR, LALR and CLR have none.
  • What merging can do. Merge an LR(1) grammar's states and you may get new reduce-reduce conflicts, never new shift-reduce conflicts, and possibly later error detection.
  • Lookahead propagation. In CLOSURE1, the lookahead of [B→⋅γ,b][B \to \cdot\gamma, b] is FIRST(βa\beta a). Taking FOLLOW(BB) instead turns CLR into something close to SLR and loses its power.
  • Yacc and Bison build LALR(1) tables and resolve remaining conflicts by default rules (shift over reduce; the earlier production for reduce-reduce) or by declared precedence and associativity.

Common misconceptions.

  • "LALR has fewer states than SLR." It has the same number.
  • "LALR merging can introduce shift-reduce conflicts." Not for an LR(1) grammar.
  • "CLR parsers accept more languages than SLR parsers." They accept more grammars; the languages are the same, the deterministic context-free languages.
  • "Every LL(1) grammar is LALR(1)." No; it is LR(1), but some LL(1) grammars are not LALR(1).

GATE examination perspective

  • Containment questions. LR(0) ⊂\subset SLR ⊂\subset LALR ⊂\subset CLR for grammars; LL(1) ⊂\subset LR(1). Watch for options claiming LL(1) ⊂\subset LALR(1) or LL(1) ⊂\subset SLR(1): both are false.
  • State counts. "n1n_1 states for SLR, n2n_2 for LALR, n3n_3 for CLR": the true relation is n1=n2≤n3n_1 = n_2 \le n_3.
  • Merging conflicts. "If a CLR(1) table has no conflict, the LALR(1) table may have": reduce-reduce conflicts only.
  • Lookahead computation. Compute FIRST(βa\beta a) for the items added by closure; a nullable β\beta passes the parent's lookahead down.
  • One core, several lookaheads. In I0I_0 of the L = R grammar, L→⋅idL \to \cdot \textbf{id} appears with = (through S→⋅L=RS \to \cdot L = R) and with $\text{\textdollar} (through R→⋅LR \to \cdot L). Options that list only one lookahead are wrong.
  • ε\varepsilon-productions in LR(1) items. [A→⋅,a][A \to \cdot, a] is a complete item from the start; its lookahead decides where the empty reduction goes.
  • Which grammar is which. Locate the states with complete items and check lookaheads; for an LALR question, merge same-core states and recheck.

Summary

Sources

  1. A. V. Aho, M. S. Lam, R. Sethi, J. D. Ullman, Compilers, Principles, Techniques, and Tools, 2nd ed., Pearson Addison-Wesley, 2007: Section 4.7.1 (canonical LR(1) items), Section 4.7.2 (constructing LR(1) sets of items), Section 4.7.3 (canonical LR(1) parsing tables), Section 4.7.4 (constructing LALR parsing tables), Section 4.7.5 (efficient construction of LALR parsing tables), Section 4.9.1 (the parser generator Yacc).

Next steps