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.
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.
Context-free grammar that generates words over the alphabet T
(a type of terminals).
- NT : Type uN
Type of nonterminals.
- initial : self.NT
Initial nonterminal.
- rules : List (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 uT} {N : Type uN} {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 uT} {N : Type uN} {r : ContextFreeRule T N} (x : Symbol T N) {s₁ s₂ : List (Symbol T N)},
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
.
Equations
- g.Produces u v = ∃ r ∈ g.rules, r.Rewrites u 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
- g.Derives = Relation.ReflTransGen g.Produces
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.
Equations
- g.Generates s = g.Derives [Symbol.nonterminal g.initial] s
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.
Equations
- r.reverse = { input := r.input, output := r.output.reverse }
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.