1 Context Free Grammars
1.1 Deﬁnitions
ContextFree Grammars
Deﬁnition 1.
A
contextfree grammar
(CFG) is
G
= (
V,
Σ
,R,S
) where
•
V
is a ﬁnite set of variables/nonterminals.
•
Σ is a ﬁnite set of terminals. Σ is disjoint from
V
.
•
R
is a ﬁnite set of rules or productions of the form
A
→
α
where
A
∈
V
and
α
∈
(
V
∪
Σ)
*
•
S
∈
V
is the start symbol
Example: Palindromes
Example
2
.
A string
w
is a
palindrome
if
w
=
w
R
.
G
pal
= (
{
S
}
,
{
0
,
1
}
,R,S
) deﬁnes palindromes over
{
0
,
1
}
, where
R
is
S
→
±
S
→
0
S
→
1
S
→
0
S
0
S
→
1
S
1
Or more brieﬂy,
R
=
{
S
→
±

0

1

0
S
0

1
S
1
}
Language of a CFG
Derivations
Expand the start symbol using one of its rules. Further expand the resulting string by expanding
one of the variables in the string, by the RHS of one of its rules. Repeat until you get a string of
terminals. For the grammar
G
pal
= (
{
S
}
,
{
0
,
1
}
,
{
S
→
±

0

1

0
S
0

1
S
1
}
,S
) we have
S
⇒
0
S
0
⇒
00
S
00
⇒
001
S
100
⇒
0010100
Derivations
Formal Deﬁnition
Deﬁnition 3.
Let
G
= (
V,
Σ
,R,S
) be a CFG. We say
αAβ
⇒
G
αγβ
, where
α,β,γ
∈
(
V
∪
Σ)
*
and
A
∈
V
if
A
→
γ
is a rule of
G
.
We say
α
*
⇒
G
β
if either
α
=
β
or there are
α
0
,α
1
,...α
n
such that
α
=
α
0
⇒
G
α
1
⇒
G
α
2
⇒
G
··· ⇒
G
α
n
=
β
2