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

ConflictTrigger on the stackWhat is ambiguousYacc’s default resolution
Shift-reducetop symbols match a rule’s RHS and the next buffer letter could legally be shiftedwhen to reduce βž” changes the groupingshift
Reduce-reducetop symbols match the RHS of more than one rulewhich rule to reverseuse 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

StepStackBufferAction
0shift
1shift
2reduce (3)
3reduce (2)
4shift
5reduce (3)
6reduce (1)
7ACCEPT

Applied Exercise

Problem: parse with Plus-Times-A: (1) Β· (2) Β· (3) Β· (4) .

StepStackBufferAction
0shift
1reduce (4)
2shift
3shift
4reduce (4)
5⚠ conflict β€” reduce by (2), or shift ?
6(shift branch) shift
7reduce (4)
8reduce (3)
9reduce (2)
10reduce (1)
11ACCEPT

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