(in simple language with definitions, construction steps, diagrams, and examples)
An LR(0) automaton is a finite state machine (DFA) constructed from LR(0) items of a grammar. It is used to guide bottom-up (shift-reduce) parsing.
👉 It shows:
A production with a dot (•):
A → α • β
👉 Dot shows how much of the production is already seen.
If:
A → α • B β
Then include:
Moves dot over a symbol:
A → α • X β → A → α X • β
Add new start symbol:
S' → S
I0 = CLOSURE(S' → • S)
For each state and symbol:
GOTO(I, X)
Continue until no new states are generated.
S → CC
C → cC | d
S' → S
S → CC
C → cC | d
States:
I0 → I1 → I2 → ...
Transitions:
I0 --S--> I1
I0 --C--> I2
I0 --c--> I3
I0 --d--> I4
👉 Each state = set of LR(0) items
An LR(0) parsing table is a table that tells the parser:
| State | ACTION (terminals) | GOTO (non-terminals) | |||
|---|---|---|---|---|---|
| c | d | $ | S | C |
If:
A → α • a β
Then:
ACTION[i, a] = Shift j
If:
A → α •
Then:
ACTION[i, a] = Reduce A → α
(for all terminals)
If:
S' → S •
Then:
ACTION[i, $] = ACCEPT
If:
GOTO(I, A) = j
Then:
GOTO[i, A] = j
Grammar:
S → CC
C → cC | d
| State | c | d | $ | S | C |
|---|---|---|---|---|---|
| 0 | S3 | S4 | 1 | 2 | |
| 1 | ACC | ||||
| 2 | S3 | S4 | 5 | ||
| 3 | S3 | S4 | 6 | ||
| 4 | R C→d | R C→d | R | ||
| 5 | R S→CC |
Legend:
Push initial state (0)
Read input symbol
Check ACTION table:
Use GOTO for next state
Accept at final state
👉 That’s why LR(0) is weak
✔ Simple concept ✔ Foundation of LR parsing ✔ Easy automaton construction
❌ Too many conflicts ❌ Not suitable for real compilers ❌ No lookahead symbol
| Feature | LR(0) | SLR(1) | CLR(1) |
|---|---|---|---|
| Lookahead | None | FOLLOW set | Full lookahead |
| Conflicts | Many | Fewer | Almost none |
| Power | Weak | Medium | Very strong |
| Use | Theory | Some use | Real compilers |
👉 Frequently asked:
| Aspect | LR(0) Automaton | LR(0) Parsing Table |
|---|---|---|
| Definition | DFA of items | Parsing decision table |
| Components | States + transitions | ACTION + GOTO |
| Purpose | Show grammar states | Drive parsing |
| Construction | Closure + GOTO | From automaton |
| Output | State diagram | Table format |
| Use | Basis of LR parsing | Shift-reduce parsing |
Open this section to load past papers