CD Notes
Deep exploration of shift-reduce parsing: stack operations, reduction strategies, action tables, conflict types and resolution, relationship to LR parsing, practical implementations with complete examples.
Core Concept
Shift-reduce parsing is a bottom-up parsing technique using a stack and an input buffer. The parser repeatedly shifts symbols onto the stack or reduces them according to grammar productions.
Two Operations
Parsing Algorithm
Basic Shift-Reduce Algorithm
Shift vs Reduce Decisions
When to Shift
Conditions
1. No complete phrase on stack matching production RHS
2. More input needed to form phrase
3. Lookahead suggests continuing
Example
Stack: [E]
Input: + T
Lookahead: +
Might match: E → E + T (need to shift +)
Decision: SHIFT
When to Reduce
Conditions
1. Stack top matches RHS of some production
2. Lookahead confirms reduction is appropriate
3. No ambiguity with shift
Example
Stack: [id]
Input: +
Lookahead: +
Match: F → id
Decision: REDUCE
Shift-Reduce Example
Expression Parsing: 2 + 3
| E | E + T | T |
| T | num |
| [$, 2] + 3 $ reduce T | 2 |
| [$, T] + 3 $ reduce E | T |
| [$, E, +, 3] $ reduce T | 3 |
| [$, E, +, T] $ reduce E | E + T |
Conflict Resolution
Shift-Reduce Conflicts
| Problem | Both shift and reduce valid on same lookahead |
| Stack | [E] |
| Input | + T |
| Lookahead | + |
| Shift | Continue parsing E + ... |
| Reduce | Reduce E immediately |
| 1. Operator precedence | * higher than + |
| 2. Associativity | All left-associative |
Reduce-Reduce Conflicts
| Problem | Multiple productions could reduce same symbols |
| Stack | [id] |
| Input | , |
| Lookahead | , |
| Reduce by: A | id |
| Reduce by: B | id |
Parsing Table Construction
Action Table
| - shift s | Push terminal, go to state s |
| - reduce p | Reduce by production p |
| - accept | Successful parse |
| - error | Syntax error |
Example Shift-Reduce Table
Practical Implementation
Pseudocode: Shift-Reduce Parser
Relationship to LR Parsing
Shift-Reduce = LR Parsing Basis
LR(0) vs LR(1) Shift-Reduce
LR(0)
Uses only stack contents for decisions
Limited conflict resolution
Fewer states
LR(1)
Uses lookahead token
Resolves most conflicts
More states
Both use same shift-reduce mechanism
Error Recovery
Simple Error Recovery
Advantages and Disadvantages
Advantages ✓
Disadvantages ✗
Quick Revision Notes
- Shift: Push lookahead onto stack, advance input
- Reduce: Pop RHS from stack, push LHS non-terminal
- Conflict resolution: Precedence/associativity for shift-reduce
- LR(0): Minimal information (no lookahead)
- LR(1): Full lookahead (powerful, larger tables)
- Automatic: Compiler-compiler generates tables
- Stack-based: Implicit call stack for recursion
Interview Q&A
Q1: What causes shift-reduce conflicts?
A: Ambiguity in the grammar when both actions (shift and reduce) are grammatically valid. Example: dangling-else problem. Resolved using operator precedence and associativity declarations.
Q2: How do you decide what production to reduce by?
A: The parser matches stack contents against grammar productions. When stack top matches a production's RHS and lookahead confirms it's safe to reduce (won't prevent valid parses), reduce by that production.
Q3: Why can't you completely eliminate shift-reduce conflicts?
A: Some shift-reduce conflicts represent genuinely ambiguous grammar or language design choices (like operator precedence). They must be resolved by declaring precedence/associativity to disambiguate.
Q4: How does lookahead help resolve conflicts?
A: Lookahead tells you what comes next. If lookahead suggests the current phrase is complete, reduce. If it suggests continuing, shift. This helps distinguish similar parse states.
Q5: What's the difference between shift-reduce and reduce-reduce conflicts?
A: Shift-reduce: shift vs reduce (often resolvable with precedence). Reduce-reduce: multiple reductions possible (usually indicates grammar ambiguity, must fix grammar).
Q6: Why are LR parsers preferred over shift-reduce by hand?
A: Hand-coded shift-reduce requires manually managing stack and making complex decisions. LR parser generators automate this: compute tables, generate code, handle conflicts systematically. Less error-prone and more maintainable.
Exam Focus
Revise definitions, diagrams, examples, and short-answer points for Shift-Reduce Parsing: Fundamental Mechanism of Bottom-Up Syntax Analysis.
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, shift, reduce
Related Compiler Design Topics