Context-free grammar
A
context-free grammar
G
is a 4-tuple (
V,
Σ
,R,S
)where
•
V
is an
alphabet
(containing nonterminals and termi-
nals),
•
Σ(
⊆
V
) is a nonempty set of
terminals
(elements of
V
−
Σ
are the
nonterminals
),
•
R
is a nonempty, Fnite set of
rules
,with
R
⊆
(
V
−
Σ)
×
V
∗
.
•
S
is the
start symbol
(
∈
V
−
Σ).
Example:
G
=(
Σ
)
where
V
=
{
a, b, M, A, B
}
Σ=
{
a, b
}
R
=
{
(
S, aMb
)
,
(
M, A
)
,
(
M, B
)
,
(
A, e
)
,
(
A, aA
)
,
(
B,e
)
,
(
B,bB
)
}
•
instead of writing (
A, u
), we will simply write
A
→
u
.
Note: in general, a grammar (not necessarily a CFG) can have rules
that replace any string of terminals or nonterminals by any other string
of terminals or nonterminals. For example,
aB
→
bA
could be a rule.
CFG restricts the rules to be
R
⊆
(
V
−
Σ)
×
V
∗
, which means the
left-hand side of each rule must be a single nonterminal.
2