Canonical Collection of LR(0) Items for an Expression Grammar

Canonical Collection of LR(0) Items for an Expression Grammar

Verified Sources
Nov 4, 2026

We want the canonical collection of LR(0) items for the grammar:

E→E+T∣TT→T∗F∣FF→(E)∣id\begin{aligned} E &\rightarrow E + T \mid T \\ T &\rightarrow T * F \mid F \\ F &\rightarrow (E) \mid id \end{aligned}

We use the standard LR(0) item form:

[A→α ⋅ β][A \rightarrow \alpha \,\boldsymbol{\cdot}\, \beta]

where the dot position indicates how much of α\alpha has been recognized. The canonical collection is built from an initial item set I0I_0 using closure and goto transitions:

  • closure adds items implied by dots before nonterminals.
  • goto moves the dot over a grammar symbol and then applies closure.

Key components:

  • LR(0) item
  • closure
  • goto
  • canonical collection

Note: To construct the LR(0) collection, we augment the grammar with a new start production:

S′→ES' \rightarrow E

This ensures a unique start state and acceptance configuration.

LR(0) Items, Closure, and Goto (Tutorial)

Step-by-step construction of the canonical LR(0) collection

  1. 1
    Step 1

    Use the augmented start production S′→ES'\rightarrow E, and the grammar rules for E,T,FE,T,F as given.

  2. 2
    Step 2

    Start with the single item [S′→⋅E][S'\rightarrow \cdot E]. Because the dot is before nonterminal EE, add all productions of EE with dot at the beginning. This includes items for E→E+TE\rightarrow E+T and E→TE\rightarrow T.

  3. 3
    Step 3

    For each symbol XX that appears immediately after a dot in any item of I0I_0, shift the dot over XX in those items to form a new set, then apply closure.

  4. 4
    Step 4

    From each newly created item set IkI_k, compute goto(Ik,X)goto(I_k, X) for all possible XX after dots; each distinct result becomes another state.

Step 1: Production list (for reference)

Let us label productions implicitly by their form:

  1. S′→ES' \rightarrow E
  2. E→E+TE \rightarrow E + T
  3. E→TE \rightarrow T
  4. T→T∗FT \rightarrow T * F
  5. T→FT \rightarrow F
  6. F→(E)F \rightarrow (E)
  7. F→idF \rightarrow id

The LR(0) item sets will contain items of the form [lhs→α⋅β][\text{lhs} \rightarrow \alpha \cdot \beta].

Step 2: Initial state I0I_0

I0=closure{[S′→⋅E]}I_0 = closure\{[S' \rightarrow \cdot E]\}

Items in I0I_0:

  • From [S′→⋅E][S' \rightarrow \cdot E], dot is before EE, so include:
    • [E→⋅E+T][E \rightarrow \cdot E + T]
    • [E→⋅T][E \rightarrow \cdot T]

From [E→⋅T][E \rightarrow \cdot T], dot is before TT, so include:

  • [T→⋅T∗F][T \rightarrow \cdot T * F]
  • [T→⋅F][T \rightarrow \cdot F]

From [T→⋅F][T \rightarrow \cdot F], dot is before FF, so include:

  • [F→⋅(E)][F \rightarrow \cdot (E)]
  • [F→⋅id][F \rightarrow \cdot id]

Therefore:

I0={[S′→⋅E],[E→⋅E+T],[E→⋅T],[T→⋅T∗F],[T→⋅F],[F→⋅(E)],[F→⋅id]}\begin{aligned} I_0=\{& [S' \rightarrow \cdot E], \\ &[E \rightarrow \cdot E + T], \\ &[E \rightarrow \cdot T], \\ &[T \rightarrow \cdot T * F], \\ &[T \rightarrow \cdot F], \\ &[F \rightarrow \cdot (E)], \\ &[F \rightarrow \cdot id] \} \end{aligned}

Step 3: Compute transitions and all states

We now compute goto(I0,X)goto(I_0, X) for each symbol XX that can follow a dot in I0I_0:

  • EE, TT, FF, (,id(, id

State I1=goto(I0,E)I_1 = goto(I_0, E)

Shift dot over EE from items where the dot is before EE:

  • [S′→⋅E]⇒[S′→E⋅][S' \rightarrow \cdot E] \Rightarrow [S' \rightarrow E \cdot]
  • [E→⋅E+T]⇒[E→E⋅+T][E \rightarrow \cdot E + T] \Rightarrow [E \rightarrow E \cdot + T]

No closure additions (dot is before terminals or end). So:

I1={[S′→E⋅],  [E→E⋅+T]}I_1=\{ [S' \rightarrow E\cdot],\; [E \rightarrow E\cdot + T] \}

State I2=goto(I0,T)I_2 = goto(I_0, T)

Shift dot over TT where possible:

  • [E→⋅T]⇒[E→T⋅][E \rightarrow \cdot T] \Rightarrow [E \rightarrow T \cdot]
  • [T→⋅T∗F]⇒[T→T⋅∗F][T \rightarrow \cdot T * F] \Rightarrow [T \rightarrow T\cdot * F]

So:

I2={[E→T⋅],  [T→T⋅∗F]}I_2=\{ [E \rightarrow T\cdot],\; [T \rightarrow T\cdot * F] \}

State I3=goto(I0,F)I_3 = goto(I_0, F)

Shift dot over FF:

  • [T→⋅F]⇒[T→F⋅][T \rightarrow \cdot F] \Rightarrow [T \rightarrow F\cdot]

So:

I3={[T→F⋅]}I_3=\{ [T \rightarrow F\cdot] \}

State I4=goto(I0,id)I_4 = goto(I_0, id)

Shift dot over idid:

  • [F→⋅id]⇒[F→id⋅][F \rightarrow \cdot id] \Rightarrow [F \rightarrow id\cdot]

So:

I4={[F→id⋅]}I_4=\{ [F \rightarrow id\cdot] \}

State I5=goto(I0,()I_5 = goto(I_0, ()

Shift dot over $(:

  • [F→⋅(E)]⇒[F→(⋅E)][F \rightarrow \cdot (E)] \Rightarrow [F \rightarrow ( \cdot E )]

Now closure applies because dot is before nonterminal EE:

  • add [E→⋅E+T][E \rightarrow \cdot E + T]
  • add [E→⋅T][E \rightarrow \cdot T] And then recursively for TT and FF:
  • [T→⋅T∗F][T \rightarrow \cdot T * F]
  • [T→⋅F][T \rightarrow \cdot F]
  • [F→⋅(E)][F \rightarrow \cdot (E)]
  • [F→⋅id][F \rightarrow \cdot id]

Thus:

I5={[F→(⋅E)],[E→⋅E+T],[E→⋅T],[T→⋅T∗F],[T→⋅F],[F→⋅(E)],[F→⋅id]}\begin{aligned} I_5=\{& [F \rightarrow ( \cdot E )], \\ &[E \rightarrow \cdot E + T], \\ &[E \rightarrow \cdot T], \\ &[T \rightarrow \cdot T * F], \\ &[T \rightarrow \cdot F], \\ &[F \rightarrow \cdot (E)], \\ &[F \rightarrow \cdot id] \} \end{aligned}

Step 4: Continue from newly created states until closure

From I1={[S′→E⋅],[E→E⋅+T]}I_1=\{[S'\rightarrow E\cdot],[E\rightarrow E\cdot + T]\}

Only possible dot-after symbol is ++ (from [E→E⋅+T][E\rightarrow E\cdot + T]).

State I6=goto(I1,+)I_6 = goto(I_1, +) Shift dot over ++:

  • [E→E⋅+T]⇒[E→E+⋅T][E \rightarrow E \cdot + T] \Rightarrow [E \rightarrow E + \cdot T]

Dot is before TT, so closure adds:

  • [T→⋅T∗F][T \rightarrow \cdot T * F]
  • [T→⋅F][T \rightarrow \cdot F] And closure for FF:
  • [F→⋅(E)][F \rightarrow \cdot (E)]
  • [F→⋅id][F \rightarrow \cdot id]

So:

I6={[E→E+⋅T],[T→⋅T∗F],[T→⋅F],[F→⋅(E)],[F→⋅id]}\begin{aligned} I_6=\{& [E \rightarrow E + \cdot T], \\ &[T \rightarrow \cdot T * F], \\ &[T \rightarrow \cdot F], \\ &[F \rightarrow \cdot (E)], \\ &[F \rightarrow \cdot id] \} \end{aligned}

From I2={[E→T⋅],[T→T⋅∗F]}I_2=\{[E\rightarrow T\cdot],[T\rightarrow T\cdot * F]\}

Possible dot-after symbol is ∗*.

State I7=goto(I2,∗)I_7 = goto(I_2, *) Shift over ∗*:

  • [T→T⋅∗F]⇒[T→T∗⋅F][T \rightarrow T\cdot * F] \Rightarrow [T \rightarrow T * \cdot F]

Dot is before FF, so closure adds:

  • [F→⋅(E)][F \rightarrow \cdot (E)]
  • [F→⋅id][F \rightarrow \cdot id]

Thus:

I7={[T→T∗⋅F],  [F→⋅(E)],  [F→⋅id]}I_7=\{ [T \rightarrow T * \cdot F],\; [F \rightarrow \cdot (E)],\; [F \rightarrow \cdot id] \}

From I5={[F→(⋅E)],...}I_5=\{[F\rightarrow ( \cdot E )], ...\}

We compute transitions for I5I_5.

Symbols that can appear immediately after a dot in I5I_5:

  • EE, TT, FF, (,id(, id

State goto(I5,E)=I8goto(I_5, E) = I_8 Shift dot over EE:

  • [F→(⋅E)]⇒[F→(E⋅)][F \rightarrow ( \cdot E )] \Rightarrow [F \rightarrow (E \cdot )]
  • [E→⋅E+T]⇒[E→E⋅+T][E \rightarrow \cdot E + T] \Rightarrow [E \rightarrow E \cdot + T]

No closure additions (dot before terminals or end). So:

I8={[F→(E⋅)],  [E→E⋅+T]}I_8=\{ [F \rightarrow (E\cdot )],\; [E \rightarrow E\cdot + T] \}

State goto(I5,T)=I9goto(I_5, T) = I_9 Shift dot over TT:

  • [E→⋅T]⇒[E→T⋅][E \rightarrow \cdot T] \Rightarrow [E \rightarrow T\cdot]
  • [T→⋅T∗F]⇒[T→T⋅∗F][T \rightarrow \cdot T * F] \Rightarrow [T \rightarrow T\cdot * F]

So:

I9={[E→T⋅],  [T→T⋅∗F]}I_9=\{ [E \rightarrow T\cdot],\; [T \rightarrow T\cdot * F] \}

Notice I9I_9 has the same contents as I2I_2; hence it is not a new state (canonical collection reuses identical item sets). Therefore:

  • I9≡I2I_9 \equiv I_2

State goto(I5,F)=I3goto(I_5, F) = I_3 Shifting over FF yields [T→F⋅][T\rightarrow F\cdot] again, so it matches I3I_3.

State goto(I5,id)=I4goto(I_5, id) = I_4 Matches I4I_4.

State goto(I5,()=I5goto(I_5, () = I_5 Shifting over (( from [F→⋅(E)][F\rightarrow \cdot (E)] reproduces the same structure as I5I_5 (it’s the same closure pattern), so:

  • goto(I5,′(′)≡I5goto(I_5, '(') \equiv I_5

From I6={[E→E+⋅T],[T→⋅T∗F],[T→⋅F],...}I_6=\{[E\rightarrow E+\cdot T], [T\rightarrow \cdot T*F], [T\rightarrow \cdot F], ...\}

Symbols after dots: TT, FF, (,id(, id.

State goto(I6,T)goto(I_6, T) Shift dot over TT:

  • [E→E+⋅T]⇒[E→E+T⋅][E \rightarrow E+\cdot T] \Rightarrow [E \rightarrow E+T\cdot]
  • [T→⋅T∗F]⇒[T→T⋅∗F][T \rightarrow \cdot T * F] \Rightarrow [T \rightarrow T\cdot * F]

So:

I10={[E→E+T⋅],  [T→T⋅∗F]}I_{10}=\{ [E \rightarrow E+T\cdot],\; [T \rightarrow T\cdot * F] \}

But [T→T⋅∗F][T \rightarrow T\cdot * F] appears in I2I_2 and I10I_{10} also contains [E→T⋅][E\rightarrow T\cdot]? No: I10I_{10} contains [E→E+T⋅][E\rightarrow E+T\cdot], which differs. Hence I10I_{10} is new.

State goto(I6,F)goto(I_6, F) Shift over FF:

  • [T→⋅F]⇒[T→F⋅][T\rightarrow \cdot F] \Rightarrow [T \rightarrow F\cdot]

So goto(I6,F)=I3goto(I_6, F)=I_3.

State goto(I6,id)=I4goto(I_6, id)=I_4
State goto(I6,′(′)=I5goto(I_6, '(')=I_5

From I7={[T→T∗⋅F],[F→⋅(E)],[F→⋅id]}I_7=\{[T\rightarrow T*\cdot F], [F\rightarrow \cdot (E)], [F\rightarrow \cdot id]\}

Symbols after dots: FF, (,id(, id.

State goto(I7,F)goto(I_7, F) Shift dot over FF:

  • [T→T∗⋅F]⇒[T→T∗F⋅][T\rightarrow T* \cdot F] \Rightarrow [T\rightarrow T*F\cdot]

So:

I11={[T→T∗F⋅]}I_{11}=\{ [T \rightarrow T*F\cdot] \}

State goto(I7,′(′)=I5goto(I_7, '(')=I_5
State goto(I7,id)=I4goto(I_7, id)=I_4

Quick equality check for item sets

Two LR(0) states are the same iff their item sets are identical (same productions and same dot positions). After each goto, sort/compare the items to reuse states like I9≡I2I_9 \equiv I_2 and goto(I5,′(′)≡I5goto(I_5,'(')\equiv I_5.

Don’t forget grammar augmentation

If you omit S′→ES'\rightarrow E, you may miss the unique acceptance-related item [S′→E⋅][S'\rightarrow E\cdot] in the collection.

Final canonical collection C\mathcal{C} of LR(0) item sets

We list the distinct item sets constructed:

I0I_0

I0={[S′→⋅E],[E→⋅E+T],[E→⋅T],[T→⋅T∗F],[T→⋅F],[F→⋅(E)],[F→⋅id]}\begin{aligned} I_0=\{& [S' \rightarrow \cdot E],\\ [E \rightarrow \cdot E + T],\\ [E \rightarrow \cdot T],\\ [T \rightarrow \cdot T * F],\\ [T \rightarrow \cdot F],\\ [F \rightarrow \cdot (E)],\\ [F \rightarrow \cdot id] \} \end{aligned}

I1=goto(I0,E)I_1 = goto(I_0,E)

I1={[S′→E⋅],  [E→E⋅+T]}I_1=\{ [S' \rightarrow E\cdot],\; [E \rightarrow E\cdot + T] \}

I2=goto(I0,T)I_2 = goto(I_0,T)

I2={[E→T⋅],  [T→T⋅∗F]}I_2=\{ [E \rightarrow T\cdot],\; [T \rightarrow T\cdot * F] \}

I3=goto(I0,F)I_3 = goto(I_0,F)

I3={[T→F⋅]}I_3=\{ [T \rightarrow F\cdot] \}

I4=goto(I0,id)I_4 = goto(I_0,id)

I4={[F→id⋅]}I_4=\{ [F \rightarrow id\cdot] \}

I5=goto(I0,′(′)I_5 = goto(I_0,'(')

I5={[F→(⋅E)],[E→⋅E+T],[E→⋅T],[T→⋅T∗F],[T→⋅F],[F→⋅(E)],[F→⋅id]}\begin{aligned} I_5=\{& [F \rightarrow ( \cdot E )],\\ [E \rightarrow \cdot E + T],\\ [E \rightarrow \cdot T],\\ [T \rightarrow \cdot T * F],\\ [T \rightarrow \cdot F],\\ [F \rightarrow \cdot (E)],\\ [F \rightarrow \cdot id] \} \end{aligned}

I6=goto(I1,′+′)I_6 = goto(I_1,'+')

I6={[E→E+⋅T],[T→⋅T∗F],[T→⋅F],[F→⋅(E)],[F→⋅id]}\begin{aligned} I_6=\{& [E \rightarrow E + \cdot T],\\ [T \rightarrow \cdot T * F],\\ [T \rightarrow \cdot F],\\ [F \rightarrow \cdot (E)],\\ [F \rightarrow \cdot id] \} \end{aligned}

I7=goto(I2,′∗′)I_7 = goto(I_2,'*')

I7={[T→T∗⋅F],  [F→⋅(E)],  [F→⋅id]}I_7=\{ [T \rightarrow T * \cdot F],\; [F \rightarrow \cdot (E)],\; [F \rightarrow \cdot id] \}

I8=goto(I5,E)I_8 = goto(I_5,E)

I8={[F→(E⋅)],  [E→E⋅+T]}I_8=\{ [F \rightarrow (E\cdot )],\; [E \rightarrow E\cdot + T] \}

I10=goto(I6,T)I_{10} = goto(I_6,T)

I10={[E→E+T⋅],  [T→T⋅∗F]}I_{10}=\{ [E \rightarrow E + T\cdot],\; [T \rightarrow T\cdot * F] \}

I11=goto(I7,F)I_{11} = goto(I_7,F)

I11={[T→T∗F⋅]}I_{11}=\{ [T \rightarrow T*F\cdot] \}

So the canonical collection has 11 distinct states:

C={I0,I1,I2,I3,I4,I5,I6,I7,I8,I10,I11}\mathcal{C}=\{I_0,I_1,I_2,I_3,I_4,I_5,I_6,I_7,I_8,I_{10},I_{11}\}

LR(0) canonical collection build order (conceptual)

Create $I_0$

Start

closure of [S′→⋅E][S'\rightarrow \cdot E]"

Go from $I_0$

First layer

goto(I0,E),goto(I0,T),goto(I0,F),goto(I0,id),goto(I0,′(′)goto(I_0,E), goto(I_0,T), goto(I_0,F), goto(I_0,id), goto(I_0,'(')"

Go from $I_1$ and $I_2$

Second layer

Create I6I_6 via '+', and I7I_7 via '*'"

Go from $I_5$, $I_6$, $I_7$

Third layer

Create I8I_8, then I10I_{10} and I11I_{11}"

No new goto results

Done

All reachable distinct LR(0) item sets are included"

Common checks and edge cases

Knowledge Check

Question 1 of 4
Q1Single choice

In constructing the canonical collection of LR(0) items, the function closure adds items when: