Consider the following grammar: SCCS \to CC CcCdC \to cC | d Find the LR(1) set of items.

Consider the following grammar: SCCS \to CC CcCdC \to cC | d Find the LR(1) set of items.

Verified Sources
Sep 12, 2026

We compute the canonical LR(1) sets (states) for the grammar:

SCC,CcCdS \to CC,\quad C \to cC \mid d

We will build LR(1) items of the form

[Aαβ,  a][A \to \alpha \,\boldsymbol{\cdot}\, \beta,\; a]

where aa 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:

SSS' \to S

and end-of-input marker \ ,$ is the lookahead for the initial state.

We will denote the canonical LR(1) sets by I0,I1,I_0, I_1, \dots.

LR(1) Items: Closure, GOTO, and Lookaheads

Construct LR(1) item sets for the grammar

  1. 1
    Step 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}.

  2. 2
    Step 2

    Start with [S' → · S, ].SincedotisbeforenonterminalS,additemsforSwithlookahead]. Since dot is before nonterminal S, add items for S with lookahead . S→CC means we need items for C that will appear in the right-hand side of S.

  3. 3
    Step 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.

  4. 4
    Step 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].

  5. 5
    Step 5

    Continue generating goto transitions until closure/goto yields the full finite canonical collection.

FIRST facts used for lookahead propagation

From CcCdC \to cC \mid d:

FIRST(C)={c,d}\text{FIRST}(C) = \{c, d\}

Also for the augmented start:

  • The only terminal that can appear immediately from a derivation of SS is the first terminal of CCCC, which is the first terminal of the first CC. So:
FIRST(S)={c,d}\text{FIRST}(S) = \{c, d\}

We will also use:

  • For any terminal aa, FIRST(aa) = {aa}.
  • 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 [Aαβ,  a][A \to \alpha \cdot \beta,\; a].

State I0I_0

Start item:

