Context-Free Grammars #
This file contains the definition of a context-free grammar, which is a grammar that has a single nonterminal symbol on the left-hand side of each rule.
We restrict nonterminals of a context-free grammar to Type because universe polymorphism would be
cumbersome and unnecessary; we can always restrict a context-free grammar to the finitely many
nonterminal symbols that are referred to by its finitely many rules.
Main definitions #
ContextFreeGrammar: A context-free grammar.ContextFreeGrammar.language: A language generated by a given context-free grammar.
Main theorems #
Language.IsContextFree.reverse: The class of context-free languages is closed under reversal.
Equations
- instReprContextFreeRule = { reprPrec := reprContextFreeRule✝ }
Context-free grammar that generates words over the alphabet T (a type of terminals).
- NT : Type
Type of nonterminals.
- initial : self.NT
Initial nonterminal.
- rules : Finset (ContextFreeRule T self.NT)
Rewrite rules.
Instances For
Inductive definition of a single application of a given context-free rule r to a string u;
r.Rewrites u v means that the r sends u to v (there may be multiple such strings v).
- head
{T : Type u_1}
{N : Type u_2}
{r : ContextFreeRule T N}
(s : List (Symbol T N))
: r.Rewrites (Symbol.nonterminal r.input :: s) (r.output ++ s)
The replacement is at the start of the remaining string.
- cons
{T : Type u_1}
{N : Type u_2}
{r : ContextFreeRule T N}
(x : Symbol T N)
{s₁ s₂ : List (Symbol T N)}
(hrs : r.Rewrites s₁ s₂)
: r.Rewrites (x :: s₁) (x :: s₂)
There is a replacement later in the string.
Instances For
Rule r rewrites string u is to string v iff they share both a prefix p and postfix q
such that the remaining middle part of u is the input of r and the remaining middle part
of u is the output of r.
Given a context-free grammar g and strings u and v
g.Produces u v means that one step of a context-free transformation by a rule from g sends
u to v.
Instances For
Given a context-free grammar g and strings u and v
g.Derives u v means that g can transform u to v in some number of rewriting steps.
Equations
Instances For
Given a context-free grammar g and a string s
g.Generates s means that g can transform its initial nonterminal to s in some number of
rewriting steps.
Instances For
The language (set of words) that can be generated by a given context-free grammar g.
Instances For
A given word w belongs to the language generated by a given context-free grammar g iff
g can derive the word w (wrapped as a string) from the initial nonterminal of g in some
number of steps.
Context-free languages are defined by context-free grammars.
Equations
- L.IsContextFree = ∃ (g : ContextFreeGrammar T), g.language = L
Instances For
Rules for a grammar for a reversed language.
Instances For
Grammar for a reversed language.
Equations
Instances For
Alias of the reverse direction of ContextFreeGrammar.produces_reverse.
Alias of the reverse direction of ContextFreeGrammar.generates_reverse.
The class of context-free languages is closed under reversal.