Langlib

Langlib.Grammars.LR.Definition

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:

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.

def CF_grammar.RewritesRightmost {T N : Type} (r : N × List (symbol T N)) (u v : List (symbol T N)) :

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
    def CF_grammar.ProducesRightmost {T : Type} (g : CF_grammar T) (u v : List (symbol T g.nt)) :

    A single rightmost derivation step in a context-free grammar.

    Equations
    Instances For
      @[reducible, inline]
      abbrev CF_grammar.DerivesRightmost {T : Type} (g : CF_grammar T) :
      List (symbol T g.nt)List (symbol T g.nt)Prop

      Rightmost derivation: reflexive-transitive closure of rightmost production.

      Equations
      Instances For
        def CF_grammar.lrLookahead {T : Type} (k : ) (w : List T) :

        The k terminal symbols visible as LR lookahead.

        Equations
        Instances For
          def CF_grammar.augmentSymbol {T N : Type} :
          symbol T Nsymbol T (Option N)

          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
            def CF_grammar.augmentString {T N : Type} (w : List (symbol T N)) :

            Embed a sentential form into the fresh-start augmentation.

            Equations
            Instances For
              @[simp]
              def CF_grammar.augmentRule {T N : Type} (r : N × List (symbol T N)) :

              Embed a production into the fresh-start augmentation.

              Equations
              Instances For

                The distinguished production of the augmented grammar.

                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
                    theorem CF_grammar.augmentRule_mem {T : Type} (g : CF_grammar T) {r : g.nt × List (symbol T g.nt)} (hr : r g.rules) :

                    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.

                        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.

                        def CF_grammar.CoreIsLRk {T : Type} (g : CF_grammar T) (k : ) :

                        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
                          def CF_grammar.IsLRk {T : Type} (g : CF_grammar T) (k : ) :

                          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.

                          Equations
                          Instances For
                            theorem CF_grammar.CoreIsLRk.mono {T : Type} (g : CF_grammar T) {k l : } (hkl : k l) (hg : g.CoreIsLRk k) :

                            Core LR lookahead is monotone: a grammar satisfying the handle condition with k symbols also satisfies it with any larger lookahead.

                            theorem CF_grammar.IsLRk.mono {T : Type} (g : CF_grammar T) {k l : } (hkl : k l) (hg : g.IsLRk k) :
                            g.IsLRk l

                            LR lookahead is monotone: a grammar that is LR(k) is also LR(l) for any l ≥ k.

                            def is_LRk {T : Type} (k : ) (L : Language T) :

                            Language class generated by an LR(k) grammar.

                            Equations
                            Instances For
                              def LRk {T : Type} (k : ) :

                              The class of languages generated by LR(k) grammars for a fixed amount of lookahead.

                              Equations
                              Instances For
                                def is_LR {T : Type} (L : Language T) :

                                Language class generated by an LR(k) grammar for some finite k.

                                Equations
                                Instances For
                                  def LR {T : Type} :

                                  The class of LR languages.

                                  Equations
                                  Instances For
                                    theorem is_LR_of_is_LRk {T : Type} {k : } {L : Language T} (h : is_LRk k L) :

                                    Every LR(k) language is an LR language.

                                    theorem is_LRk_mono {T : Type} {k l : } (hkl : k l) {L : Language T} (h : is_LRk k L) :
                                    is_LRk l L

                                    LR language classes are monotone in the amount of lookahead.