
(in simple language with definitions, working, items, tables, examples, diagrams, and key exam points)
(in simple language with definitions, working, items, tables, examples, diagrams, and key exam points)
LR Parsing is a bottom-up parsing technique that reads input:
π So LR means:
βScan input left-to-right and produce rightmost derivation in reverse.β
LR parser builds the parse tree: π from bottom (input) to top (start symbol)
β Most powerful deterministic parsing method β Used in real compilers β Handles large class of grammars β No backtracking
| Type | Meaning |
|---|---|
| LR(0) | Simplest LR parser |
| SLR(1) | Simple LR with FOLLOW sets |
| CLR(1) | Canonical LR (most powerful) |
| LALR(1) | Look-Ahead LR (used in practice) |
An LR(0) parser is a bottom-up parser that uses:
An LR(0) item is a production with a dot (β’) showing parsing position.
E β E + T
E β T
T β id
E β E + T
Items:
E β β’ E + T
E β E β’ + T
E β E + β’ T
E β E + T β’
π Dot shows how much of production is parsed
Closure adds all possible productions for a non-terminal after dot.
If:
A β Ξ± β’ B Ξ²
Then add:
GOTO moves the dot over a symbol.
If:
E β E β’ + T
After GOTO(+):
E β E + β’ T
LR(0) parser uses a DFA:
States (Items) β Transitions β Parsing Table
Two parts:
| Part | Meaning |
|---|---|
| ACTION | shift/reduce/accept |
| GOTO | next state |
E β E + T | T
T β id
E' β E
States are built using:
| State | id | + | $ | E | T |
|---|---|---|---|---|---|
| 0 | S5 | 1 | 2 | ||
| 1 | S6 | ACC | |||
| 2 | R2 | R2 | |||
| 5 | R3 | R3 |
Legend:
Start with initial state (0)
Read input left to right
Use ACTION table:
Use GOTO for transitions
Accept when input is fully parsed
β Too weak β Cannot handle many grammars β Shift-reduce conflicts common β No lookahead (big issue)
| Feature | LR(0) | SLR(1) | CLR(1) |
|---|---|---|---|
| Lookahead | None | 1 | 1 |
| Power | Weak | Medium | Very Strong |
| Conflicts | Many | Fewer | Almost none |
| Use | Theory | Some use | Compiler design |
π Frequently asked:
| Aspect | LR Parsing | LR(0) Parser |
|---|---|---|
| Definition | Parsing technique | Simplest LR parser |
| Direction | Bottom-up | Bottom-up |
| Lookahead | Depends on type | None |
| Method | LR family | LR(0) items |
| Strength | Very powerful | Weak |
| Components | Stack, table | Items, DFA |
| Use | Compiler design | Theoretical basis |
Open this section to view and download past papers
Click on Show Past Papers to load available exam papers.