Grammars in Chomsky normal form #
In Sipser's definition Sipser, Definition 2.8, a context-free grammar is in
Chomsky normal form when every rule has the form A → B C or A → a, where B and C are not
the start symbol S, except that the rule S → ε is permitted. Hopcroft, Motwani and Ullman's
normal form [HMU06] has no ε-rule and allows S on right-hand sides.
Cslib.ChomskyNormalFormGrammar covers both: its rules form a list, so that algorithms can
iterate over them, and a Boolean flag records the rule S → ε. Only when that flag is set must
S occur on no right-hand side; this is what keeps ε out of the languages of all the other
nonterminals. Sipser's form is the case in which S occurs on no right-hand side regardless.
ChomskyNormalFormGrammar.toCFG reads the grammar as a Mathlib ContextFreeGrammar, and every
statement about derivations and languages is made through it.
The design (rule constructors leaf and node, the field names NT, initial and rules, and
the embedding toCFG) follows Loitzl and Dvořák's Mathlib pull request
leanprover-community/mathlib4#19943, which was closed unmerged and had no ε-rule.
Main definitions #
Cslib.ChomskyNormalFormRule: a ruleA → a(leaf) orA → B C(node).Cslib.ChomskyNormalFormGrammar: a grammar in Chomsky normal form, with an optionalS → ε.Cslib.ChomskyNormalFormGrammar.toCFG: the grammar as a MathlibContextFreeGrammar.
Main results #
Cslib.ChomskyNormalFormGrammar.derives_nil_iff: only the start symbol derivesε, and only with the ruleS → ε.Cslib.ChomskyNormalFormGrammar.mem_language_nil_iff:εis in the language iff the grammar has the ruleS → ε.Cslib.ChomskyNormalFormGrammar.derives_iff_of_ne_nil: a nonterminalAderives a nonempty wordwiffw = awith a ruleA → a, orw = u vwithuandvnonempty and a ruleA → B Csuch thatBderivesuandCderivesv.
References #
A rule of a grammar in Chomsky normal form, over terminals T and nonterminals N.
- leaf {T : Type u_1} {N : Type u_2} (A : N) (a : T) : ChomskyNormalFormRule T N
The rule
A → a. - node {T : Type u_1} {N : Type u_2} (A B C : N) : ChomskyNormalFormRule T N
The rule
A → B C.
Instances For
The left-hand side of a rule.
The right-hand side of a rule.
A rule in Chomsky normal form as a context-free rule.
A context-free grammar in Chomsky normal form over terminals T: rules A → a and
A → B C, together with the rule S → ε for the start symbol S when nilRule is set. With
that rule, S occurs on no right-hand side.
- NT : Type
The type of nonterminals.
- initial : self.NT
The start symbol.
The rules
A → aandA → B C.- nilRule : Bool
Whether the grammar has the rule
initial → ε. - initial_notMem_output : self.nilRule = true → ∀ r ∈ self.rules, Symbol.nonterminal self.initial ∉ r.output
With the rule
initial → ε, the start symbol occurs on no right-hand side.
A grammar in Chomsky normal form as a Mathlib ContextFreeGrammar.
The rules of G.toCFG are those of G and, with nilRule, the rule initial → ε.
A nonterminal A derives a nonempty word w exactly when w is a letter a with a rule
A → a, or w = u ++ v for nonempty u and v and a rule A → B C such that B derives u
and C derives v.