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 #
ContextFreeGrammar.computeNullables: Fixpoint iteration to compute all nonterminals from whichgcan derive the empty string.ContextFreeGrammar.eliminateEmpty: Eliminates all rules which rewrite to the empty string.
Main theorems #
ContextFreeGrammar.computeNullables_iff: All and only nullable symbols are computed.ContextFreeGrammar.eliminateEmpty_correct: The transformed grammar's language coincides with the original up to omission of the empty word.
References #
- [John E. Hopcroft, Rajeev Motwani, and Jeffrey D. Ullman. 2006. Introduction to Automata Theory, Languages, and Computation (3rd Edition). Addison-Wesley Longman Publishing Co., Inc., USA.] [Hopcroft et al. 2006]
Properties of context-free transformations to the empty string.
A nonterminal is nullable if it can be transformed into the empty string
Equations
Instances For
A word is nullable if it can be transformed into the empty string
Equations
Instances For
Definition and properties of ContextFreeGrammar.NullableRelated u v which relates strings,
which are equal up to nullable symbols.
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)
- empty_left
{T : Type uT}
{g : ContextFreeGrammar T}
(u : List (Symbol T g.NT))
(hu : NullableWord u)
: NullableRelated [] u
The empty string is
NullableRelatedto anyw, s.t.,NullableWord w - cons_term
{T : Type uT}
{g : ContextFreeGrammar T}
{u v : List (Symbol T g.NT)}
(huv : NullableRelated u v)
(t : T)
: NullableRelated (Symbol.terminal t :: u) (Symbol.terminal t :: v)
A terminal symbol
tneeds to be matched exactly - cons_nterm_match
{T : Type uT}
{g : ContextFreeGrammar T}
{u v : List (Symbol T g.NT)}
(huv : NullableRelated u v)
(n : g.NT)
: NullableRelated (Symbol.nonterminal n :: u) (Symbol.nonterminal n :: v)
A nonterminal symbol
ncan be matched exactly - cons_nterm_nullable
{T : Type uT}
{g : ContextFreeGrammar T}
{u v : List (Symbol T g.NT)}
(huv : NullableRelated u v)
{n : g.NT}
(hn : NullableNonTerminal n)
: NullableRelated u (Symbol.nonterminal n :: v)
A nonterminal symbol
n, s.t.,NullableNonterminal non the right, need not be matched
Instances For
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.
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
- ContextFreeGrammar.ruleIsNullable p r = decide (∀ s ∈ r.output, ContextFreeGrammar.symbolIsNullable p s = true)
Instances For
Add the input of a rule as a nullable symbol to p if the rule is nullable
Equations
- ContextFreeGrammar.addIfNullable r p = if ContextFreeGrammar.ruleIsNullable p r = true then insert r.input p else p
Instances For
generators g is the set of nonterminals that appear in the left hand side of rules of g
Equations
Instances For
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
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
Definition and properties of ContextFreeGrammar.eliminateEmpty, the algorithm to remove all
rules with an empty right-hand side from a grammar.
Compute all possible combinations of leaving out nullable nonterminals from u
Equations
- One or more equations did not get rendered due to their size.
- ContextFreeGrammar.nullableCombinations p [] = [[]]
- ContextFreeGrammar.nullableCombinations p (Symbol.terminal t :: s) = List.map (fun (x : List (Symbol T N)) => Symbol.terminal t :: x) (ContextFreeGrammar.nullableCombinations p s)
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
- g.eliminateEmpty = { NT := g.NT, initial := g.initial, rules := ContextFreeGrammar.removeNullables g.computeNullables }