Parsing and Shift-Reduce Parsers
Context: FIT2014_MOC Β· the recognition problem for a CFG β run derivation machinery backwards from a string Β· Assignment 2 material (lexical analysis, parsing, computability) Parent Framework: Derivations and Parse Trees
Quick Revision
- π― Objective: given a CFG and a string, decide membership and β if a member β recover a parse tree / derivation. A parser is a program that does this.
- π¦ Core Components: top-down β expand toward the string | bottom-up β reduce the string back to | shift-reduce β the LR parser drilled here.
- β‘ Key Constraint: not every CFG has an LR parser β , and an ambiguous grammar guarantees conflicts.
π How It Works
1. The parsing problem
- Two questions at once β (a) is a word of ? (b) if so, exhibit a parse tree or a derivation for it.
- Top-down β start at and apply productions forwards, trying to reach .
- Bottom-up β start at and apply productions in reverse, trying to reduce it to .
2. LR parsers
- The acronym is the spec β scans input Left to right Β· constructs a Rightmost derivation in reverse Β· bottom-up.
- Machine model β implemented by a Deterministic PDA (DPDA), not a general nondeterministic one.
- β‘ Key Constraint: β deterministic PDAs are strictly weaker, so β some CFGs admit no LR parser.
3. Shift-reduce parser (the LR type examined here)
- State = stack + buffer β stack holds terminals and nonterminals processed so far (initially empty); buffer holds the unread suffix (initially the whole input).
- Two moves only β Shift the next input letter onto the stack, or Reduce when a block of top-most stack symbols equals the right-hand side of a rule β replace it by that ruleβs left-hand side.
- Accept condition β stack contains only the start symbol and the buffer is empty.
- Reduce = rule in reverse β this is what makes the run a rightmost derivation read backwards.
4. Ambiguity is what breaks the parser
- Grammar Plus-Times-A β ambiguous: has two parse trees, one grouping , the other .
- Grammar Plus-Times-B β the layered rewrite ( above above ) forces to bind tighter, giving one tree.
- Design lesson β fix ambiguity in the grammar (stratify by precedence), not in the parser.
βοΈ Core Decision Matrix
| Conflict | Trigger on the stack | What is ambiguous | Yaccβs default resolution |
|---|---|---|---|
| Shift-reduce | top symbols match a ruleβs RHS and the next buffer letter could legally be shifted | when to reduce β changes the grouping | shift |
| Reduce-reduce | top symbols match the RHS of more than one rule | which rule to reverse | use the rule listed first |
When It Flips: the conflict is a symptom, not the disease β an unambiguous, stratified grammar (Plus-Times-B) produces neither conflict, so the defaults never fire.
π Exam Execution Trace & Applied Exercises
Manual Execution Trace
Grammar : (1) Β· (2) Β· (3) Β· Input
| Step | Stack | Buffer | Action |
|---|---|---|---|
| 0 | shift | ||
| 1 | shift | ||
| 2 | reduce (3) | ||
| 3 | reduce (2) | ||
| 4 | shift | ||
| 5 | reduce (3) | ||
| 6 | reduce (1) | ||
| 7 | ACCEPT |
Applied Exercise
Problem: parse with Plus-Times-A: (1) Β· (2) Β· (3) Β· (4) .
| Step | Stack | Buffer | Action |
|---|---|---|---|
| 0 | shift | ||
| 1 | reduce (4) | ||
| 2 | shift | ||
| 3 | shift | ||
| 4 | reduce (4) | ||
| 5 | β conflict β reduce by (2), or shift ? | ||
| 6 | (shift branch) shift | ||
| 7 | reduce (4) | ||
| 8 | reduce (3) | ||
| 9 | reduce (2) | ||
| 10 | reduce (1) | ||
| 11 | ACCEPT |
Final Extracted Output: the shift branch groups . Taking the reduce branch at step 5 also reaches ACCEPT, but groups β two accepting runs, two trees βΉ the grammar is ambiguous.
β οΈ Common Mistakes
- π‘ A reduce needs the whole RHS on top β the matching symbols must be the top-most contiguous block of the stack, in order; a partial match is not a legal reduce.
- π‘ βBoth branches acceptβ is not a bug in the trace β it is the evidence of ambiguity. Donβt discard one branch to make the answer tidy.
- π‘ Bottom-up βΉ rightmost derivation in reverse β reading the reduce steps backwards gives a rightmost derivation, not a leftmost one (contrast the leftmost/prefix behaviour driving the PDA).
- π‘ β βitβs context-freeβ does not entitle you to an LR parser.
π§ Active Recall
At with still buffered, why is either move legal β and what does each one mean?
Answer
- Short answer: the stack top matches the RHS of rule (2), so a reduce is legal; the bufferβs next letter can also be shifted. Reducing commits to ; shifting defers and yields .
- Why: Shift-reduce conflict β Plus-Times-A gives and the same nonterminal , so the grammar itself never fixes precedence. Yacc breaks the tie by shifting; the real fix is Plus-Times-Bβs stratified .
Why can't every context-free grammar be given a shift-reduce parser?
Answer
- Short answer: an LR parser is a deterministic PDA, and deterministic PDAs recognise only the deterministic context-free languages β a proper subset, .
- Why: No backtracking, no guessing β at each step the parser must choose shift-or-reduce (and which rule) from bounded lookahead alone. A general PDA may explore all branches nondeterministically; a DPDA cannot, so grammars needing that search β every ambiguous grammar among them β fall outside its reach.