LR(k) Grammars #
This file defines the grammar-side deterministic context-free notion used in
parser theory, as a restriction of the repository's own context-free grammars
(CF_grammar). The key definition is CF_grammar.IsLRk: after a rightmost
derivation step, the reducible handle is uniquely determined by the already-built
sentential prefix and by k terminal lookahead symbols.
The definition uses the standard fresh-start augmentation. This matters even for language recognition: the completed augmented start rule is the parser's accept action, so including it in handle uniqueness rules out accept/reduce and accept/shift conflicts as well as ordinary reduce/reduce and shift/reduce conflicts.
This is the grammar-side class matching DPDAs. The equivalence is proved by
the two standard constructions in Langlib.Grammars.LR.Equivalence:
- an LR(k) parser as a DPDA, proving
is_LR → is_DCF; - a deterministic grammar construction from a DPDA, proving
is_DCF → is_LR.
A context-free rule is, in this development, a pair r : g.nt × List (symbol T g.nt)
whose first component r.1 is the left-hand nonterminal and whose second component
r.2 is the right-hand output string.
A rightmost use of a context-free rule r = (input, output).
The rule rewrites an occurrence of r.1 whose suffix contains terminals only.
Equations
- One or more equations did not get rendered due to their size.
Instances For
A single rightmost derivation step in a context-free grammar.
Equations
- g.ProducesRightmost u v = ∃ r ∈ g.rules, CF_grammar.RewritesRightmost r u v
Instances For
Rightmost derivation: reflexive-transitive closure of rightmost production.
Equations
Instances For
The k terminal symbols visible as LR lookahead.
Equations
- CF_grammar.lrLookahead k w = List.take k w
Instances For
Embed an original grammar symbol into a grammar with a fresh start
nonterminal. none is reserved for the fresh start and original nonterminals
are embedded with some.
Equations
Instances For
Embed a sentential form into the fresh-start augmentation.
Equations
Instances For
Fresh-start augmentation of a context-free grammar.
The new initial nonterminal is none; every original nonterminal is renamed to
some A; and the sole production headed by the fresh start is none → some S.
Equations
Instances For
Forget the fresh start symbol. The fresh start itself is sent to the original initial nonterminal, so the augmented start production projects to a reflexive step.
Equations
Instances For
Forget the fresh start symbol in a sentential form.
Equations
Instances For
An original rightmost rewrite embeds as a rightmost rewrite of the augmented grammar.
An original rightmost production step embeds into the augmented grammar.
An original rightmost derivation embeds into the augmented grammar, starting at the embedded original start symbol.
The distinguished start production is a rightmost step.
Lift an original rightmost derivation from its start symbol to a derivation from the fresh augmented start symbol.
An ordinary context-free rewrite embeds into the augmented grammar.
An ordinary derivation embeds into the augmented grammar.
The distinguished start production is an ordinary context-free step.
Lift an original derivation from its start symbol to a derivation from the fresh augmented start symbol.
Projecting one augmented context-free step gives zero or one original steps. The zero-step case is exactly the fresh start production.
Project an augmented derivation back to the original grammar.
Projecting one augmented rightmost step gives zero or one original rightmost steps. Again, the zero-step case is the fresh start production.
Project an augmented rightmost derivation back to the original grammar.
Fresh-start augmentation preserves the generated language.
Knuth's semantic handle-uniqueness condition, before fresh-start augmentation.
Suppose the first derivation has just used r₁ after the prefix p₁, so
its handle ends after p₁ ++ r₁.2. The equality in the premise says that
the entire second right-sentential form can also be split at that first handle
boundary, with a terminal suffix y. If the actual suffix s₁ of the first
handle and y have the same k visible terminals, an LR(k) grammar must choose
the same handle position and production.
Allowing the second handle to end later than the first is essential: that is the
case which detects a possible shift instead of the first reduction. Requiring
only p₁ ++ r₁.2 = p₂ ++ r₂.2 would detect reduce/reduce conflicts but
miss shift/reduce conflicts.
Equations
- One or more equations did not get rendered due to their size.
Instances For
Semantic LR(k) condition for a context-free grammar.
Handle uniqueness is checked after adjoining a genuinely fresh start symbol and
the production S′ → S. Thus the condition includes conflicts with the accept
action, not only conflicts between original productions.
Instances For
Core LR lookahead is monotone: a grammar satisfying the handle condition
with k symbols also satisfies it with any larger lookahead.
LR lookahead is monotone: a grammar that is LR(k) is also LR(l) for any l ≥ k.