(Exam-ready, simple explanation with steps, items, diagrams, and key points)
An LR(1) parser is a bottom-up parsing technique that:
👉 Full form:
LR(1) = Left-to-right scan + Rightmost derivation (reverse) + 1 lookahead
LR(1) parser uses:
An LR(1) item is written as:
[A → α • β, a]
Where:
A → αβ is production• shows parsing positiona is lookahead symbol👉 “We expect to reduce A → αβ only if next input is a”
If:
[A → α • B β, a]
Then for every production:
B → γ
Add:
[B → • γ, b]
Where:
b ∈ FIRST(βa)👉 Lookahead makes LR(1) very precise
If:
[A → α • X β, a]
Then:
GOTO(I, X) = [A → α X • β, a]
An LR(1) automaton is a DFA of LR(1) item sets.
👉 It contains:
S' → S
I0 = CLOSURE([S' → • S, $])
I0 --S--> I1
I0 --C--> I2
I1 --+--> I3
S → CC
C → cC | d
S' → S
[S' → • S, $]
[S → • CC, $]
[C → • cC, c/d/$]
[C → • d, c/d/$]
| State | ACTION (terminals) | GOTO (non-terminals) | |||
|---|---|---|---|---|---|
| c | d | $ | S | C |
If:
[A → α • a β, b]
Then:
ACTION[i, a] = SHIFT j
If:
[A → α •, a]
Then:
ACTION[i, a] = REDUCE A → α
👉 Only for that specific lookahead a
If:
[S' → S •, $]
Then:
ACTION[i, $] = ACCEPT
GOTO[i, A] = j
✔ Uses exact lookahead symbol ✔ Avoids unnecessary reductions ✔ Removes most conflicts ✔ Handles complex grammars
✔ Very powerful parser ✔ No ambiguity in decisions ✔ No shift/reduce conflicts (in most cases) ✔ Used as theoretical base for LALR
❌ Large number of states ❌ Complex parsing table ❌ Memory expensive ❌ Hard to construct manually
| Feature | LR(0) | SLR(1) | LR(1) |
|---|---|---|---|
| Lookahead | None | FOLLOW | Specific symbol |
| Power | Low | Medium | Very High |
| States | Few | Medium | Many |
| Conflicts | Many | Some | Very few |
| Complexity | Low | Medium | High |
👉 Very frequently asked:
| Aspect | LR(1) Parser |
|---|---|
| Type | Bottom-up |
| Lookahead | 1 (exact symbol) |
| Basis | LR(1) items |
| Power | Very high |
| Table | ACTION + GOTO |
| Automaton | DFA of item sets |
| Conflicts | Minimal |
| Complexity | High |
Open this section to load past papers