1. Gyanastra
  2. Compiler Design
  3. CLR(1) and LALR(1) Parsing
  4. LR(1) Items and Lookaheads
Free lesson, no account needed 5 min read 2 practice questions after it

Compiler Design · CLR(1) and LALR(1) Parsing

LR(1) Items and Lookaheads

Overview

LR(1) items extend LR(0) items by attaching lookahead symbols to dotted productions. The lookahead controls exactly where a reduce move is placed in the parsing table, making LR(1) (CLR(1)) parsing more precise than SLR(1), which uses FOLLOW of the left-hand side.

Key concepts

  • LR(0) item: any production of the grammar with a dot (•) placed somewhere on the right-hand side. For S -> AA, the LR(0) items are S -> •AA, S -> A•A, S -> AA•.
  • LR(1) item: an LR(0) item plus a lookahead symbol (or set of lookahead symbols). So: LR(1) item = LR(0) item + lookahead.
  • Final item: the item with the dot at the extreme right, i.e. a complete/reduce item, such as S -> AA•.
  • Lookahead purpose: the lookaheads determine where the reduce move of the final item is placed in the parsing table.
  • Canonical collection of LR(1) items: formed by starting from the augmented production and carrying lookaheads along with each item.

How it works

  1. SLR(1) and LR(0) parsing use the canonical collection of LR(0) items; to move to LR(1), attach lookaheads to those same LR(0) items.
  2. Take any production and place the dot on the right-hand side — that dotted production is an LR(0) item; add the lookahead symbols to it.
  3. When the final item is reached (dot at the extreme right), do not place the reduce move in the entire row of the parsing table, and do not place it in the FOLLOW of the left-hand side (as SLR does).
  4. Place the reduce move only in the columns of the lookahead symbols.
  5. Build the canonical collection of LR(1) items starting from the augmented production, carrying lookaheads with each item.

Formulas

LR(1) item = LR(0) item + lookahead
  • LR(0) item: production with a dot somewhere on the right-hand side.
  • lookahead: symbol or set of symbols attached to the item, controlling reduce placement.

Worked examples

Reduce placement with a lookahead set. If the final item is S -> AA• with lookahead set {C, D}, then in the parsing table the reduce move by S -> AA is entered only in the columns for C and D of that state — not in every terminal column, and not according to FOLLOW(S).

Start of the LR(1) construction. For the grammar S -> AA, A -> aA | b (the same grammar for which LR(0) and SLR(1) tables were already built):

  • Start with the augmented production S' -> •S as an LR(0) item.
  • Convert it to an LR(1) item by adding the lookahead $ (the lookahead for the augmented production is always $).
  • Because the dot is before S, all S-productions (S -> AA, …) are added with the appropriate lookaheads.
Figure. Start with [S' -> •S, $]. Closure adds S -> •AA with appropriate lookaheads. The final S -> AA• with lookahead set C D places reduce S -> AA only under C and D, not the entire row or FOLLOW S.

Comparison

FeatureLR(0) itemLR(1) item
ContentProduction with a dot on the RHSLR(0) item + lookahead symbol(s)
Reduce placementReduce move placed in the whole row (all terminals)Reduce move placed only in the lookahead columns
Basis for tableCanonical collection of LR(0) items (used in LR(0) and SLR(1))Canonical collection of LR(1) items (used in CLR(1))

Revision summary

  • LR(0) item = any production with a dot anywhere on the right-hand side.
  • LR(1) item = LR(0) item + lookahead.
  • The lookahead is attached to the final item (dot at the right end) and controls where the reduce move goes.
  • With lookahead {C, D} on S -> AA•, the reduce move is placed only under C and D — not in the entire row and not in FOLLOW(S).
  • SLR(1) uses FOLLOW of the left-hand side; LR(1) uses the item's own lookahead set, which is more precise.
  • Construction always begins from the augmented production, whose lookahead is always $.

Previous-year exam insights

  • Closure rule for LR(1) items: for item [A → α•Bβ, a], add [B → •γ, b] for every production B → γ and every b in FIRST(βa); if β is nullable, b includes a.
  • Goto rule: for a set I and symbol X, GOTO(I,X) = closure of {[A → αX•β, a] : [A → α•Xβ, a] in I};
    lookaheads are carried unchanged by the dot shift.
  • The initial item is [S' → •S, ];thestartsymbol′sproductionsgetlookaheadFIRST(]; the start symbol's productions get lookahead FIRST() = $, and lookaheads propagate through closure, so different items in the same state may have different lookahead sets.
  • Reduce placement: for [A → α•, a] in state I, put reduce by A → α in ACTION[I, a] for each lookahead a; a conflict (shift/reduce or reduce/reduce) means the grammar is not LR(1).
  • LALR(1) merges states of the CLR(1) canonical collection that have the same LR(0) core (same items ignoring lookaheads), taking the union of lookaheads; LALR(1) has the same number of states as SLR(1)/LR(0) and may introduce reduce/reduce conflicts that CLR(1) does not have, but never shift/reduce conflicts.
  • Language hierarchy: every SLR(1) grammar is LALR(1), every LALR(1) grammar is CLR(1)/LR(1), and every LR(1) grammar is unambiguous; the converses fail, so LALR(1) and CLR(1) accept exactly the deterministic context-free languages.
Pages 1–2 of 2