Unit-Rule Elimination for CFGs #
This file computes unit pairs and removes unit productions without changing the language.
Main declarations #
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
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
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 }