COT 4210
Section F
Spring 2001
Context-free Grammars and Languages
Definition 25
.
A
Context-free grammar (CFG)
is a PSG, G = (N,
, P, S), where P
N G V
G
*.
A production r
P is denoted r: X
w
and means that an occurrence of X can be replaced by w
in any context
in which X occurs - this is where the term "context free" originates - rewriting X
"free of any contextual constraints."
NOTE
:
Context-free grammars were defined by Chomsky
to be Phrase Structure grammars with the
Type-2
restriction.
CFG
and
Type-2
grammars are
synonymous terms.
The family of all languages defined by Context-free grammars is called the family of Context-
free Languages
(CFLs).
Example 31.
G
e
= ( {E, T, F, X}, {n,v, +, -, *, /,
( , )}, P, E), where
E = { 1: E ’ E + T,
2: E ’ E
T,
3: E
T,
4: T T F,
5: T ’ T/ F,
6: T
F,
7: F
+X
8: F
X
9: F L X
10: X
n,
11: X
v,
12: X
( E ),
}
L(G
e
) = { x
∈
{n, v, +, -, *, /,
( , )}*
| x denotes a well-formed arithmetic expression over the
operator symbols {+, ,
, /
} and operand symbols {v, n}allowing parenthesized sub-
expressions nested arbitrarily deep.}
A derivation of x = (n+v) n
∈
L(G
e
) is illustrated below.
In general, x
∈
L(G) may have several
distinct derivations.
Note
:
refers to G
e
[1]
E
3
T
4
’ T F
6
F F
9
’ X F
12
(E)L F
1
(E+T)
F
3
(T+T)L F
6
(F+T)L F
9
(X+T)L F
10
(n+T)L F
6
(n+F)
F
9
(n+X)L F
11
(n+v)
F
9
(n+v)
X
10
(n+v)L n
Exercise 14:
Show that 3469(10)9(12)169(11)369(10)
is also a derivation for (n+v)
n in G
e
.
Can you construct yet another derivation for (n+v) n in G
e
that is distinct from either of the ones
already identified?
March 28, 2001
Page 61