Symbols, rules and derivations of a context-free grammar #
Lemmas on mathlib's Symbol, ContextFreeRule and ContextFreeGrammar that the derivation
trees, the closure properties and the weighted grammars share. [UPSTREAM] candidate for
Mathlib/Computability/ContextFreeGrammar.lean.
Main definitions #
Symbol.terminal?,Symbol.IsNonterminal,Symbol.mapNonterminal: the terminal at a symbol, the nonterminal predicate, and relabelling of nonterminals.ContextFreeRule.NonterminalPos: the positions of a right-hand side holding a nonterminal.ContextFreeGrammar.RulesWithLHS: the rules of a grammar with a given left-hand side.DFA.toContextFreeGrammar M: the right-linear grammar of a finite automaton over a finite alphabet.
Main results #
ContextFreeGrammar.Derives.append_split: a derivation from a concatenation splits into derivations from the two halves.ContextFreeGrammar.produces_map_terminal_append_singleton_append_iff: the one-step productions of a word with a single nonterminal, the step of linear grammars.ContextFreeGrammar.Derives.terminal_mem: a derivation introduces only terminals that occur in the output of a rule.DFA.language_toContextFreeGrammar: the right-linear grammar ofMgenerates the language ofM, so over a finite alphabet regular languages are context-free (Language.IsRegular.isContextFree).Language.isContextFree_top_iff: the language of all words is context-free exactly when the alphabet is finite.
Implementation notes #
The right-linear grammar of a finite automaton M has a nonterminal for each state, a rule
q → a q' for each transition from q to q' on the letter a, and a rule q → ε for each
accepting state q; its sentential forms are a word w followed by the state M.eval w, or an
accepted word. The alphabet must be finite, because a grammar has finitely many rules and so uses
finitely many letters: over an infinite alphabet the language of all words is regular, by a
one-state automaton, but not context-free.
References #
The nonterminal symbols.
Equations
- (Symbol.terminal a).IsNonterminal = False
- (Symbol.nonterminal n).IsNonterminal = True
Instances For
Equations
- (Symbol.terminal a).instDecidablePredIsNonterminal = isFalse ⋯
- (Symbol.nonterminal n).instDecidablePredIsNonterminal = isTrue trivial
Relabel the nonterminals of a symbol.
Equations
- Symbol.mapNonterminal f (Symbol.terminal a) = Symbol.terminal a
- Symbol.mapNonterminal f (Symbol.nonterminal n) = Symbol.nonterminal (f n)
Instances For
The positions on the right-hand side of r that hold a nonterminal: the slots at which a
derivation from r branches.
Equations
- r.NonterminalPos = { i : Fin r.output.length // r.output[i].IsNonterminal }
Instances For
A rewrite of a concatenation u₁ ++ u₂ happens entirely in u₁ or entirely in u₂.
No rule rewrites a word of terminals.
A rewrite of a word with a single nonterminal replaces that nonterminal by the output of a rule with that input.
The rules of g with left-hand side a.
Equations
- g.RulesWithLHS a = ↥({x ∈ g.rules | x.input = a})
Instances For
A production from a concatenation u₁ ++ u₂ happens entirely in u₁ or entirely in
u₂.
A derivation from a concatenation u₁ ++ u₂ splits into derivations from u₁ and from
u₂.
A word of terminals produces nothing.
A word with a single nonterminal produces exactly the words obtained by replacing that nonterminal with the output of one of its rules.
A word ending in its only nonterminal produces exactly the words obtained by replacing that nonterminal with the output of one of its rules, the step of right-linear grammars.
A terminal of a derived word is a terminal of the source word or of the output of a rule.
Regular languages #
The right-linear grammar of M: a nonterminal for each state, a rule q → a (M.step q a) for
each state q and letter a, and a rule q → ε for each accepting state q.
Equations
- One or more equations did not get rendered due to their size.
Instances For
The grammar of M derives every word followed by the state M reaches on it.
The sentential forms of the grammar of M are a word followed by the state M reaches on it,
and the words M accepts.
The grammar of M generates the language of M.
Over a finite alphabet, every regular language is context-free.
The language of all words is context-free exactly when the alphabet is finite: it is regular over any alphabet, but a grammar has finitely many rules and so uses finitely many letters.