Canonical Collection of LR(0) Items for an Expression Grammar
We want the canonical collection of LR(0) items for the grammar:
We use the standard LR(0) item form:
where the dot position indicates how much of has been recognized. The canonical collection is built from an initial item set 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:
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
- 1Step 1
Use the augmented start production , and the grammar rules for as given.
- 2Step 2
Start with the single item . Because the dot is before nonterminal , add all productions of with dot at the beginning. This includes items for and .
- 3Step 3
For each symbol that appears immediately after a dot in any item of , shift the dot over in those items to form a new set, then apply closure.
- 4Step 4
From each newly created item set , compute for all possible after dots; each distinct result becomes another state.
Step 1: Production list (for reference)
Let us label productions implicitly by their form:
The LR(0) item sets will contain items of the form .
Step 2: Initial state
Items in :
- From , dot is before , so include:
From , dot is before , so include:
From , dot is before , so include:
Therefore:
Step 3: Compute transitions and all states
We now compute for each symbol that can follow a dot in :
- , , ,
State
Shift dot over from items where the dot is before :
No closure additions (dot is before terminals or end). So:
State
Shift dot over where possible:
So:
State
Shift dot over :
So:
State
Shift dot over :
So:
State
Shift dot over $(:
Now closure applies because dot is before nonterminal :
- add
- add And then recursively for and :
Thus:
Step 4: Continue from newly created states until closure
From
Only possible dot-after symbol is (from ).
State Shift dot over :
Dot is before , so closure adds:
- And closure for :
So:
From
Possible dot-after symbol is .
State Shift over :
Dot is before , so closure adds:
Thus:
From
We compute transitions for .
Symbols that can appear immediately after a dot in :
- , , ,
State Shift dot over :
No closure additions (dot before terminals or end). So:
State Shift dot over :
So:
Notice has the same contents as ; hence it is not a new state (canonical collection reuses identical item sets). Therefore:
State Shifting over yields again, so it matches .
State Matches .
State Shifting over from reproduces the same structure as (it’s the same closure pattern), so:
From
Symbols after dots: , , .
State Shift dot over :
So:
But appears in and also contains ? No: contains , which differs. Hence is new.
State Shift over :
So .
State
State
From
Symbols after dots: , .
State Shift dot over :
So:
State
State
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 and .
Don’t forget grammar augmentation
If you omit , you may miss the unique acceptance-related item in the collection.
Final canonical collection of LR(0) item sets
We list the distinct item sets constructed:
So the canonical collection has 11 distinct states:
LR(0) canonical collection build order (conceptual)
Create $I_0$
Startclosure of "
Go from $I_0$
First layer"
Go from $I_1$ and $I_2$
Second layerCreate via '+', and via '*'"
Go from $I_5$, $I_6$, $I_7$
Third layerCreate , then and "
No new goto results
DoneAll reachable distinct LR(0) item sets are included"
Common checks and edge cases
Knowledge Check
In constructing the canonical collection of LR(0) items, the function closure adds items when: