Consider the following grammar: Find the LR(1) set of items.
We compute the canonical LR(1) sets (states) for the grammar:
We will build LR(1) items of the form
where is the lookahead terminal. The core mechanics are:
- Closure adds items for nonterminals immediately after the dot, with lookaheads propagated using FIRST.
- GOTO (or transition) moves the dot over a given grammar symbol and then applies closure.
Key terms: LR(1) item , closure , GOTO , FIRST.
Augmented grammar
Use the standard augmented start rule:
and end-of-input marker \ ,$ is the lookahead for the initial state.
We will denote the canonical LR(1) sets by .
LR(1) Items: Closure, GOTO, and Lookaheads
Construct LR(1) item sets for the grammar
- 1Step 1
We need FIRST sets for suffixes of the form FIRST(βa). Since C can derive terminal strings starting with c or d, FIRST(C) = {c, d}. Also FIRST(X) for a terminal X is {X}.
- 2Step 2
Start with [S' → · S, . S→CC means we need items for C that will appear in the right-hand side of S.
- 3Step 3
Whenever we have [A→α·Bβ, a], for each production B→γ we add [B→·γ, b] for every b in FIRST(βa) where FIRST(βa) includes terminals that can appear first in strings derived from β followed by a.
- 4Step 4
For each state I_k and each symbol X such that some item has dot immediately before X, compute goto(I_k, X) as closure of shifted items [A→αX·β, a].
- 5Step 5
Continue generating goto transitions until closure/goto yields the full finite canonical collection.
FIRST facts used for lookahead propagation
From :
Also for the augmented start:
- The only terminal that can appear immediately from a derivation of is the first terminal of , which is the first terminal of the first . So:
We will also use:
- For any terminal , FIRST() = {}.
- If a string β can begin with terminals in FIRST(β), those are the candidate lookaheads.
Compute canonical LR(1) sets
We list items explicitly. Recall items are .
State
Start item:
Closure on adds items for with lookahead \ $:
Now dot is before in . For items of the form [S \to \cdot C C,\; \]CC,$,C$C$, the lookaheads are:
Thus closure adds:
So:
Compute GOTO transitions from I0
- 1Step 1
Shift dot over S: [S'→S·, $]. This yields a state with only the completed start production item.
- 2Step 2
From [S→·CC, ]. Since dot is before another C, we add closure items for C with lookahead FIRST(C$)= {c,d}.
- 3Step 3
From items [C→·cC, c] and [C→·cC, d], shifting yields [C→c·C, c] and [C→c·C, d] and closure on dot-before-C introduces [C→·cC, lookahead] and [C→·d, lookahead] for those lookaheads.
- 4Step 4
From [C→·d, c] and [C→·d, d], shifting yields completed items [C→d·, c] and [C→d·, d].
State
State
Shift dot in [S \to \cdot CC,\; \]C$:
Dot is before the second , so closure adds items with lookahead in FIRST(C\)={c,d}$:
Thus:
State
From :
From :
Now dot is before ; closure adds items for with lookahead equal to the lookahead of each item (because the “β” after the dot is empty, so FIRST() = {}): So from lookahead add:
from lookahead add:
Hence:
State
Shift in:
Thus:
More states from I2, I3
We now compute transitions similarly.
From
has items:
- [S \to C\cdot C,\; \]C$)
- the same -items as for both lookaheads .
So transitions:
- : shift to [S\to CC\cdot,\; \]$ (completed)
- and : same as from the closure items for but with the same lookahead behavior as in /.
This yields new state:
Also:
(because the relevant -items are identical in structure and lookahead sets).
From
contains:
- and (dot before )
- plus the closure items , , and similarly for lookahead .
So:
- shifts dot in to completion for . This creates a state:
- and reproduce and respectively (by the same reasoning as earlier).
Thus no further distinct LR(1) sets arise.
Canonical LR(1) collection construction (state discovery order)
Initial closure
$I_0$Start with [S'→·S, $], then expand via S→CC and C→cC|d."
Accept state
$I_1$From I0 via goto(S): [S'→S·, $]."
After first C in S→CC
$I_2$From I0 via goto(C): [S→C·C, $] plus closure for C with lookahead {c,d}."
After reading a c in C productions
$I_3$From I0 via goto(c): items like [C→c·C, a] with a∈{c,d}."
Completed terminal d in C
$I_4$From I0 via goto(d): [C→d·, c] and [C→d·, d]."
Completed S→CC
$I_5$From I2 via goto(C): [S→CC·, $]."
Completed C→cC
$I_6$From I3 via goto(C): [C→cC·, c] and [C→cC·, d]."
Lookahead propagation shortcut
When you have [A→α·Bβ, a], the lookaheads for items of B are FIRST(βa). If β is empty, FIRST(βa) = {}.
Avoid confusing canonical LR(1) with LR(0)
In LR(1) you must keep the lookahead terminal in every item; otherwise states merge incorrectly and you won’t get the canonical collection.
Final answer: the LR(1) item sets
The canonical LR(1) sets for the given grammar are the six distinct collections below.
Knowledge Check
In LR(1) closure, for an item [A→α·Bβ, a], the lookaheads added for B→γ are computed from which set?