
(Exam-ready, simple explanation with steps, items, diagrams, and key points)
(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 view and download past papers
Click on Show Past Papers to load available exam papers.