[SS,  $][S' \to \cdot S,\; \$]

Closure on SS adds items for SCCS \to CC with lookahead \ $:

[SCC,  $][S \to \cdot CC,\; \$]

Now dot is before CC in SCCS \to \cdot C C. For items of the form [S \to \cdot C C,\; \],weadditemsfor, we add items for CwithlookaheadinFIRST( with lookahead in FIRST(C ,$,)i.e.,FIRST(firstsymbolsoftheremainingsuffix) i.e., FIRST(first-symbols of the remaining suffix Cfollowedbyfollowed by$).Sincetheremainingsuffixbeginswiththenext). Since the remaining suffix begins with the next C$, the lookaheads are:

FIRST(C$)=FIRST(C)={c,d}\text{FIRST}(C\$) = \text{FIRST}(C) = \{c,d\}

Thus closure adds:

[CcC,  c],[Cd,  c],[CcC,  d],[Cd,  d][C \to \cdot cC,\; c],\quad [C \to \cdot d,\; c],\quad [C \to \cdot cC,\; d],\quad [C \to \cdot d,\; d]

So:

  • I0=I_0 =
{[SS,  $],  [SCC,  $],  [CcC,  c],  [Cd,  c],  [CcC,  d],  [Cd,  d]}\{ [S' \to \cdot S,\; \$],\; [S \to \cdot CC,\; \$],\; [C \to \cdot cC,\; c],\; [C \to \cdot d,\; c],\; [C \to \cdot cC,\; d],\; [C \to \cdot d,\; d] \}

Compute GOTO transitions from I0

  1. 1
    Step 1

    Shift dot over S: [S'→S·, $]. This yields a state with only the completed start production item.

  2. 2
    Step 2

    From [S→·CC, ],shiftingoverthefirstCgives[SCC,], shifting over the first C gives [S→C·C, ]. Since dot is before another C, we add closure items for C with lookahead FIRST(C$)= {c,d}.

  3. 3
    Step 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.

  4. 4
    Step 4

    From [C→·d, c] and [C→·d, d], shifting yields completed items [C→d·, c] and [C→d·, d].

State I1=GOTO(I0,S)I_1 = \text{GOTO}(I_0, S)

I1={[SS,  $]}I_1 = \{[S' \to S \cdot,\; \$]\}

State I2=GOTO(I0,C)I_2 = \text{GOTO}(I_0, C)

Shift dot in [S \to \cdot CC,\; \]overthefirstover the firstC$:

[SCC,  $][S \to C\cdot C,\; \$]

Dot is before the second CC, so closure adds CC items with lookahead in FIRST(C\)={c,d}$:

[CcC,  c],  [Cd,  c],  [CcC,  d],  [Cd,  d][C \to \cdot cC,\; c],\; [C \to \cdot d,\; c],\; [C \to \cdot cC,\; d],\; [C \to \cdot d,\; d]

Thus:

I2={[SCC,  $],  [CcC,  c],  [Cd,  c],  [CcC,  d],  [Cd,  d]}I_2 = \{ [S \to C\cdot C,\; \$],\; [C \to \cdot cC,\; c],\; [C \to \cdot d,\; c],\; [C \to \cdot cC,\; d],\; [C \to \cdot d,\; d] \}

State I3=GOTO(I0,c)I_3 = \text{GOTO}(I_0, c)

From [CcC,  c][C\to \cdot cC,\; c]:

[CcC,  c][C \to c\cdot C,\; c]

From [CcC,  d][C\to \cdot cC,\; d]:

[CcC,  d][C \to c\cdot C,\; d]

Now dot is before CC; closure adds items for CC with lookahead equal to the lookahead of each item (because the “β” after the dot is empty, so FIRST(ϵa\epsilon a) = {aa}): So from lookahead cc add:

[CcC,  c], [Cd,  c][C \to \cdot cC,\; c],\ [C \to \cdot d,\; c]

from lookahead dd add:

[CcC,  d], [Cd,  d][C \to \cdot cC,\; d],\ [C \to \cdot d,\; d]

Hence:

I3={[CcC,  c],  [CcC,  d],  [CcC,  c],  [Cd,  c],  [CcC,  d],  [Cd,  d]}I_3 = \{ [C \to c\cdot C,\; c],\; [C \to c\cdot C,\; d],\; [C \to \cdot cC,\; c],\; [C \to \cdot d,\; c],\; [C \to \cdot cC,\; d],\; [C \to \cdot d,\; d] \}

State I4=GOTO(I0,d)I_4 = \text{GOTO}(I_0, d)

Shift in:

  • [Cd,  c][Cd,  c][C\to \cdot d,\; c] \mapsto [C\to d\cdot,\; c]
  • [Cd,  d][Cd,  d][C\to \cdot d,\; d] \mapsto [C\to d\cdot,\; d]

Thus:

I4={[Cd,  c], [Cd,  d]}I_4 = \{[C \to d\cdot,\; c],\ [C \to d\cdot,\; d]\}

More states from I2, I3

We now compute transitions similarly.

From I2I_2

I2I_2 has items:

  • [S \to C\cdot C,\; \](dotbefore(dot beforeC$)
  • the same CC-items as I0I_0 for both lookaheads c,dc,d.

So transitions:

  • GOTO(I2,C)\text{GOTO}(I_2, C): shift to [S\to CC\cdot,\; \]$ (completed)
  • GOTO(I2,c)\text{GOTO}(I_2, c) and GOTO(I2,d)\text{GOTO}(I_2, d): same as from the closure items for CC but with the same lookahead behavior as in I0I_0/I3I_3.

This yields new state:

I5=GOTO(I2,C)={[SCC,  $]}I_5 = \text{GOTO}(I_2, C) = \{[S \to CC\cdot,\; \$]\}

Also:

GOTO(I2,c)=I3,GOTO(I2,d)=I4\text{GOTO}(I_2, c) = I_3,\quad \text{GOTO}(I_2, d)= I_4

(because the relevant CC-items are identical in structure and lookahead sets).

From I3I_3

I3I_3 contains:

  • [CcC,  c][C \to c\cdot C,\; c] and [CcC,  d][C \to c\cdot C,\; d] (dot before CC)
  • plus the closure items [CcC,  c][C\to \cdot cC,\; c], [Cd,  c][C\to \cdot d,\; c], and similarly for lookahead dd.

So:

  • GOTO(I3,C)\text{GOTO}(I_3, C) shifts dot in [CcC,  a][C\to c\cdot C,\; a] to completion [CcC,  a][C\to cC\cdot,\; a] for a{c,d}a\in\{c,d\}. This creates a state:
I6={[CcC,  c], [CcC,  d]}I_6 = \{[C \to cC\cdot,\; c],\ [C \to cC\cdot,\; d]\}
  • GOTO(I3,c)\text{GOTO}(I_3, c) and GOTO(I3,d)\text{GOTO}(I_3, d) reproduce I3I_3 and I4I_4 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) = {aa}.

Avoid confusing canonical LR(1) with LR(0)

In LR(1) you must keep the lookahead terminal aa 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.

I0I_0

{[SS,  $],  [SCC,  $],  [CcC,  c],  [Cd,  c],  [CcC,  d],  [Cd,  d]}\{ [S' \to \cdot S,\; \$],\; [S \to \cdot CC,\; \$],\; [C \to \cdot cC,\; c],\; [C \to \cdot d,\; c],\; [C \to \cdot cC,\; d],\; [C \to \cdot d,\; d] \}

I1I_1

{[SS,  $]}\{[S' \to S \cdot,\; \$]\}

I2I_2

{[SCC,  $],  [CcC,  c],  [Cd,  c],  [CcC,  d],  [Cd,  d]}\{ [S \to C\cdot C,\; \$],\; [C \to \cdot cC,\; c],\; [C \to \cdot d,\; c],\; [C \to \cdot cC,\; d],\; [C \to \cdot d,\; d] \}

I3I_3

{[CcC,  c],  [CcC,  d],  [CcC,  c],  [Cd,  c],  [CcC,  d],  [Cd,  d]}\{ [C \to c\cdot C,\; c],\; [C \to c\cdot C,\; d],\; [C \to \cdot cC,\; c],\; [C \to \cdot d,\; c],\; [C \to \cdot cC,\; d],\; [C \to \cdot d,\; d] \}

I4I_4

{[Cd,  c], [Cd,  d]}\{[C \to d\cdot,\; c],\ [C \to d\cdot,\; d]\}

I5I_5

{[SCC,  $]}\{[S \to CC\cdot,\; \$]\}

I6I_6

{[CcC,  c], [CcC,  d]}\{[C \to cC\cdot,\; c],\ [C \to cC\cdot,\; d]\}

Knowledge Check

Question 1 of 4
Q1Single choice

In LR(1) closure, for an item [A→α·Bβ, a], the lookaheads added for B→γ are computed from which set?