Computer Engineering is coming soon

We're still writing and reviewing the notes for every subject, and we'll open it when every subject is ready. Meanwhile you can practise aptitude, ask Aria about any subject, or see the branches that are ready.

LR parsers: SLR, CLR and LALR

How LR parsers work (SLR, CLR/LR(1), LALR): items, closure/goto, constructing ACTION/GOTO tables, conflicts, and step-by-step examples of table construction and parsing.

Drafted with Aria, reviewed by the AiCanCode.org team · Updated . How we write and check our notes. Spotted an error? Use Give Feedback at the bottom of the page.

Why it matters

LR parsers (SLR, CLR and LALR) are used by compiler engineers when building the parser (a front-end phase) for programming languages that require deterministic bottom-up parsing. They give algorithms to build efficient parser tables automatically (YACC/Bison-like tools use LALR by default), and they determine whether a grammar is safely parseable with one-token lookahead.

Key ideas

  • Definition — LR parsing: Left-to-right scanning of input, constructing a Rightmost derivation in reverse. "L" = left-to-right, "R" = rightmost derivation, integer (usually 0 or 1) = number of lookahead tokens used.
  • Definition — item: a production with a dot · somewhere on RHS indicating how much of that production has been recognised.
    • LR(0) item: A production A -> α·β (no lookahead).
    • LR(1) item (also called a CLR item): A production with one-symbol lookahead: (A -> α·β, a) meaning we are in state after α and this item is valid when next input symbol (lookahead) can be a.
  • Algorithmic building blocks:
    • closure(I): add items whose LHS appears immediately after dots, recursively. (Define exactly below in algorithm form.)
    • goto(I,X): move the dot over symbol X in every item A->α·Xβ in I, then take closure of resulting items.
    • canonical collection: start with closure of [S'->·S] (with appropriate lookahead for LR(1)), repeatedly compute goto for all grammar symbols until no new sets appear. The resulting DFA recognizes the set of viable prefixes of the grammar (often asked in exams).
  • Definition — ACTION / GOTO table: For each state (an item set) and terminal a, ACTION[state,a] is shift s, reduce r, accept, or error. For nonterminals, GOTO[state,A] is a new state number.
  • Table construction (exact rules, assume augmented grammar S'->S and states are indexed):
    1. If state i contains item [A->α·aβ] where a is terminal and goto(Ii,a)=Ij then ACTION[i,a] = shift j.
    2. If state i contains item [A->α·] (dot at right end) for A != S' then for every terminal a in LOOK(A) put ACTION[i,a]=reduce A->α. The set LOOK(A) differs:
      • For SLR(1): LOOK(A) = FOLLOW(A).
      • For CLR/LR(1): use the lookahead a that is stored with the LR(1) item (each reduce entry uses that lookahead only).
      • For LALR(1): states are built by merging LR(1) states with the same LR(0) core; reduces use the union of lookaheads for merged items.
    3. If state i contains [S'->S·] then ACTION[i,$] = accept.
    4. For nonterminals A, if goto(Ii,A)=Ij then GOTO[i,A] = j.
  • Definitions of conflicts:
    • shift–reduce conflict: a cell wants both shift and reduce.
    • reduce–reduce conflict: a cell wants two different reductions. These indicate the grammar is not parseable by that LR variant.
  • Relationships (typical ordering of power): LR(0) ⊂ SLR(1) ⊂ LALR(1) ⊂ CLR (LR(1)). (Explanation: CLR uses full 1-token lookahead in every item so is most powerful; LALR merges compatible LR(1) states and is always at least as powerful as SLR (a strict superset); LR(0) is the weakest.)

Exact algorithms (to be drawn in exam answers):

  • Draw: a directed graph of item-sets (states). Label nodes with the set of items (write each production with dot and for LR(1) include lookahead after a comma). Label edges with the grammar symbol X used by goto.
  • Draw parsing stack diagram for a trace: stack content shows alternating states and grammar symbols, current input pointer, and ACTION taken (shift/reduce/accept) at each step.

Formulas

closure(I): start with items in I; while there is an item [A->α·Bβ (, a)] with nonterminal B immediately right of · and B->γ in productions, add item [B->·γ (, b)] for each b in FIRST(βa) (for LR(1)) or just [B->·γ] (for LR(0)). Repeat until no new items.

  • FIRST & FOLLOW (syntax to remember): FIRST(X) = set of terminals that can appear at the start of strings derived from X. FOLLOW(A) = set of terminals that can appear immediately to the right of A in some sentential form (include $ for start symbol). ACTION[i, a] = shift j when goto(Ii, a) = Ij and a is terminal. If [A->α·] in Ii and A != S' then for each a in LOOK(A): ACTION[i,a] = reduce A->α. If [S'->S·] in Ii then ACTION[i,$] = accept. GOTO[i, A] = j when goto(Ii, A) = Ij and A nonterminal. (Units: these are algorithms/syntax, no SI units.)

Worked examples

Example 1 — SLR table construction and parse trace (standard exam question) Grammar (augmented): 0: S' -> S 1: S -> C C 2: C -> c C 3: C -> d Step 1: compute canonical LR(0) item-sets (describe on answer sheet and label I0..I6). Draw each item set; show edges labelled c, d, C, S. Key states (you should produce these sets in exam): I0: S'->·S, S->·C C, C->·c C, C->·d I1: C->c·C, C->·c C, C->·d I2: C->d· I3: S->C·C, C->·c C, C->·d I4: C->c C· I5: S->C C· I6: S'->S· Step 2: compute FIRST & FOLLOW: FIRST(C) = {c, d}; FOLLOW(C) = {c, d, $} ; FOLLOW(S) = {$}. Step 3: build SLR ACTION/GOTO using rules above. (List table rows; in exam draw table with terminal columns c,d,$ and nonterminal column C,S and fill:)

  • Example table entries (show in answer):
    • ACTION[0,c] = s1, ACTION[0,d] = s2, GOTO[0,C] = 3, GOTO[0,S] = 6.
    • ACTION[1,c] = s1, ACTION[1,d] = s2, GOTO[1,C] = 4.
    • ACTION[2,c] = r3, ACTION[2,d] = r3, ACTION[2,$] = r3.
    • ACTION[3,c] = s1, ACTION[3,d] = s2, GOTO[3,C] = 5.
    • ACTION[4,c] = r2, ACTION[4,d] = r2, ACTION[4,$] = r2.
    • ACTION[5,$] = r1.
    • ACTION[6,$] = accept. Step 4: parse input cdcd (with $ appended). Show stack and actions step-by-step (push states/symbols):
  1. [0], input c d c d $ → ACTION[0,c]=s1 → push c and state1.
  2. state1, input d → s2 push d,state2.
  3. state2 has C->d· → r3: reduce by C->d. Pop d and state 2 (two entries), push C, then goto(1,C)=4.
  4. state4, input c → r2: reduce by C->cC. Pop C and state 4, then c and state 1 (four entries), push C, then goto(0,C)=3.
  5. state3, input c → s1; then state1, input d → s2; state2 → r3 (pop d,2; push C; goto(1,C)=4); state4, input $ → r2 (pop C,4 and c,1; push C; goto(3,C)=5); state5, input $ → r1 (pop C,5 and C,3; push S; goto(0,S)=6); state6, input $ → accept. Final result: input accepted. (In exam show full stack at each step; final acceptance at state6 on $.)

Example 2 — concept: LR(1) vs LALR merging effect (model-answer style)

  • Build canonical LR(1) item-sets for a small grammar (in exam you would list items with lookaheads). CLR (LR(1)) keeps distinct lookaheads, producing more states; LALR merges item-sets that have identical LR(0) cores (same productions and dot positions but possibly different lookaheads). The merging reduces number of states but may create new conflicts: if two LR(1) states had different lookaheads that prevented a conflict in CLR, their merge in LALR might combine lookaheads and produce shift/reduce or reduce/reduce conflicts.
  • In exam: show one small grammar, list two LR(1) states that differ only in lookahead, show that merging them produces conflicting lookahead set, and conclude LALR has a conflict while CLR does not. (Give the concrete merged ACTION cell containing both shift and reduce.)

Notes for exam answers: give complete item sets for CLR and then show the merged LALR states and explain the conflict.

Common mistakes

  • Wrong computation of FIRST/FOLLOW — not handling nullable productions correctly (forget FIRST(βa) when computing LR(1) closure).
  • Forgetting to augment the grammar with S' -> S; missing the accept condition.
  • When tracing parse, forgetting to pop the correct number of symbols/states for a reduction (reduce by A->α pops 2|α| stack entries if stack stores alternating symbols and states).
  • Merging LR(1) states incorrectly for LALR: you must merge only states with identical LR(0) cores; then union their lookaheads for each item.

For your exam

Question types you will see:

  • Definitions: "Define LR(0) item, closure, goto." Include precise algorithmic pseudocode.
  • Short notes: differences among SLR, LALR and CLR; power relation and typical use in parser generators.
  • Derivations: construct canonical sets of items, draw DFA of item-sets, produce ACTION/GOTO table.
  • Trace problems: show step-by-step parsing of a given input using your table (stack contents, action, remaining input at each step).
  • Longer questions: demonstrate an example where SLR fails but LALR or CLR succeeds; or show LALR merge causing conflict while CLR is fine. What a full-marks answer includes:
  • Augmented grammar, exact item-sets (each item written out), labelled DFA, complete ACTION/GOTO table, clear trace or conflict explanation, and justification of lookahead sets (FIRST/FOLLOW or LR(1) lookahead computation). Always state accept condition.

Quick check

  1. What is an LR(1) item? 2) In SLR how do you decide which terminals trigger a reduce for A->α· ? 3) Why does LALR have fewer states than CLR? Answers: An LR(1) item is a production with a dot and a one-symbol lookahead (A->α·β, a).; Use FOLLOW(A) — reduces by A->α are placed in ACTION[i,a] for every a in FOLLOW(A).; Because LALR merges LR(1) item-sets that have the same LR(0) core (identical productions and dot positions), combining their lookaheads.

Finished this topic? Mark it so your progress, study plan and readiness keep up.

Stuck on something here?