LR Parsing — LR(0), SLR(1), LR(1), LALR(1)
799 words
4 min read
Visual companion
Python
Type and operator map
Python Week 1: the first filter for runtime behavior
View
Revision summary
What this note is really saying
Short form
# LR Parsing — LR(0), SLR(1), LR(1), LALR(1) ## 🎯 Learning Objectives - Construct LR(0) items and DFA - Build SLR(1) parsing tables - Differentiate LR(0), SLR(1), LR(1), and LALR(1) - Trace shift-reduce parsing on input * * * ## 1. Shift-Reduce Parsing ### 1.1 Intuition LR parsers are **bottom-up**: they start from...

LR Parsing — LR(0), SLR(1), LR(1), LALR(1)
🎯 Learning Objectives
- Construct LR(0) items and DFA
- Build SLR(1) parsing tables
- Differentiate LR(0), SLR(1), LR(1), and LALR(1)
- Trace shift-reduce parsing on input
1. Shift-Reduce Parsing
1.1 Intuition
LR parsers are bottom-up: they start from the input and try to reduce to the start symbol. The parser maintains a stack of grammar symbols and uses a parsing table to decide whether to shift (push next input) or reduce (pop handle, push non-terminal).
1.2 LR Parsing Algorithm
pythonstack = [$] input = w$ while True: action = TABLE[state(top)][input[i]] if action == shift s: push input[i]; push state s; i++ elif action == reduce A → β: pop 2|β| (symbols + states) push A; push TABLE[state(top)][A] elif action == accept: break else: error
2. LR(0) Items
An LR(0) item is a production with a dot indicating position:
Production: E→E+T
Items:
- E→⋅E+T (about to parse E)
- E→E⋅+T (parsed E, about to see +)
- E→E+⋅T (parsed E+, about to parse T)
- E→E+T⋅ (fully parsed — reduce ready)
2.1 LR(0) DFA Construction
(Diagram)
3. SLR(1) Parsing
SLR(1) = Simple LR with 1 lookahead symbol. Uses FOLLOW sets to decide reductions.
3.1 SLR Table Construction
For each item set Ii:
- Shift: If A→α⋅aβ∈Ii and GOTO(Ii,a) = Ij: set ACTION[i,a] = shift j
- Reduce: If A→α⋅∈Ii: set ACTION[i,a] = reduce A→α for all a∈ FOLLOW(A)
- Goto: If GOTO(Ii,A) = Ij: set GOTO[i,A] = j
3.2 SLR vs LR(0)
LR(0) reduces whenever the dot is at the end (even if the next symbol can't follow). SLR(1) only reduces if the next input is in FOLLOW of the LHS.
3.3 Limitations of SLR
Some grammars have shift-reduce conflicts even with SLR:
pseudoS → L = R S → R L → *R L → id R → L
FOLLOW(R) includes =, causing a conflict: should we reduce R → L or shift when seeing =?
4. LR(1) Items
LR(1) items include a lookahead — the set of terminals that can follow the completed production.
Format: [A→α⋅β,a]
Example: [E \to E + \cdot T, \]means:"abouttoparseT,andaftercompletingE+E,thenexttokenwillbe."
5. LALR(1)
LALR(1) = Look-Ahead LR(1). Merges LR(1) states with identical core (same production, different lookahead). Significantly fewer states than LR(1) with almost the same power.
| Parser | States | Power | Table Size |
|---|---|---|---|
| LR(0) | Small | Low | Small |
| SLR(1) | Small | Medium | Small |
| LALR(1) | Medium | High | Medium |
| LR(1) | Large | Highest | Large |
Bison generates LALR(1) parsers.
6. 📝 Practice Questions
Q1: What is the difference between a shift action and a reduce action?Answer: Shift pushes the next input symbol onto the stack and moves to a new state. Reduce pops the right-hand side of a production from the stack and pushes the left-hand side non-terminal, transitioning via GOTO. Q2: Why does LR(1) have more states than LALR(1)?Answer: LR(1) states include specific lookahead terminals. States with the same core production but different lookaheads are kept separate. LALR(1) merges these states, combining their lookahead sets. This reduces the number of states but can introduce conflicts (rarely). Q3: What causes a shift-reduce conflict? Give an example.Answer: A shift-reduce conflict occurs when the parser can't decide whether to shift the next token or reduce a production. Example: the dangling-else problem with grammarS → if E then S | if E then S else S. When seeingelse, should the parser reduce the inner if or shift the else? Q4: How does FOLLOW information help SLR(1) reduce conflicts relative to LR(0)?Answer: LR(0) reduces whenever the dot is at the end of an item. SLR(1) only reduces if the next input symbol is in FOLLOW of the left-hand side. This eliminates reductions that can never lead to a valid parse. Q5: Trace LR parsing of "id + id * id" using the expression grammar.Answer: The trace shows a sequence of shift and reduce actions: shift id (F→id), reduce T→F, reduce E→T, shift +, shift id (F→id), reduce T→F, shift _, shift id (F→id), reduce T→T_F, reduce E→E+T, accept.
7. 🔗 Cross-References
- Week 3 - LL(1) Parsing: Compare with LR parsing
- Week 5 - Semantic Analysis: Parser feeds semantic analyzer
- BSCS3021 (ToC): CFG, LR languages Join Discord PreviousSyntax Analysis INextSemantic Analysis