LR Items and Shift–Reduce Parsing

Interpret a dotted production as progress through a rule and trace the parser’s choice to shift or reduce.

Source
Anbloq
Language
English
Created
Updated
Chapters
2
Reading time
1 min
In this article2 chapters
  1. The stack summarizes a viable prefix
  2. A reduction recognizes a suffix as a single nonterminal

The stack summarizes a viable prefix

An item \(A\to\alpha\mathbin{\cdot}\beta\) records that symbols of \(\alpha\) have been recognized while \(\beta\) is expected. A closure operation adds items beginning at the dot for any expected nonterminal. A transition on a recognized symbol advances the corresponding dots and takes closure again. Different histories reaching the same item set share the same local parser state.

A reduction recognizes a suffix as a single nonterminal

When a rule is complete and the current lookahead authorizes reduction, pop the production’s right-hand side and use the transition on its left-hand nonterminal from the underlying state. A shift instead moves forward with the next token. An ambiguous expression grammar can make both possible at the same point, such as whether to reduce an addition before a multiplication arrives. Grammar structure or declared precedence resolves the intended case. Grammar ambiguity explains conflicting trees. Predictive parsing uses an earlier decision point.[1]

References

  1. Andrew C. Myers, Cornell CS 4120: Compiler Course Notes ↩