Langlib

Langlib.Classes.ContextFree.Pumping.EpsilonElimination

Epsilon Elimination for CFGs #

This file computes nullable nonterminals and removes empty productions while preserving the nonempty part of the language.

Main declarations #

theorem ContextFreeRule.Rewrites.empty {T : Type uT} {N : Type uN} {u : List (Symbol T N)} {r : ContextFreeRule T N} (hu : r.Rewrites u []) :

Properties of context-free transformations to the empty string

@[reducible, inline]

NullableNonTerminal n holds if n can be transformed into the empty string

Equations
Instances For
    @[reducible, inline]

    NullableWord u holds if u can be transformed into the empty string

    Equations
    Instances For
      theorem ContextFreeGrammar.DerivesIn.empty_of_append_left {T : Type uT} {g : ContextFreeGrammar T} {m : } {u v : List (Symbol T g.NT)} (huv : g.DerivesIn (u ++ v) [] m) :
      km, g.DerivesIn u [] k
      theorem ContextFreeGrammar.DerivesIn.empty_of_append_right_aux {T : Type uT} {g : ContextFreeGrammar T} {u v w : List (Symbol T g.NT)} {m : } (hwm : g.DerivesIn w [] m) (hw : w = u ++ v) :
      km, g.DerivesIn v [] k
      theorem ContextFreeGrammar.DerivesIn.empty_of_append_right {T : Type uT} {g : ContextFreeGrammar T} {m : } {u v : List (Symbol T g.NT)} (huv : g.DerivesIn (u ++ v) [] m) :
      km, g.DerivesIn v [] k
      theorem ContextFreeGrammar.DerivesIn.empty_of_append {T : Type uT} {g : ContextFreeGrammar T} {u v w : List (Symbol T g.NT)} {m : } (huvw : g.DerivesIn (u ++ v ++ w) [] m) :
      km, g.DerivesIn v [] k
      theorem ContextFreeGrammar.DerivesIn.mem_nullable {T : Type uT} {g : ContextFreeGrammar T} {u : List (Symbol T g.NT)} {s : Symbol T g.NT} {m : } (hu : g.DerivesIn u [] m) (hsu : s u) :
      km, g.DerivesIn [s] [] k
      theorem ContextFreeGrammar.Derives.append_left_trans {T : Type uT} {g : ContextFreeGrammar T} {u v w x : List (Symbol T g.NT)} (huv : g.Derives u v) (hwx : g.Derives w x) :
      g.Derives (w ++ u) (x ++ v)
      theorem ContextFreeGrammar.Derives.of_empty {T : Type uT} {g : ContextFreeGrammar T} {u : List (Symbol T g.NT)} (hu : g.Derives [] u) :
      u = []
      theorem ContextFreeGrammar.DerivesIn.nullable_mem_nonterminal {T : Type uT} {g : ContextFreeGrammar T} {u : List (Symbol T g.NT)} {s : Symbol T g.NT} {m : } (hu : g.DerivesIn u [] m) (hsu : s u) :
      ∃ (n : g.NT), s = Symbol.nonterminal n
      inductive ContextFreeGrammar.NullableRelated {T : Type uT} {g : ContextFreeGrammar T} :
      List (Symbol T g.NT)List (Symbol T g.NT)Prop

      NullableRelated u v means that v and u are equal up to interspersed nonterminals, each of which can be transformed to the empty string (i.e. for each additional nonterminal nt, NullableNonterminal nt holds)

      Instances For
        theorem ContextFreeGrammar.NullableRelated.append_split {T : Type uT} {g : ContextFreeGrammar T} {u v w : List (Symbol T g.NT)} (huvw : NullableRelated u (v ++ w)) :
        ∃ (v' : List (Symbol T g.NT)) (w' : List (Symbol T g.NT)), u = v' ++ w' NullableRelated v' v NullableRelated w' w
        theorem ContextFreeGrammar.NullableRelated.append_split_three {T : Type uT} {g : ContextFreeGrammar T} {u v w x : List (Symbol T g.NT)} (huvwx : NullableRelated u (v ++ w ++ x)) :
        ∃ (u₁ : List (Symbol T g.NT)) (u₂ : List (Symbol T g.NT)) (u₃ : List (Symbol T g.NT)), u = u₁ ++ u₂ ++ u₃ NullableRelated u₁ v NullableRelated u₂ w NullableRelated u₃ x
        def ContextFreeGrammar.symbolIsNullable {T : Type uT} {N : Type uN} [DecidableEq N] (p : Finset N) (s : Symbol T N) :

        Check if a symbol is nullable (w.r.t. to set of nullable symbols p), i.e., symbol_is_nullable p s only holds if s is a nonterminal and it is in p

        Equations
        Instances For

          A rule is nullable if all output symbols are nullable

          Equations
          Instances For
            def ContextFreeGrammar.addIfNullable {T : Type uT} {N : Type uN} [DecidableEq N] (r : ContextFreeRule T N) (p : Finset N) :

            Add the input of a rule as a nullable symbol to p if the rule is nullable

            Equations
            Instances For
              noncomputable def ContextFreeGrammar.generators {T : Type uT} (g : ContextFreeGrammar T) [DecidableEq g.NT] :

              generators g is the set of all nonterminals that appear in the left hand side of rules of g

              Equations
              Instances For
                noncomputable def ContextFreeGrammar.addNullables {T : Type uT} {g : ContextFreeGrammar T} [DecidableEq g.NT] (p : Finset g.NT) :

                Single round of fixpoint iteration; adds r.input to the set of nullable symbols if all symbols in r.output are nullable

                Equations
                • One or more equations did not get rendered due to their size.
                Instances For
                  @[irreducible]
                  noncomputable def ContextFreeGrammar.addNullablesIter {T : Type uT} {g : ContextFreeGrammar T} [DecidableEq g.NT] (p : Finset g.NT) (hpg : p g.generators) :

                  Fixpoint iteration computing the set of nullable symbols of g.

                  Equations
                  Instances For

                    Compute the least-fixpoint of add_nullable_iter, i.e., all (and only) nullable symbols

                    Equations
                    Instances For
                      @[irreducible]
                      theorem ContextFreeGrammar.addIfNullable_monotone {T : Type uT} {g : ContextFreeGrammar T} [DecidableEq g.NT] {r : ContextFreeRule T g.NT} {p₁ p₂ : Finset g.NT} (hpp : p₁ p₂) :
                      @[irreducible]
                      def ContextFreeGrammar.nullableCombinations {T : Type uT} {N : Type uN} [DecidableEq N] (p : Finset N) (u : List (Symbol T N)) :
                      List (List (Symbol T N))

                      Compute all possible combinations of leaving out nullable nonterminals from u

                      Equations
                      Instances For

                        Computes all variations of leaving out nullable symbols (except the empty string) of r

                        Equations
                        • One or more equations did not get rendered due to their size.
                        Instances For

                          Compute all variations of leaving out nullable symbols (except the empty string) of gs rules

                          Equations
                          Instances For
                            theorem ContextFreeGrammar.nullableRelated_mem_eliminateEmpty_rules {T : Type uT} {g : ContextFreeGrammar T} [DecidableEq g.NT] [DecidableEq T] {r : ContextFreeRule T g.NT} {u : List (Symbol T g.NT)} (hur : NullableRelated u r.output) (hrg : r g.rules) (hu : u []) :
                            { input := r.input, output := u } g.eliminateEmpty.rules
                            @[irreducible]
                            theorem ContextFreeGrammar.derivesIn_non_empty_to_nullableRelated_derives {T : Type uT} {g : ContextFreeGrammar T} [DecidableEq g.NT] [DecidableEq T] {u v : List (Symbol T g.NT)} (hv : v []) {m : } (huv : g.DerivesIn u v m) :
                            ∃ (u' : List (Symbol T g.NT)), NullableRelated u' u g.eliminateEmpty.Derives u' v