Unit Elimination #
This file contains the algorithm to eliminate rules from a context-free grammar which rewrite to a single nonterminal symbol, while preserving the language of the grammar.
Main definitions #
ContextFreeGrammar.computeUnitPairs: Fixpoint iteration to compute allUnitPairs, i.e., all pairs of two nonterminal symbols for which the second can be derived from the first by a chain of rule applications which rewrite to a single nonterminal.ContextFreeGrammar.eliminateUnitRules: Eliminates all rules rewriting to a single nonterminal.
Main theorems #
ContextFreeGrammar.computeUnitPairs_iff: All and onlyUnitPairs are computed.ContextFreeGrammar.eliminateUnitRules_correct: The transformed grammar's language coincides with the original.
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]
Convenient to talk about the rule rewriting a nonterminal nᵢ to another nonterminal nₒ
Equations
- ContextFreeGrammar.unitRule nᵢ nₒ = { input := nᵢ, output := [Symbol.nonterminal nₒ] }
Instances For
UnitPair n₁ n₂, (w.r.t. a ContextFreeGrammar g) means that g can derive n₂ from n₁
only using unitRules
- refl {T : Type uT} {g : ContextFreeGrammar T} [DecidableEq g.NT] {n : g.NT} (hng : n ∈ g.generators) : UnitPair n n
- trans {T : Type uT} {g : ContextFreeGrammar T} [DecidableEq g.NT] {n₁ n₂ n₃ : g.NT} (hng : unitRule n₁ n₂ ∈ g.rules) (hnn : UnitPair n₂ n₃) : UnitPair n₁ n₃
Instances For
Fixpoint iteration to compute all UnitPairs.
A unit pair is a pair (n₁, n₂) of nonterminals s.t. g can transform n₁ to n₂ only using
unitRules, i.e., a chain of productions rewriting nonterminals to nonterminals
generatorsProdDiag g is the diagonal of g.generators × g.generators
Equations
Instances For
Reflects transitivity of unit pairs. If (n₂, n₃) is a unit pair and g rewrites n₁ to n₂
then (n₁, n₃) are also a unit pair
Instances For
If r is a unit rule, add all unit pairs (r.input, n) to l for all unit pairs
(r.output, n) in l
Equations
- ContextFreeGrammar.collectUnitPairs r l = match r.output with | [Symbol.nonterminal v] => List.foldr (ContextFreeGrammar.addUnitPair r.input v) ∅ l | x => ∅
Instances For
Single step of fixpoint iteration, adding unit pairs to l for all rules r in g.rules
Equations
- One or more equations did not get rendered due to their size.
Instances For
Fixpoint iteration computing the unit pairs of g.
Equations
Instances For
Compute the least-fixpoint of add_unitPairs_iter, i.e., all (and only) unit pairs
Equations
Instances For
Definition and properties of ContextFreeGrammar.eliminateUnitRules, the algorithm to remove
all rules with a single nonterminal on the right-hand side.
For a given unit pair (n₁, n₂), computes rules r : n₁ → o, s.t. there is a rule
r' : n₂ → o in g (and o is non-unit)
Equations
- One or more equations did not get rendered due to their size.
Instances For
Computes non-unit rules for all unit pairs
Equations
Instances For
Given g, computes a new grammar g' in which all unit rules are removed and, for each
unit pair (n₁, n₂), we add rules r : n₁ → o if the rule r' : n₂ → o is in the grammar
(and non-unit)
Equations
- g.eliminateUnitRules = { NT := g.NT, initial := g.initial, rules := ContextFreeGrammar.removeUnitRules ContextFreeGrammar.computeUnitPairs }