Length Restriction #
This file contains the algorithm to translate a ContextFreeGrammar.Wellformed grammar to a
chomsky normal form grammar while preserving the language of the original grammar. A context-free
grammar is wellformed if it has rules that rewrite either to a single terminal or multiple
nonterminals.
Main definitions #
ContextFreeGrammar.restrictLength: Transforms a context-free grammar to a chomsky normal form grammar by replacing rules that rewrite to multiple nonterminals to a set of cascading rules.
Main theorems #
ContextFreeGrammar.restrictLength_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]
Wellformed r holds if the rule's output is not a single nonterminal (UnitRule), not empty,
or if the output is more than one symbol, it is only nonterminals
- terminal
{T : Type uT}
{N : Type u_1}
{n : N}
(t : T)
: { input := n, output := [Symbol.terminal t] }.Wellformed
Rule rewriting to a single terminal is wellformed
- nonterminals
{T : Type uT}
{N : Type u_1}
{n : N}
(u : List (Symbol T N))
(h2 : 2 ≤ u.length)
(hu :
∀ s ∈ u,
match s with
| Symbol.nonterminal n => True
| x => False)
: { input := n, output := u }.Wellformed
Rule rewriting to mulitple nonterminals is wellformed
Instances For
Definition of ContextFreeGrammar.restrictLength, the algorithm to translate a wellformed
context-free grammar to a chomsky normal form grammar
Shorthand for the new type of nonterminals.
Equations
- ContextFreeGrammar.NT' = (g.NT ⊕ (r : ContextFreeRule T g.NT) × Fin (r.output.length - 2))
Instances For
Computes a cascade of rules generating r.output if it only contains nonterminals. For a rule
r : n -> n₁n₂n₃n₄, generates rules n -> n₁m₂, m₂ -> n₂m₃, and m₃ -> n₃n₄. The type of of NT',
encodes the correspondence between rules and the new nonterminals.
Equations
- One or more equations did not get rendered due to their size.
Instances For
We assume all rules' output is either a pair of nonterminals, a single terminal or a string of
at least 3 nonterminals. In the first two cases we can directly translate them, otherwise we
generate new rules using compute_rules_rec.
Equations
- One or more equations did not get rendered due to their size.
Instances For
Compute all ChomskyNormalFormRules corresponding to the original ContextFreeRules
Equations
Instances For
Construct a ChomskyNormalGrammar corresponding to the original ContextFreeGrammar
Equations
- g.restrictLength = { NT := ContextFreeGrammar.NT', initial := Sum.inl g.initial, rules := ContextFreeGrammar.restrictLengthRules g.rules.toList }
Instances For
A grammar is Wellformed if all rules are ContextFreeRule.Wellformed
Equations
- g.Wellformed = ∀ r ∈ g.rules, r.Wellformed
Instances For
Definitions of embeding into and projecting to the type of symbols of the new grammar
Intuitive embedding of symbols of the original grammar into symbols of the new grammar's type
Equations
Instances For
Intuitive embedding of strings of the original grammar into strings of the new grammar's type
Instances For
Projection from symbols of the new grammars type into symbols of the original grammar
Equations
Instances For
Projection from strings of the new grammars type into strings of the original grammar