CD Notes
Comprehensive exploration of CLR parser construction: complete item sets, lookahead propagation algorithms, full DFA generation, parsing tables, comparison with SLR/LALR, practical compiler implementation.
Introduction
A CLR parser (Canonical LR) is the most powerful bottom-up parser with single lookahead. It constructs complete LR(1) item sets with full lookahead information for each item.
What Makes CLR "Canonical"?
| - Complete | Contains all information needed for parsing |
| - Minimal | No redundant items removed |
| - Result | Most powerful LR(1) parser variant |
Core Concepts
LR(1) Items Revisited
| [A | α • β, a] |
| 1. Production: A | α • β (core) |
| 2. Lookahead | a (specific to this item) |
| Meaning | Parser has seen α, expects β, and when β is complete, |
Why Item-Specific Lookahead Matters
| SLR uses | FOLLOW(A) globally |
| CLR uses | Item-specific lookahead (more precise) |
| Grammar: S | Aa | b A c |
| A | d |
| - After "b" | lookahead is c |
| - After first "A" | lookahead is a |
CLR Item Set Construction
CLOSURE Algorithm (for CLR)
| for each [A | α • Bβ, a] in closure(I) |
| for each production B | γ |
| add [B | • γ, b] to closure(I) |
Key Difference from SLR
SLR CLOSURE
for each [A → α • Bβ] in I
add [B → • γ] for all B → γ
CLR CLOSURE
for each [A → α • Bβ, a] in I
for each B → γ
for each b in FIRST(βa)
add [B → • γ, b]
Result: CLR items have more specific lookaheads
Building CLR Parsing Tables
Complete Algorithm
| 1. Build augmented grammar with S' | S |
| 4. For each reduce item [A | α •, a]: |
| ACTION[I, a] = "reduce A | α" |
| 5. For accept item [S' | S •, $]: |
Example: CLR Parser Construction
Grammar
| S' | E |
| E | E + T | T |
| T | id |
Item Set Construction
| [S' | • E, $] |
| [E | • E + T, {$, +}] |
| [E | • T, {$, +}] |
| [T | • id, {$, +}] |
| [S' | E •, $] |
| [E | E • + T, {$, +}] |
| [E | T •, {$, +}] |
| [T | id •, {$, +}] |
| [E | E + • T, {$, +}] |
| [T | • id, {$, +}] |
CLR Parsing Table
| St | id | + | $ | E | T |
|---|---|---|---|---|---|
| 0 | s3 | 1 | 2 | ||
| 1 | s4 | acc | |||
| 2 | r2 | r2 | |||
| 3 | r3 | r3 | |||
| 4 | s3 | 5 | |||
| 5 | r1 | r1 |
CLR Advantages
Power
Precision
CLR Limitations
Table Size
Computational Cost
CLR vs Alternatives
When to Use CLR
Use CLR when
- Grammar too complex for SLR/LALR
- Absolute minimum conflicts needed
- Language definition precision important
- Table size not critical (research)
Avoid CLR when
- LALR works (most cases)
- Table size critical
- Parse speed important
- Compiler generation speed matters
Quick Revision Notes
- CLR parser: Canonical LR(1), full lookahead per item
- LR(1) item: [A → α • β, a] with specific lookahead a
- CLOSURE: Add items for non-terminals after •, computing FIRST(βa) lookahead
- Parsing table: Fully specified, few/no conflicts
- Advantage: Most powerful single-lookahead parser
- Disadvantage: Large table size, expensive generation
- Practical: LALR usually sufficient; CLR for research
Interview Q&A
Q1: How does CLR lookahead differ from SLR's global FOLLOW sets?
A: SLR uses the same lookahead for all occurrences of a non-terminal (FOLLOW(A)). CLR computes item-specific lookahead, taking into account the context where that item appears. This precision eliminates many conflicts.
Q2: Why are CLR parsing tables so large?
A: CLR preserves item-specific lookahead, creating more distinct item sets. SLR merges items with the same core but different lookaheads. CLR keeps all distinctions, resulting in more states and larger tables.
Q3: Can CLR parsing tables have shift-reduce or reduce-reduce conflicts?
A: Theoretically yes, if the grammar is ambiguous. However, CLR's precision means many ambiguities are detected and reported. Well-designed unambiguous grammars parse without conflicts in CLR.
Q4: When would you choose LALR over CLR?
A: Almost always in practice. LALR provides most of CLR's power with much smaller tables (state merging). Table size and generation speed matter in real compilers. CLR is mainly used in research and compiler-compiler education.
Q5: How does lookahead propagation work in CLR?
A: When adding [B → • γ, a] to closure of [A → α • Bβ, b], the lookahead a is derived from FIRST(βb). This captures exactly which terminals can follow B in this parse context.
Q6: What grammar property determines if CLR has fewer conflicts than SLR?
A: The ability to distinguish different parse contexts for the same non-terminal. If a non-terminal appears in contexts with different lookahead requirements, CLR detects this distinction; SLR cannot.
Exam Focus
Revise definitions, diagrams, examples, and short-answer points for CLR Parser: Canonical LR(1) Parsing with Complete Lookahead Information.
Interview Use
Prepare one clear explanation, one practical example, and one common mistake for this Compiler Design topic.
Search Terms
compiler-design, compiler design, compiler, design, syntax, analysis, clr, parser
Related Compiler Design Topics