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.
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)),
ContextFreeRule.Rewrites r (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)},
ContextFreeRule.Rewrites r s₁ s₂ → ContextFreeRule.Rewrites r (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
.
Add extra prefix to context-free rewriting.
Add extra postfix to context-free rewriting.
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
- ContextFreeGrammar.Produces g u v = ∃ r ∈ g.rules, ContextFreeRule.Rewrites r 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.
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
- ContextFreeGrammar.Generates g s = ContextFreeGrammar.Derives g [Symbol.nonterminal g.initial] s
Instances For
The language (set of words) that can be generated by a given context-free grammar g
.
Equations
- ContextFreeGrammar.language g = {w : List T | ContextFreeGrammar.Generates g (List.map Symbol.terminal w)}
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.
Add extra prefix to context-free producing.
Add extra postfix to context-free producing.
Add extra prefix to context-free deriving.
Add extra prefix to context-free deriving.