Every context-free grammar has an equivalent one with finite nonterminals #
Since a ContextFreeGrammar stores its rules in a Finset, only finitely many
nonterminals can ever appear in a derivation. We restrict the nonterminal type to
those that actually occur in the grammar, producing an equivalent grammar whose
nonterminal type is Fintype.
Main results #
ContextFreeGrammar.toFiniteNT— restrict any CFG to one withFintype NT.ContextFreeGrammar.toFiniteNT_language— the restricted grammar generates the same language.ContextFreeGrammar.exists_fintype_nt— every context-free language has a grammar withFintype NT.
Used nonterminals #
The finite set of nonterminals occurring in the grammar (rules + start symbol).
Equations
- One or more equations did not get rendered due to their size.
Instances For
Equations
Symbol embedding and restriction #
Embed a symbol over the restricted nonterminals back to the original type.
Equations
- g.liftSymbol (Symbol.terminal t) = Symbol.terminal t
- g.liftSymbol (Symbol.nonterminal ⟨n, property⟩) = Symbol.nonterminal n
Instances For
Embed a word over restricted nonterminals back to the original type.
Equations
- g.liftWord = List.map g.liftSymbol
Instances For
Restrict a single symbol, given that any nonterminal in it is used.
Equations
- g.restrictOneSymbol (Symbol.terminal t) x = Symbol.terminal t
- g.restrictOneSymbol (Symbol.nonterminal n) h_2 = Symbol.nonterminal ⟨n, ⋯⟩
Instances For
All-used predicate #
All nonterminals in a word are used.
Instances For
Word restriction #
Restrict a word with all-used nonterminals to the restricted type.
Equations
- g.restrictWord w hw = List.pmap (fun (s : Symbol T g.NT) (hs : s ∈ w) => g.restrictOneSymbol s ⋯) w ⋯
Instances For
The restricted grammar #
Restrict a rule to used nonterminals.
Equations
- g.restrictRule r hr = { input := ⟨r.input, ⋯⟩, output := g.restrictWord r.output ⋯ }
Instances For
The context-free grammar with nonterminals restricted to the finite used set.
Equations
- One or more equations did not get rendered due to their size.