Compiler Design · LR(0) and SLR(1) Parsing
Introduction to LR Parsers and Their Four Types
Overview
LR parsers are a class of bottom-up parsers that use a stack, an input buffer and a parsing table to analyse input. They come in four types whose power increases from LR(0) to CLR(1), and all four share the same parsing algorithm.
Key concepts
- LR parser: a bottom-up parser belonging to the LR family; it is an algorithm requiring three components.
- Four types of LR parsers (in ascending order of power): LR(0), SLR(1), LALR(1), CLR(1).
- Power ordering: LR(0) is the least powerful; CLR(1) is the most powerful.
- Components of an LR parser:
- A stack (parse stack)
- An LR parsing table
- An input buffer
- Parser classification:
- Top-down parsers: LL(1) and recursive descent.
- Bottom-up parsers: operator precedence parser and the class of LR parsers.
How it works
- The LR parser is structured like the LL(1) parser: LL(1) uses the LL(1) parsing table, while LR parsing uses the LR parsing table.
- The input is supplied in the input buffer, and a stack is used.
- The parsing algorithm is the same for all four LR parser types; only the construction of the parsing table differs.
Drawing the diagram…
Comparison
| Parser type | Power (ascending) |
|---|---|
| LR(0) | Least powerful |
| SLR(1) | Higher than LR(0) |
| LALR(1) | Higher than SLR(1) |
| CLR(1) | Most powerful |
| Parser | Table used |
|---|---|
| LL(1) | LL(1) parsing table |
| LR(0), SLR(1), LALR(1), CLR(1) | LR parsing table (same algorithm for all four) |
Revision summary
- LR parsers are bottom-up parsers, not top-down.
- The four types in ascending power: LR(0) < SLR(1) < LALR(1) < CLR(1).
- Every LR parser needs exactly three components: input stack, LR parsing table, input buffer.
- The parsing algorithm is identical for all four LR parsers; only the parsing-table construction differs.
- Do not confuse the LL(1) parsing table (top-down) with the LR parsing table (bottom-up).
Previous-year exam insights
- LR parsers produce a rightmost derivation in reverse (they are bottom-up parsers). Asked inGATE 2019
- The canonical LR parser is CLR(1), i.e. LR(1) with a single lookahead terminal; every LR(k) grammar with k > 1 can be transformed into an equivalent LR(1) grammar.
Asked inUGC NET 2014 - Power/grammar-class inclusion: every LR(0) grammar is SLR(1), every SLR(1) grammar is LALR(1), and every LALR(1) grammar is CLR(1) (LR(1)); the reverse inclusions do not hold, so LR(0) ⊂ SLR(1) ⊂ LALR(1) ⊂ CLR(1).
- All LR-family parsers (LR(0), SLR(1), LALR(1), CLR(1)) are deterministic bottom-up parsers that can be implemented with the same shift-reduce parsing algorithm; they differ only in how the parsing table is constructed (lookahead and item-set merging).
- The LR parsing table has two parts, ACTION (shift, reduce, accept, error) and GOTO (for nonterminals), and the parser configuration is (stack, remaining input); a shift pushes the input symbol and advances, a reduce pops the handle's right-hand side and pushes the left-hand-side nonterminal using GOTO.
- LALR(1) has the same number of states as SLR(1)/LR(0) (it merges LR(1) states with identical cores), so it is as space-efficient as SLR(1) but more powerful; CLR(1) has the most states.