Documentation

LeanPool.PumpingCfg.ChomskyNormalForm.EmptyElimination

Empty Elimination #

This file contains the algorithm to eliminate rules from a context-free grammar which rewrite to the empty string, while preserving the language of the grammar (except the empty string).

Main definitions #

Main theorems #

References #

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]

A nonterminal is nullable if it can be transformed into the empty string

Equations
Instances For
    @[reducible, inline]

    A word is nullable if it 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

      Definition and properties of ContextFreeGrammar.NullableRelated u v which relates strings, which are equal up to nullable symbols.

      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

        Definition an properties of ContextFreeGrammar.ComputeNullables computing the nullable nonterminals of a grammar g, i.e., those symbols which can be transformed to the empty string.

        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 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
                      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₂) :

                      Definition and properties of ContextFreeGrammar.eliminateEmpty, the algorithm to remove all rules with an empty right-hand side from a grammar.

                      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 empty string) of gs rules

                          Equations
                          Instances For

                            Given g, computes a new grammar in which all rules deriving [] are removed and all rules in g have a set of corresponding rules in g' in which some nullable symbols do not appear in the output. For example if r: V -> ABC is in g and A and B are nullable, the rules r₁ : V -> ABC, r₂ : V -> BC, r₃ : V -> AC, r₄ : V -> C will be in g.eliminate_empty

                            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