
(in simple language with definitions, construction steps, diagrams, and examples)
(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 view and download past papers
Click on Show Past Papers to load available exam papers.