<< стр. 6(всего 9)СОДЕРЖАНИЕ >>
so letting W1 = U and W2 = V we are done.
Assume the lemma is true for all derivations with less than k steps. Assume
U V в‡’в€— W contains k steps. As above assume the п¬Ѓrst step is U V в‡’ U V
where U в‡’ U . Note that U V в‡’в€— W uses only k в€’ 1 steps. By induction there
are derivations U в‡’в€— W1 , V в‡’в€— W2 containing at most k steps. Therefore
U в‡’ U , U в‡’в€— W1 , V в‡’в€— W2 are the required derivations.
One of the results of this lemma is that we can get from U V в‡’в€— W by the
derivations
U V в‡’в€— W1 V в‡’в€— W1 W2
where W = W1 W2 since if X О± в‡’ X ОІ is a derivation, then so are X О± V в‡’ X ОІ V
and U X О± в‡’ U X ОІ .
Grammars
140

The next lemma shows us that in applying productions, the order in which
we apply them is not particularly important. In fact we can derive any word
w in the language generated by by replacing the leftmost nonterminal by a
production at each step in the derivation. This is called a leftmost derivation
of w.

Lemma 4.3 Let w в€€ (L), the language generated by = (N , T, S, P).
There exists a leftmost derivation of w.

Proof The proof uses induction of the number of steps n in the derivation. If
n = 1, the derivation S в‡’ w is obviously a leftmost derivation. If n = k > 1,
and S в‡’в€— w, let S в‡’ U V where U, V в€€ (N в€Є T )в€— . By Lemma 4.2, there exists
derivations U в‡’в€— w1 and V в‡’в€— w2 , where w = w1 w2 . Since both of these
derivations contain less than k steps, there exist leftmost derivations U в‡’в€—
w1 and V в‡’в€— w2 . Then S в‡’ U V в‡’в€— w1 V в‡’в€— w1 w2 is a leftmost derivation
of w.

The following lemma shows that if the language L generated by a context-
free grammar does not contain the empty word О» then L can be generated by a
context-free grammar which does not contain any productions of the form A в†’
О», which we shall call a О» production. The only purpose of such a production is
to remove A from the string of symbols. For example if we have productions
C в†’ abBa Aaaa and A в†’ О», we can then derive C в‡’в€— abBaaaa. We could
simply remove A в†’ О» and replace it with abBa Aaaa в†’ abBaaaa. We would
have to do this wherever A occurs in a production. For example if we have
the C в†’ ab Aa Aba, if A в†’ О» is removed, we would have to include C в†’
ab Aaba, C в†’ aba Aba, and C в†’ ababa.
Suppose we have productions A в†’ a B, B в†’ C, C в†’ aa, C в†’ О». If we
add the production B в†’ О», we have created a new О» production. If we just
remove C в†’ О», we can no longer derive a. A nonterminal X is called nilpotent
if X в‡’в€— О». We solve the problem above by removing all nilpotents and not just
those directly from О» productions. Thus we would also add the production
A в†’ a above since B is nilpotent.
In the next lemma it is necessary to be able to determine the nilpotents of a
grammar. The following algorithm determines the set of all nilpotents in a
grammar by examining the productions.

(1) If A в†’ О» is a production, then A в€€ .
(2) If A в†’ A1 A2 . . . An where A1 , A2 , . . . , An в€€ , then A в€€ .
(3) Continue (2) until no new elements are added to .
4.2 Chomsky normal form and Greibach normal form 141

Lemma 4.4 Let be a grammar such that (L) does not contain the empty
word. Form a grammar by beginning with the productions in and
(i) removing all О» productions;
(ii) for each production A в†’ w in , where w = w1 X 1 w2 X 2 . . . wn X n and
(not necessarily distinct) nilpotents X 1 , X 2 , . . . , X n in w, let P be the
power set of {1, 2, . . . , n} and for p в€€ P, form productions A в†’ w p
where w p is the string w with the {X i : i в€€ p} removed, which produce О»
productions.
The language generated by is equal to the language generated by .
Proof Let L be the language generated by = (N , T, S, P), and L be the
= (N , T, S, P ). The language L вЉ† L, since any
language generated by
production in which is not in can be replaced with the original productions
in used to deп¬Ѓne it.
To show L вЉ† L , let w в€€ L. Using induction on the number of steps in the
derivation, we show that if S в‡’в€— w using productions in P, then S в‡’в€— w using
productions in P . If n = 1 then S в‡’ w is obviously a production in P since
w = О». Assume n = k and S в‡’в€— w is a derivation in containing k steps. Let
S в‡’ A1 A2 A3 . . . Am be the п¬Ѓrst derivation where Ai в€€ N в€Є T . Therefore
S в‡’ A1 A2 A3 . . . Am в‡’в€— w is the derivation S в‡’в€— w.
By Lemma 4.2, there exist derivations Ai в‡’в€— wi in , for 1 в‰¤ i в‰¤ m, where
w = w1 w2 . . . wm and each derivation has less than k steps. By induction, if
wi = О» there exists derivations Ai в‡’в€— wi in for 1 в‰¤ i в‰¤ m (note that if Ai
is a terminal then Ai = wi and Ai в‡’в€— wi has 0 steps). Let Ai = Ai if wi = О»
and Ai = О» if wi = О». Then S в‡’ A1 A2 A3 . . . Am is a derivation in and
S в‡’ A1 A2 A3 . . . Am в‡’ в€— w1 A2 A3 . . . Am в‡’ в€— w1 w2 A3 . . . Am в‡’ в€— w1 w2 . . . wm
is a derivation in .
For future reference, we point out that if one follows the proof above care-
fully, one п¬Ѓnds that if (L) had contained the empty word, the only difference
between (L) and (L ) is that (L) would have contained the empty word,
while (L ) does not. The nonterminal S is a nilpotent that does not get removed,
however if S в†’ О» is a production, the production is removed.
We now start making progress toward Chomsky normal form. We show
that given a grammar we can determine another grammar which generates the
same language and has no productions of the form A в†’ B, where A, B в€€ N .
These productions are called trivial productions since they simply relabel
nonterminals. The process is simple. If A в†’ B and B в†’ W , where W в€€ (T в€Є
Grammars
142

N )в€— , we remove A в†’ B and include A в†’ W . More generally, if A1 в†’ A2 в†’
A3 в†’ В· В· В· в†’ Am в‡’в€— B, where each Ai в†’ Ai+1 is a trivial production and
B в†’ w, then remove the trivial productions and include A1 в†’ w.

Lemma 4.5 If (L), the language generated by = (N , T, S, P), does not
contain the empty word, then there exists a grammar with no О» productions
and no trivial productions such that (L) = (L ).

Proof First assume has had the О» productions removed as shown in the pre-
vious theorem. Create by removing all of the trivial projections and wherever
A1 в†’ A2 в†’ A3 в†’ В· В· В· в†’ Am в‡’в€— B occurs, where each Ai в†’ Ai+1 is a triv-
ial production and B в†’ w, then remove the trivial productions and include
A1 в†’ B.
By construction, (L ) вЉ† (L).
Conversely, assume S в‡’в€— w occurs in . We use induction on the number
of trivial derivations to show that there is a derivation S в‡’в€— w in . Obviously
if there is no trivial production then the derivation is in . Assume there are
k trivial productions in the derivation. Assume that the derivation is a leftmost
derivation of w. Assume S в‡’в€— w has the form
S в‡’в€— V1 A1 V2 в†’ w1 A1 V2 в†’ w1 A2 V2 в†’ w1 A3 V2 в†’ В· В· В· в†’ w1 Am V2
в‡’в€— w1 w V2
в‡’ w1 w w2

where A1 в†’ A2 в†’ A3 в†’ В· В· В· в†’ Am is the last sequence of trivial productions
in the derivation, and Am в†’ w . Then there are derivations V1 в‡’в€— w1 , V2 в‡’в€—
w2 in , and
S в‡’в€— V1 A1 V2 в‡’в€— w1 A1 V2 в‡’ w1 w V2 в‡’в€— w1 w w2

has less trivial productions and all productions are in в€Є . Hence by the
induction hypothesis there is a derivation S в‡’в€— w in .

Lemma 4.6 If (L), the language generated by = (N , T, S, P), does not
= (N , T, S, P ) in
contain the empty word, then there exists a grammar
which every production either has the form A в†’ A1 A2 A3 . . . Am for n в‰Ґ 2
where A, A1 , A2 , A3 , . . . , Am are nonterminals or A в†’ a where A is a non-
terminal and a is a terminal such that (L) = (L ).

Proof Assume all О» productions and all trivial productions have been elim-
inated. Thus all productions are of the form A в†’ A1 , A2 , A3 , . . . , Am where
m в‰Ґ 2 and Ai в€€ N в€Є T or A в†’ a where A is a nonterminal and a is a ter-
minal. If A1 , A2 , A3 , . . . , Am all are nonterminals then the production has the
4.2 Chomsky normal form and Greibach normal form 143

proper form. If not, for each Ai = ai where ai is a terminal, form a new nonter-
minal X ai . Replace A в†’ A1 , A2 , A3 , . . . , Am with A1 , A2 , A3 , . . . , Am where
Ai = Ai if Ai is a nonterminal and Ai = X ai if Ai is a terminal. Thus if we
have V1 a1 V2 a2 V3 a3 . . . Vn an Vn+1 where Vi в€€ N в€— and ai is a terminal, replace
it with V1 X a1 V2 X a2 V3 X a3 . . . Vn X an Vn+1 and add productions X ai в†’ ai for
1 в‰¤ i в‰¤ n. Let = (N , T, S, P ) be the new grammar formed. We need to
show that (L ) = (L). Clearly (L) вЉ† (L ) since
A в‡’ V1 a1 V2 a2 V3 a3 . . . Vn an Vn+1
in can be replaced by
Aв‡’ V1 X a1 V2 X a2 V3 X a3 . . . Vn X an Vn+1
в‡’ V1 a1 V2 X a2 V3 X a3 . . . Vn X an Vn+1
в‡’ V1 a1 V2 a2 V3 X a3 . . . Vn X an Vn+1
. .
. .
. .
в‡’ V1 a1 V2 a2 V3 a3 . . . Vn an Vn+1
in .
Conversely assume that in the derivation
S в‡’в€— w1 ww2
where U в‡’в€— w1 and A в‡’в€— w and Vi в‡’ vi are productions in
S в‡’в€— U AU в‡’ U V1 X a1 V2 X a2 . . . Vn X an Vn+1 V
в‡’в€— w1 v1 a1 v2 a2 v3 a3 . . . vn an vn+1 w2 = w1 ww2 ,
where
A в†’ V1 X a1 V2 X a2 . . . Vn X an Vn+1
is a production which is in and not in and the derivation is
S в‡’в€— U AV
в‡’в€— w1 AV
в‡’ w1 V1 X a1 V2 X a2 . . . Vn X an Vn+1 V
в‡’в€— w1 v1 X a1 V2 X a2 . . . Vn X an Vn+1 V
в‡’ w1 v1 a1 V2 X a2 . . . Vn X an Vn+1 V
в‡’в€— w1 v1 a1 v2 X a2 . . . Vn X an Vn+1 V
в‡’ w1 v1 a1 v2 a2 V3 X a3 . . . Vn X an Vn+1 V
..
..
..
в‡’ w1 v1 a1 v2 a2 v3 a3 . . . vn an Vn+1 V
в‡’в€— w1 v1 a1 v2 a2 v3 a3 . . . vn an vn+1 V
в‡’в€— w1 ww2 .
Grammars
144

This may be replaced by

S в‡’в€— U V1 a1 V2 a2 . . . Vn an Vn+1 V
в‡’в€— w1 V1 a1 V2 a2 . . . Vn an Vn+1 V
в‡’в€— w1 v1 a1 V2 a2 . . . Vn an Vn+1 V
. .
. .
. .
в‡’в€— w1 v1 a1 v2 a2 . . . an Vn+1 V
в‡’в€— w1 v1 a1 v2 a2 . . . an vn+1 V
в‡’в€— w1 v1 a1 v2 a2 . . . an vn+1 w2
в‡’в€— w1 ww2 .

We have a derivation for S в‡’в€— w1 ww2 in . Hence вЉ†.

From the above lemmas we are now able to prove that a context-free gram-
mar whose language does not contain the empty word can be expressed in
Chomsky normal form.
Lemma 4.7 If (L), the language generated by = (N , T, S, P), does not
contain the empty word, then there exists a grammar in which every production
has either the form

A в†’ BC

or

Aв†’a

where A, B, and C are nonterminals and a is a terminal such that (L) = (L ).

Proof By the previous lemma, in which every production has either the form
A в†’ A1 A2 A3 . . . Am where A, A1 , A2 , A3 , . . . , Am are nonterminals or A в†’
a where A is a nonterminal and a is a terminal. We construct a new grammar
by replacing every production of the form A в†’ A1 A2 A3 . . . Am by the set
of productions A в†’ A1 X 1 , X 1 в†’ A2 X 2 , . . . , X mв€’2 в†’ Amв€’1 Am , where each
replacement of a production in uses a new set of symbols.

A в‡’ A 1 X 2 в‡’ A 1 A 2 X 3 в‡’в€— A 1 A 2 A 3 . . . A m

is a derivation in , (L) вЉ† (L ).
Conversely, if S в‡’в€— w in contains no productions which are not in , then
w в€€ (L). If it does, let Wm be the last term in the derivation containing a symbol
which is not in so we have Wm в‡’ Wm+1 в‡’в€— w and Wm в‡’ Wm+1
in
has the form U X mв€’2 V в‡’ U Amв€’1 Am V . Therefore the derivation uses the set
4.2 Chomsky normal form and Greibach normal form 145

of productions A в†’ A1 X 1 , X 1 в†’ A2 X 2 , . . . , X mв€’2 в†’ Amв€’1 Am and has the
form
S в‡’в€— U AV в€— в‡’в€— в‡’в€—
U A1 X 1 V U A1 X 1 V
в‡’в€— в‡’в€—
U A1 A2 X 2 V U A1 A2 X 2 V
в‡’в€— в‡’в€—
U A1 A2 A3 X 3 V U A1 A2 A3 X 3 V
. . . .
. . . .
. . . .
в‡’в€— U A1 В· В· В· Amв€’2 X mв€’2 V в‡’в€— U A1 В· В· В· Amв€’2 X mв€’2 V
в‡’в€— w
в‡’ Wm+1

where U = U A1 A2 A3 В· В· В· Amв€’2 and Ai в‡’в€— Ai is a derivation in . If this
derivation S в‡’в€— w is not in , we again pick the last term in the derivation
containing a symbol in which is not in , and continue the process until no
such terms are left. Therefore w в€€ and (L ) вЉ† (L).

Finally we remove the restriction that (L) contains the empty word. As
mentioned, following the proof of Lemma 4.4, by eliminating О» productions, if
(L) contained the empty word, one produced the same language with only the
empty word eliminated. Since all of the languages of the forms of grammars
developed since Lemma 4.4 are the same, if (L) contained the empty word,
the language developed by the grammar in the previous lemma would have
differed from (L) only in the fact that (L) contained the empty word while
(L ) did not. Thus to get (L) we need only have productions that add the
empty word to the language and leave the rest of the language alone. We do
this by adding two new nonterminal symbols, S and П€, where S is the new
start symbol and productions S в†’ SП€ and П€ в†’ О». Call this the О» extended
Chomsky normal form.

Theorem 4.4 Given a context-free grammar containing the empty word,
there is a context-free grammar in О» extended Chomsky normal form so that
(L) = (L ).

We now consider converting a context-free language to Greibach normal
form. Even though we use leftmost derivations, we have no bound on how
many derivations may occur before the п¬Ѓrst terminal symbol appears at the left
of the string. For example, using the production A в†’ Aa, we can generate the
string Aa n for arbitrary n, using n derivations without beginning a string with
a terminal symbol. We can eliminate this particular problem by eliminating the
productions of the form A в†’ Aa. This is called elimination of left recursion.
In a grammar with no О» productions or trivial productions, let

A в†’ AV1 , A в†’ AV2 , . . . , A в†’ AVn
Grammars
146

be productions in which the right-hand side of the production begins with an A
and

A в†’ U1 , A в†’ U2 , . . . , A в†’ Um

be productions in which the right-hand side of the production does not begin
with an A. We form a new grammar by adding a new nonterminal A to the
:grammar and using the following steps:

(1) Eliminate all productions of the form A в†’ AVi for 1 в‰¤ i в‰¤ n.
(2) Form productions A в†’ Ui A for 1 в‰¤ i в‰¤ n.
(3) Form productions A в†’ Vi A and A в†’ Vi .

(L) =
Lemma 4.8 (L).

Proof Let a derivation beginning with A have the form assuming A в†’ Ui A
for 1 в‰¤ i в‰¤ n and we have A в‡’ AV(1) в‡’ AV(2) V(1) в‡’в€— AV(k) . . . V(2) V(1) в‡’
U(i) V(k) . . . V(2) V(1) where V( j) в€€ {V1 , V2 , . . . , Vn } for all 1 в‰¤ j в‰¤ k and U(i) в€€
{U1 ,U2 , . . . , Um }. Therefore using leftmost derivation, any production contain-
ing A will have the form

w AW в‡’ w AV(1) W в‡’ w AV(2) V(1) W в‡’в€— w AV(k) . . . V(2) V(1) W
в‡’ wU(i) V(k) . . . V(2) V(1) W.

But

A в‡’ AV(1) в‡’ AV(2) V(1) в‡’в€— AV(k) . . . V(2) V(1) в‡’ U(i) V(k) . . . V(2) V(1)

can be replaced by

A в‡’ U(i) A в‡’ U(i) V(k) A в‡’ U(i) V(k) V(kв€’1) A
в‡’ в€— U(i) V(k) . . . V(2) A в‡’ U(i) V(k) . . . V(2) V(1) .

Placing w on the left and W on the right of each term, we have, w AW в‡’в€—
wU(i) V(k) . . . V(2) V(1) W in . Hence (L) вЉ† (L).
The proof that (L) вЉ† (L) is left to the reader.

Lemma 4.9 Let A в†’ U BV be a production in and B в†’ W1 , B в†’
W2 , . . . , B в†’ Wm be the set of all productions in with B on the left. Let
be the grammar with production A в†’ U BV removed and the productions
A в†’ U Wi V for 1 в‰¤ i в‰¤ m added, then (L) = (L).

Proof The production A в†’ U Wi V can always be replaced by the production
A в†’ U BV followed by the production B в†’ Wi . Hence (L) вЉ† (L). The
proof that (L) вЉ† (L) is left to the reader.
4.2 Chomsky normal form and Greibach normal form 147

The theorem that every contextвЂ“free grammar can be expressed in Greibach
normal form can be proved by п¬Ѓrst expressing the grammar in Chomsky normal
form. We shall not do so however so that the development for Chomsky normal
form may be omitted if desired. Using the above lemmas, we are about to take
a giant leap toward proving that every context-free grammar can be expressed
in Greibach normal form.

Lemma 4.10 Any context-free grammar which does not generate О» can be
expressed so that each of its productions is of the form

A в†’ aW,

where a is a terminal and W is a string which is empty or consists of a string
of terminals and/or nonterminals.

Proof We п¬Ѓrst order the nonterminals beginning with S, the start symbol.
For simplicity, let the nonterminals be A1 , A2 , A3 , . . . , Am . Our п¬Ѓrst goal is to
change every production so that it is either in the form

A в†’ aW,

where a is a terminal and W is a string which is empty or consists of a string
of terminals and/or nonterminals, or in the form

Ai в†’ A j Y,

where i < j and Y consists of a string of terminals and/or nonterminals. Recall
that using the procedures for elimination of left recursion and for eliminating
a nonterminal described in Lemma 4.9 to alter the productions of the grammar
does not change the language generated by the grammar.
Using induction, for i = 1, since S = A1 is automatically less than every
other nonterminal, we need only consider S в†’ SY , the S on the right-hand side
of the production can be removed by the process of elimination of left recur-
sion. Assume it is true for every Ai where i < k. We now prove the statement
for i = k. In each case where Ak в†’ A j Y is a production for k > j, use the
procedure in Lemma 4.9 to eliminate A j . When Ak в†’ Ak Y is a production, use
the process of elimination of left recursion to remove Ak from the right-hand
side.
Therefore by induction we have every production so that it is either in the
form

A в†’ aW,
Grammars
148

where a is a terminal and W is a string which is empty or consists of a string
of terminals and/or nonterminals, or in the form

Ai в†’ A j Y,

where i < j and Y consists of a string of terminals and/or nonterminals.
Any production with Am on the left-hand side must have the form Am в†’ aW
since there is no nonterminal larger than Am . If there is a production of the
form Amв€’1 в†’ Am W , use the procedures in Lemma 4.9 to eliminate Am . The
result is a production of the form Am в†’ bW . Assume k is the largest value
so Ak в†’ A j Y is a production where k < j. Again using the procedures in
Lemma 4.9 to eliminate A j , we have a procedure of the form Ak в†’ aW . When
the process is completed, we have

Ai в†’ aW

where a is a terminal and W is a string which is empty or consists of a string
of terminals and/or nonterminals for every i. We now have to consider the Bi
created using a process of elimination of left recursion. From the construction
of the Bi , it is impossible to have a production of the form Bi в†’ B j W . There-
fore productions with Bi on the left have the form Bi в†’ aW or Bi в†’ A j W .
Repeating the process above we can change these to the form Bi в†’ aW , and
the lemma is proved.

Theorem 4.5 Every context-free grammar whose language does not contain
О» can be expressed in Greibach normal form.

Proof We outline the proof. The details are left to the reader. Since we already
know that every production can be written in the form

A в†’ aW,

where a is a terminal and W is a string which is empty or consists of a string
of terminals and/or nonterminals, for every terminal b in W replace it with
nonterminal Ab and add the production Ab в†’ b. Hint: see proof of Lemma 4.6.

For any context free grammarcontaining the empty word we can form a
grammar in extended Greibach normal form, which accepts the empty word
by simply adding the production Sв†’ О» after the completion of the Greibach
normal form.
4.3 Pushdown automata and context-free languages 149

Exercises
(1) In Lemma 4.9 вЂњLet A в†’ U BV be a production in and B в†’ W1 , B в†’
W2 , . . . , B в†’ Wm be the set of all productions in with B on the left.
Let be the grammar with production A в†’ U BV removed and the pro-
ductions A в†’ U Wi V for 1 в‰¤ i в‰¤ m added, then (L) = (L),вЂќ prove
(L) вЉ† (L).
(2) Prove Theorem 4.5 вЂњEvery context-free grammar can be expressed in
Greibach normal form.вЂќ
(3) Complete the proof of Lemma 4.8.
= (N , , S, P) be the grammar deп¬Ѓned by N = {S, A, B}, =
(4) Let
{a, b}, and P be the set of productions

S в†’ AB AB AB A A в†’ Aa Aв†’О» B в†’ b.

Express this grammar in Chomsky normal form.
(5) Express the previous grammar in Greibach normal form.
(6) Let = (N , T, S, P) be the grammar with N = {S}, T = {a, b}, and P
contain the productions

S в†’ SS B в†’ aa S в†’ BS B в†’ bb S в†’ SB
A в†’ ab Sв†’ О» A в†’ ba S в†’ AS A.
Express this grammar in Chomsky normal form.
(7) Express the previous grammar in Greibach normal form.
= (N , , S, P) be the grammar deп¬Ѓned by N = {S, A, B}, =
(8) Let
{a, b}, and P be the set of productions

S в†’ Aba B A в†’ b Aa Aв†’О» B в†’ A Ab Baab A.

Express this grammar in Chomsky normal form.
(9) Express the previous grammar in Greibach normal form.

4.3 Pushdown automata and context-free languages
The primary importance of the PDA is that a language is accepted by a PDA
if and only if it is constructed by context-free grammar. Recall that a context-
free language is a language that is generated by a context-free grammar. In the
remainder of this section, we show that a language is context-free if and only
if it is accepted by a PDA.
We п¬Ѓrst demonstrate how to construct a PDA that will read the language
generated by a context-free grammar.
Grammars
150

Before beginning we need two tools. The п¬Ѓrst is the concept of pushing a
string of stack symbols into the stack. We are not changing the deп¬Ѓnition of the
stack. To push a string into the stack we simply mean that we are pushing the last
symbol of the string into the stack, then the next to last symbol into the stack, and
continuing until the п¬Ѓrst symbol of the string has been pushed into the stack. If
the string is then popped a symbol at a time, the symbols form the original string.
For example to push ab Ac, we п¬Ѓrst push c, then push A, then push b, and п¬Ѓnally
push a. Thus we may consider a PDA to have the form M = { , Q, s, I, П’, F)
where is the alphabet, Q is the set of states, s is the initial or starting state, I is
the set of stack symbols, F is the set of acceptance states, and П’ is the transition
relation. The relation П’ is a п¬Ѓnite subset of ((Q Г— в€— Г— I в€— ) Г— (Q Г— I в€— )). This
means that the machine in some state q в€€ Q can read a possibly empty string
of the alphabet by reading a letter at a time if the string is nonempty, pop and
read a possibly empty string of symbols by popping and reading a symbol at a
time if the string of symbols in nonempty and as a result can read a string of
letters, change state, and push a string of symbols onto the stack as described
above.
Throughout the remainder of this section we shall assume that only left
derivations for context-free languages are used and that the PDA has only two
states, s and t. The alphabet in the PDA consist of the terminal symbols
of the grammar . The stack symbols of the PDA consist of the terminal and
nonterminal symbols of the grammar , i.e. I = T в€Є N .
To convert a context-free grammar into a PDA, which accepts the same
language generated by , we use the following rules:

(1) Begin by pushing S, start symbol of the grammar, i.e. begin with the
automaton.

Start

Push
(t) S

(2) If a nonterminal A is popped from the stack, then for some production
A в†’ w in , w is pushed into the stack, i.e. we have the automaton in
4.3 Pushdown automata and context-free languages 151

п¬Ѓgure

pop
A
push
w

(3) If a terminal a is popped from the stack then a must be read, i.e. if we have
the automaton

pop
a

then we have the automaton

pop
a

a

Thus the terminal elements at the top of the stack are removed and matched
with the letters from the tape.
The result is that we are imitating the productions in the grammar by popping
the п¬Ѓrst part of the production and pushing the second part so that while the
grammar replaces the п¬Ѓrst part of the production with the second part, so does
the PDA. The stack then resembles the strings derived in the grammar except
that the terminals on the left of the derived string (top of the stack) are then
removed as they occur in the stack and compared with the letters on the tape.
As before a word is accepted if the word has been read and the stack is empty.
Example 4.17 Let = (N , T, S, P) be the grammar with N = {S}, T =
{a, b}, and P contain the productions
S в†’ aSa S в†’ bSb Sв†’О»
Grammars
152

which generates the language {ww R : w в€€ T в€— }. This has the PDA

Start

push
S

pop
a
a
aSa
push
a accept
accept aSb
b push
b

Consider the word abba. We can trace its path with the following table:

state instruction stack tape state instruction stack tape

О»
x0 abba t pop b Sba bba
start
t push S S abba t read b Sba ba
О»
t pop S abba t pop S ba ba
t push aSa aSa abba t pop b a ba
t pop a Sa abba t read b a a
О»
t read a Sa bba t pop a a
О» О»
t pop S a bba t read a
О» О»
t push bSb bSba bba t accept

Example 4.18 Let = (N , T, S, P) be the grammar with N = {S}, T =
{a, b}, and P contain the productions

B в†’ bb S в†’ SB
Sв†’ SS Bв†’ aa Sв†’ B S
Sв†’ О» S в†’ AS A
Aв†’ ab Aв†’ ba

which generates the language {w : w в€€ Aв€— and contains an even number of as
and an even number of bs.}. This has the PDA
4.3 Pushdown automata and context-free languages 153

Start
A
S B push
push
push push S
S S push accept
S
A
push push
push
S
pop
A
B b
a
push push push push
a
b a b
a b
push push push push
a b a
b
accept

Consider the word abbabb. We can trace its path with the following table. In
this table to save space, strings will be pushed in as one operation rather than
pushing in each symbol.

instruction stack tape instruction stack tape
О» abbabb AS babb
start pop
push S S abbabb S babb
pop
О» abbabb push ba baS babb
pop
push SS SS abbabb aS babb
pop
S abbabb read a S bb
pop
О»
push AS A AS AS abbabb bb
pop
S AS abbabb push bb bb bb
pop
push ab abS AS abbabb b bb
pop
bS AS abbabb read b b b
pop
О»
read a bS AS bbabb b
pop
О» О»
S AS bbabb read b
pop
read b S AS babb

Before formally proving that a language (L) is context-free if and only
if it is accepted by a PDA, we shall adopt a notation for PDAs which will be
more convenient. We shall denote by an ordered triple the current condition of
the PDA. This triple consists of the current state of the machine, the remaining
string of input symbols to be read, and the current string in the stack. For
example the triple (s, aabb, Aa Ba B) represents the PDA in state s, with aabb
on the input tape, and Aa Ba B in the stack. Given triples (s, u, V ) and (t, v, W ),
then notation (s, u, V ) (t, v, W ) indicates that the PDA can be changed from
(s, u, V ) to (t, v, W ) in a single transition from F. For example the transition
Grammars
154

((a, s, B), (t, О»)) when the PDA is in condition (ab, s, Baa B) gives notation
(ab, s, Baa B) (b, t, aa B) and the transition ((a, s, О»), (t, D)) when the PDA
is in condition (ab, s, Baa B) gives notation (ab, s, Baa B) (b, t, D Baa B).
We say that (s, u, V ) в€— (t, v, W ) if the PDA can be changed from (s, u, V ) to
(t, v, W ) in a п¬Ѓnite number of transitions.
Lemma 4.11 Let (L) be a context-free language. There exists a PDA that
accepts L
M = ( , Q, s, I, П’, F)
where is a п¬Ѓnite alphabet, Q is a п¬Ѓnite set of states, s is the initial or starting
state, I is a п¬Ѓnite of stack symbols, П’ is the transition relation, and F is the set
of acceptance states. The relation П’ is a subset of
О»
Г— Q Г— I О» ) Г— (Q Г— I О» )).
((
Proof As previously mentioned, we shall assume that the PDA has two
states which we shall denote here as s and t so that M = { , Q, s, I, П’, F)
where , the alphabet, consists of the terminal symbols T of the grammar
= (N , T, S, P), Q = {s, t}, s is the initial or starting state, I consists of the
terminal and nonterminal symbols of the grammar, i.e. I = T в€Є N , the set of
stack symbols, T = {s, t}, and П’ is the transition relation deп¬Ѓned as follows:
(1) ((s, О», О»), (t, S)) в€€ П’ so (s, u, О») (t, u, S) for u в€€ T в€— . Begin by pushing
S, start symbol of the grammar, i.e. The automaton begins with

Start

Push
(t) S

(2) If A в†’ w in , then (t, О», A), (t, w)) в€€ П’ so (t, u, A) (t, u, w) for u в€€
T в€— . (If a nonterminal A is popped from the stack, then for some production
A в†’ w in , w is pushed into the stack, i.e. we have in the automaton

pop
A
push
w
4.3 Pushdown automata and context-free languages 155

(3) For all a в€€ T , ((t, a, a), (t, О»)) в€€ П’ so (t, au, aw) (t, u, w) for u в€€ T в€— .
If a terminal a is popped from the stack then a must be read, i.e. if we have
in the automaton

pop
a

it must be followed by

a

We shall assume that is in Chomsky normal form. The reader is asked to
prove the theorem when is in Greibach normal form.
Using left derivation, every string that is derived either begins with a terminal
or contains no terminal. In the corresponding PDA, the derived string is placed
in the stack using (2). If there is a terminal, it is compared with the next letter to
be read as input. If they agree, then the terminal is removed from the stack using
(3). If the word generated by the terminal is the same as the word generated
by grammar, each terminal will be removed from the stack as it is generated
leaving only an empty stack after the tape has been read.
We п¬Ѓrst show, assuming leftmost derivation in , that if S в‡’в€— О±ОІ where
О± в€€ T в€— and ОІ begins with a nonterminal or is empty, then (t, О±, S) в€— (t, О», ОІ).
Hence if S в‡’в€— О± in where О± в€€ T в€— , then (s, О±, О») (t, О±, S) в€— (t, О», О»),
and О± is accepted by the PDA. We prove this using induction on the length of
the derivation. Suppose n = 0, but then we have S в‡’в€— S, so О± = О», ОІ = S,
and (t, О», S) в€— (t, О», S) gives us (t, О±, S) в€— (t, О», ОІ). Now assume S в‡’в€— Оі
in k + 1 steps. Say
S в‡’ m 1 в‡’ m 2 в‡’в€— m k в‡’ m k+1 .
Then there is a п¬Ѓrst nonterminal B in the string m k and a produc-
tion B в†’ w so m k = u Bv and m k+1 = uwv. By the induction hypothe-
sis, since S в‡’в€— u Bv, (t, u, S) в€— (t, О», Bv). Since B в†’ w using relation
(2), we have (t, О», B), (t, w)) в€€ П’ and (t, О», Bv) (t, О», wv). If the pro-
duction B в†’ w has the form B в†’ C D, where C and D are nontermi-
nals, so that w = C D, then w begins with a nonterminal and (t, u, S) в€—
(t, О», Bv) (t, О», wv) or (t, u, S) в€— (t, О», wv) where m k+1 = uwv as desired.
Grammars
156

If the production has the form B в†’ a so that w = a, where a is a terminal,
then (t, ua, S) в€— (t, a, Bv) (t, a, av) (t, О», v) using derivation (3) above.
Hence (t, uw, S) в€— (t, О», v) where m k+1 = uwv as desired. Note that since
we are using leftmost derivation, v must begin with a nonterminal.
We now show that if (t, О±, S) в€— (t, О», ОІ) with О± в€€ T в€— , ОІ в€€ I в€— , then S в‡’в€—
О±ОІ. Hence if (s, О±, О») (t, О±, S) в€— (t, О», О») so О± is accepted by the PDA, then
S в‡’в€— О± so О± is generated by the grammar.
We again use induction on the length of the computation by the PDA. If
k = 0, then (t, О», S) в€— (t, О», S) so S в‡’в€— S, which is certainly true. Assume
(t, О±, S) в€— (t, О», ОІ) in k + 1 steps so that (t, О±, S) в€— (t, v, Оі ) in k steps and
(t, v, Оі ) (t, О», ОІ). If the transition relation for (t, w, Оі ) (t, О», ОІ) is relation
(2), then Оі = Bv, ОІ = wv and B в†’ w. Since no input is read, we have w = О».
Therefore by induction, S в‡’в€— О±Оі = О± Bv. Since B в†’ w, О± Bv в‡’ О±wv = О±ОІ.
Therefore S в‡’в€— О±ОІ. If the transition relation for (t, v, Оі ) (t, О», ОІ) is type
(3), then v = a and Оі = aОІ for some terminal a. But since a is the last input
read from the string О±, О± = au for u в€€ T в€— . Hence (t, u, S) в€— (t, О», Оі ) and by
induction S в‡’в€— uОі = uaОІ = О±ОІ.

For a given pushdown automaton M = ( , Q, s, I, П’, F), we next wish to
construct a context-free grammar = (N , T, S, P). The expression N shall be
of the form p, B, q , where p and q are states of the automaton and B is in the
stack. Thus p, B, q represents the input u read in passing from state p to state
q, where B is removed from the stack. In fact we shall have p, B, q в‡’в€— u.
The terminal p, О», q represents the input read in passing from state p to state
q and leaving the stack as it was in state p. The productions consist of the
following four types.

(1) For each q в€€ T, the production S в†’ s, О», q .
(2) For each transition (( p, a, B), (q, D)) в€€ П’, where B, D в€€ I в€Є {О»}, the
productions p, B, t в†’ a q, D, t for all t в€€ Q.
(3) For each transition (( p, a, D), (q, B1 B2 . . . Bn )) в€€ П’, where D в€€ I в€Є
{О»}, B1 , B2 , . . . , Bn в€€ C, the productions
p, D, t в†’ a q, B1 , q1 q1 , B2 , q2 q2 , B3 , q3 . . . qnв€’1 , Bn , t

for all q1 , q2 , . . . , qnв€’1 , t в€€ Q.
(4) For each q в€€ Q, the production q, О», q в†’ О».

The п¬Ѓrst statement intuitively says that at the beginning we need to generate
the entire word accepted by the PDA. The second statement intuitively says that
the output generated by p, B, t , which is the input to be read by the PDA in
state p using stack B moving to state t, is equal on the right-hand side of the
4.3 Pushdown automata and context-free languages 157

production to the input read in passing from state p with stack A to state q with
stack D followed by the output generated by q, D, t which is the input read
by the PDA in state q with stack D moving to state t.
The third statement intuitively says that the output generated by p, D, t ,
which is the input to be read by the PDA in state p with stack D moving to
state t, is equal on the right-hand side of the production to the input read in
passing from state p with stack D to state q with stack B1 B2 . . . Bn followed
by the output generated by q, B1 , q1 , the input read by passing from state q
using stack B1 to state q1 . . . followed by the output generated by q1 , B2 , q2 ,
the input read by passing from state q1 using stack B2 to state q2 . . . followed
by the output generated by qnв€’1 , Bn , t , the input read by passing from state
qnв€’1 using stack Bn to state t.
The fourth statement intuitively says that to move from a state to itself
requires no input. Note that the productions of type (4) are the only ones
which pop nonterminals without replacing them with other nonterminals. Hence
a word in the language of the grammar cannot be generated without these
productions.
Lemma 4.12 A language M(L) accepted by a pushdown automaton M =
( , Q, s, I, П’, F), is a context-free language.

Proof Using the grammar = (N , , S, P), where the nonterminals and pro-
ductions are described above, we show that generates the same language as
accepted by M.
We п¬Ѓrst show that for p, q в€€ Q, B в€€ I в€Є {О»} and w в€€ Aв€— , that

p, B, q в‡’в€— w if and only if ( p, w, B) в€—
(q, О», О»).

Thus for t в€€ Q, s, О», t в‡’в€— w if and only if (s, w, О») в€— (t, О», О») so that a
word is generated by if and only if it is accepted by M.
First, using induction on the number of derivation steps, we show that if
p, B, q в‡’в€— w then ( p, w, B) в€— (q, О», О»). Beginning with n = 1, the only
possibility is that a nonterminal is popped, without replacement. This can only
occur using productions of type (4), so we have p = q, B = О», and w = О».
But this gives us ( p, О», О») в€— ( p, О», О») which is obvious. Assume n = k > 1,
then the п¬Ѓrst production can only be of type (2) or type (3). If it is type (2),
we have p, B, q в†’ a r, D, q for p, r в€€ Q, where (( p, a, B), (r, D)) в€€ П’.
Hence letting w = av, ( p, w, B) (q, v, D) and by induction if r, D, q в‡’в€— v
then (q, v, D) в€— (q, О», О»). Therefore ( p, w, B) в€— (q, О», О»).
If the п¬Ѓrst production is of type (3), we have

p, B, q в‡’ a q0 , B1 , q1 q1 , B2 , q2 q2 , B3 , q3 . . . qnв€’1 , Bn , q в‡’в€— w
Grammars
158

and (( p, a, B), (q, B1 B2 . . . Bn )) в€€ П’. So if w = av, ( p, w, B) (q, v,
B1 B2 . . . Bn ). For convenience of notation, let q = qn . Let qiв€’1 , Bi , qi в‡’в€— u i
so that w = au 1 u 2 . . . u n and v = u 1 u 2 . . . u n . By induction, (qiв€’1 , u i , Bi ) в€—
(qi , О», О»).
Therefore we have

( p, w, B) (q0 , u 1 u 2 . . . u n , B1 B2 . . . Bn )
в€—
(q1 , u 2 . . . u n , B2 . . . Bn )
в€—
(q2 , u 3 . . . u n , B3 . . . Bn )
. .
. .
. .
в€—
(qnв€’1 , u n , Bn )
(qn , О», О»)

so that ( p, w, B) в€— (q, О», О»).
We now show that if ( p, w, B) в€— (q, О», О») then p, B, q в‡’в€— w. We use
induction on the number of steps in ( p, w, B) в€— (q, О», О»). If there are 0 steps,
then p = q and w = B = О». This corresponds to p, О», p в‡’ О» which is one
of the productions. Therefore the statement is true for 0 steps.
Assume ( p, w, B) в€— (q, О», О») in k + 1 steps. First assume that we have
w = av and
в€—
( p, w, B) (q, v, D) (q, О», О»)

where (( p, a, B), (r, D)) в€€ П’, and B, D в€€ I в€Є {О»}, giving productions p, B,
q в†’ a r, D, q . Since (q, v, D) в€— (q, О», О») by induction hypothesis,
r, D, q в‡’в€— v. Therefore p, B, q в‡’ a r, D, q в‡’в€— av = w and we are
п¬Ѓnished.
Next assume w = av and the п¬Ѓrst step is ( p, w, B) (q, v, B1 B2 . . . Bn ) so
we have
в€—
( p, w, B) (q0 , v, B1 B2 . . . Bn ) (q, О», О»)

and each Bi is eventually removed from the stack in order so that there are states
q1 , q2 , . . . qnв€’1 , qn where qn = q and v = v1 v2 . . . vnв€’1 vn such that

( p, w, B) (q0 , v1 v2 . . . vnв€’1 vn , B1 B2 . . . Bn )
в€—
(q1 , v2 . . . vnв€’1 vn , B2 . . . Bn )
в€—
(q2 , v3 , . . . vnв€’1 vn , B3 . . . Bn )
..
..
..
в€—
(qnв€’1 , vn , Bn )
в€—
(qn , О», О»).
4.3 Pushdown automata and context-free languages 159

By the induction hypothesis, qiв€’1 , Bi , qi в‡’в€— vi .
But since the production is type (3),

в‡’ a q0 , B1 , q1 q1 , B2 , q2 q2 , B3 , q3 . . . qnв€’1 , Bn , q
p, B, q
в‡’в€— av1 q1 , B2 , q2 q2 , B3 , q3 . . . qnв€’1 , Bn , q
в‡’в€— av1 v2 q2 , B3 , q3 . . . qnв€’1 , Bn , q
..
..
..
в‡’в€— av1 v2 . . . vnв€’1 qnв€’1 , Bn , q
в‡’в€— av1 v2 . . . vnв€’1 vn

so that p, B, q в‡’в€— w.

Theorem 4.6 A language is context-free if and only if it is accepted by a PDA.

Exercises
(1) Construct a pushdown automaton which reads the same language as the
grammar = (N , , S, P) deп¬Ѓned by N = {S, A, B}, = {a, b, c}, and
the set of productions P given by

S в†’ aA A в†’ a AB Aв†’a Bв†’b B в†’ О».

(2) Construct a pushdown automaton which reads the same language as
generated by the grammar = (N , , S, P) deп¬Ѓned by N = {S, A, B},
= {a, b, c}, and the set of productions P given by

S в†’ AB A в†’ aba A Aв†’О» B в†’ Bcacc B в†’ О».

(3) Construct a pushdown automaton which reads the same language as
generated by the grammar = (N , , S, P) deп¬Ѓned by N = {S, A, B},
= {a, b, c, }, and the set of productions P given by

S в†’ AcB A в†’ aba A Aв†’О» B в†’ Bcacb B в†’ О».

(4) Construct a pushdown automaton which reads the same language as
generated by the grammar = (N , , S, P) deп¬Ѓned by N = {S, A, B},
= {a, b, c}, and the set of productions P given by

S в†’ AB A в†’ ac A B в†’ bcB B в†’ bB
A в†’ a Aa Bв†’О» A в†’ О».
Grammars
160

(5) Construct a pushdown automaton which reads the same language as
generated by the grammar = (N , , S, P) deп¬Ѓned by N = {S, A, B},
= {a, b, c, d}, and the set of productions P given by

S в†’ AB A в†’ a Ac B в†’ bBc B в†’ bB
A в†’ Aa A Bв†’О» A в†’ О».

(6) Construct a grammar which generates the language read by the pushdown
automaton

Start pop
b
(t)
(s)
b
a
b
a a a
pop
a
(t)
a
b b b pop
push
b
accept

(7) Construct a grammar which generates the language read by the pushdown
automaton

Start

a
a
a
b

b b
a
pop
b
Accept
4.3 Pushdown automata and context-free languages 161

(8) Construct a grammar which generates the language read by the pushdown
automaton

Start

a
a

a

b

pop
a
a
a
Accept

(9) Construct a grammar which generates the language read by the pushdown
automaton

Start
(t)
a a a
push
b b a
b b
b
a
a
b pop
b
a a b
accept accept

(10) Prove Theorem 4.6 вЂњA language is context-free if and only if it is accepted
by a PDA.вЂќ Assume the grammar is in Greibach normal form.
(11) Construct a pushdown automaton that reads the same language as the
grammar = (N , , S, P) deп¬Ѓned by N = {S, B} в€Є , = {a, b, c},
and the set of productions P given by

S в†’ aA A в†’ a AB Aв†’a Bв†’b B в†’ О».
Grammars
162

(12) Construct a pushdown automaton that reads the same language as the
grammar = (N , , S, P) deп¬Ѓned by N = {S, A, B}, = {a, b, c}, and
the set of productions P given by
S в†’ AB A в†’ aba A Aв†’О» B в†’ Bcacc B в†’ О».
(13) Construct a pushdown automaton that reads the same language as the
grammar = (N , П’, S, P) deп¬Ѓned by N = {S, A, B}, = {a, b, c}, and
the set of productions P given by
S в†’ Ad B A в†’ aba A Aв†’О» B в†’ Bcacb B в†’ О».
(14) Construct a pushdown automaton that reads the same language as the
grammar = (N , П’, S, P) deп¬Ѓned by N = {S, A, B}, П’ = {a, b, c}, and
the set of productions P given by
S в†’ AB A в†’ ac A B в†’ bcB B в†’ bB
A в†’ a Aa Bв†’О» A в†’ О».
(15) Construct a pushdown automaton that reads the same language as the
grammar = (N , П’, S, P) deп¬Ѓned by N = {S, A, B}, П’ = {a, b, c, d},
and the set of productions P given by
S в†’ AB A в†’ a Ac B в†’ bBc B в†’ bB
A в†’ Aa A Bв†’О» A в†’ О».

4.4 The Pumping Lemma and decidability
Just as we were able to show that there are languages that are not regular
languages, we are also able to show that there are languages that are not context-
free. We begin by returning to the concept of the parse tree or derivation tree.
The height of the tree is the length of the longest path in the tree. The level of
a vertex A in the tree is the length of the path from the vertex S to the vertex A.
A tree is a binary tree if each vertex has at most two children. Note that if the
grammar is in Chomsky normal form then every tree formed is a binary tree.
Lemma 4.13 If A в‡’в€— w where A is a nonterminal and the height of the
corresponding derivation tree with root A is n, then the length of w is less than
or equal to 2nв€’1 .
Proof We use induction on the height of the derivation tree. If n = 1, then the
derivation has the form A в†’ a, and the length of w = a is 1 = 20 . Assume the
lemma is true when n = k, and let A в‡’в€— w have a derivation tree of height k +
1. Then A в‡’ BC в‡’в€— uv = w, where B в‡’в€— u and C в‡’в€— v and the derivation
4.4 The Pumping Lemma and decidability 163

tree for both of these derivations has height n. Therefore both u and v have length
less than or equal to 2kв€’1 and w has length less than or equal to 2 В· 2kв€’1 = 2k =
2(k+1)в€’1 .
Previously we had a pumping theorem for regular languages. We now have
one for context-free languages.
Theorem 4.7 (Pumping Lemma) Let L be a context-free language. There
exists an integer M so that any word longer than M in L has the form xuvwy
where uw is not the empty word, the word uvw has length less than or equal
to M, and xu n wv n y в€€ L for all n в‰Ґ 0.
Proof Let L в€’ {О»} be a nonempty language generated by the grammar
= (N , , S, P) in Chomsky normal form with p productions. Let M = 2 p .
Assume there is a word w in L with length greater than or equal to M. Then
by the previous theorem, the derivation tree has height greater than p. There-
fore there is a path S в†’ В· В· В· в†’ a where a is a letter in the derivation tree with
length greater than p and a is a letter of w. Since there are only p productions,
some nonterminal occurs more than once on the left-hand side of a production.
Let C be the п¬Ѓrst nonterminal to occur the second time. Therefore we have a
derivation
S в‡’в€— О±CОІ в‡’в€— xuCvy в‡’в€— xuwvy
where О± в‡’в€— x, ОІ в‡’в€— y, C в‡’в€— uCv and C в‡’ w. But using these derivations,
we can form the derivation
S в‡’ xuCvy в‡’в€— xuuCvvy в‡’в€— xu n wv n y for any positive integer n.
Since the п¬Ѓrst production in the derivation has the form C в‡’ AB в‡’в€— uCv,
and there are no empty words, either u or v is not the empty word. Pick a letter
a in uwv; we can work our way back to S using one occurrence of each of the
productions C в‡’в€— uCv and C в‡’ w. Hence the length of the path is at most p,
and the length of uwv is less than or equal to M.
We are now able to п¬Ѓnd a language which is not a context-free language.
Corollary 4.1 The language L = {a m bm a m : m в‰Ґ 1} is not a context-free
language.
Proof Assume m is large enough so that the length of a m bm a m is larger than
M. Therefore a m bm a m = puqvr where pu n qv n r в€€ L for all n в‰Ґ 1. If either u
or v contains both a and b, for example assume u = a i b j , then (a i b j )n must be
a substring of pu n qv n r which is clearly impossible. Thus u and v each consist
entirely of strings of as or entirely of strings of bs. They cannot both be strings
Grammars
164

of as or strings of bs since the number of occurrences of the common letter in
these strings could continue as n increases but the number of occurrences of the
other letter would not increase. Therefore u must be a string of as to begin each
word a m bm a m and v must be a string of as to end each word a m bm a m which is

Example 4.19 The language L = {a i b j ci d j } : i, j в‰Ґ 1} is not a context-free
language.

Proof Let M and xu n wv n y be the same as in the previous theorem. Therefore
|uwv| в‰¤ m. Consider a m bm cm d m в€€ L. Since |uwv| в‰¤ m it must be contained
in a power of one of the letters a, b, c, or d or in the powers of two adjacent
letters. If it is contained in the power of one letter, say a, then there are fewer
as than cs in each word in M, which is a contradiction. If it is contained in a
power of two adjacent letters, say a and b, then there are fewer as than cs in
each word in M, which is a contradiction.

Example 4.20 The language L = {ww : w в€€ {a, b}в€— } is not context-free. We
shall see in Theorem 4.10 that the intersection of a regular language and a
context-free language is context-free. But L в€© a в€— bв€— a в€— bв€— ={a i b j a i b j } is not
context-free. The argument is the same as in the previous example.

Example 4.21 The language L = {x : the length of x is a prime} is not
context-free. Since primes are arbitrarily large some element w with length m of
L must have the form xu n wv n y for n в‰Ґ 2. Let m = |xwy|. Then |uv| = n в€’ w
and |xu m wv m y| = m + m(n в€’ w) which is not a prime.

We have previously seen that the set of regular languages is closed under
the operations concatenation, union, Kleene star, intersection and complement.
We now explore these same operations for the set of context-free languages.

Theorem 4.8 The set of context-free languages is closed under the operations
of concatenation, union, and Kleene star.

Proof Let L 1 and L 2 be generated by grammars 1 = (N1 , 1 , S1 , P1 ) and
2 = (N2 , 2 , S2 , P2 ) respectively. Assume N1 and N2 are disjoint. This can
always be accomplished by relabeling the elements in either N1 or N2 .
The language L 1 L 2 can be generated by the grammar = (N , , S, P)
where N = N1 в€Є N2 в€Є {S}, = 1 в€Є 2 , and P = P1 в€Є P2 в€Є {S в†’ S1 S2 }. If
u в€€ L 1 and v в€€ L 2 , then S1 в‡’в€— u, in 1 , S2 в‡’в€— v in 2 , and using leftmost
derivation, we have S в‡’ S1 S2 в‡’в€— u S2 в‡’в€— uv in .
The language L 1 в€Є L 2 can be generated by the grammar = (N , , S, P)
where N = N1 в€Є N2 в€Є {S}, = 1 в€Є 2 , and P = P1 в€Є P2 в€Є {S в†’ S1 , S в†’
4.4 The Pumping Lemma and decidability 165

S2 }. Let w в€€ L 1 в€Є L 2 . Therefore w в€€ L 1 or w в€€ L 2 . If w в€€ L 1 , then S1 в‡’в€— w in
в€— в€— в€—
1 , and S в‡’ S1 в‡’ w in . If w в€€ L 2 , then S2 в‡’ w in 2 , and S в‡’ S2 в‡’ w
in .
The language L в€— can be generated by the grammar = (N , , S, P)
1
where N = N1 в€Є {S}, = 1 and P = P1 в€Є P2 в€Є {S в†’ S1 S, S в†’ О»}. Let
w1 , w2 , w3 , . . . , wn в€€ L 1 . Using productions S в†’ S1 S and S в†’ О», we can
form the derivation S в‡’в€— S1 = S1 S1 S1 В· В· В· S1 . Using leftmost derivations
n

we can derive S в‡’в€— S1 S1 S1 В· В· В· S1 в‡’в€— w1 S1 S1 В· В· В· S1 в‡’в€— w1 w2 S1 В· В· В· S1 в‡’в€—
w1 w2 В· В· В· wn in . Hence L в€— = L, the language generated by .
1

Theorem 4.9 The set of context-free languages is not closed under the oper-
ations of intersection and complement.

Proof The sets {a n bn cm : m, n в‰Ґ 0} and {a n bm cm : m, n в‰Ґ 0} are context-
free. The п¬Ѓrst is generated by the grammar with productions

P = {S в†’ BC, B в†’ a Bb, B в†’ О», C в†’ cC, C в†’ О»}.

The second is generated by the grammar with productions

P = {S в†’ AB, A в†’ a A, A в†’ О», B в†’ bBc, B в†’ О»}.

However, the intersection is the language L = {a m bm a m : m в‰Ґ 0}, which we
have shown is not context-free.
If the set of context-free languages is closed under complement then since
L 1 в€© L 2 = (L 1 в€Є L 2 ) , the set of context-free languages is closed under inter-
section which we have already shown is not true.

Although the intersection of context-free languages is not necessarily a
context-free language, the intersection of a context-free language and a reg-
ular language is a context-free language. The proof is somewhat similar to the
one showing that the union of languages accepted by an automaton is accepted
by an automaton.
Theorem 4.10 The intersection of a regular language and a context-free
language is context-free.

Proof Let the pushdown automaton M = ( , Q, s, I, П’, F) where is the
alphabet, Q is the set of states, s is the initial or starting state, I is the set of stack
symbols, F is the set of acceptance states, and П’ is the transition relation where
the relation П’ is a п¬Ѓnite subset of ((Q Г— в€— Г— I в€— ) Г— (Q Г— I в€— )). Let the deter-
ministic п¬Ѓnite automaton M1 = ( 1 , Q 1 , q0 , П’1 , F1 ) where 1 is the alphabet,
Q 1 is the set of states, q0 is the initial or starting state, F1 is the set of acceptance
states, and П’1 is the transition function. We now deп¬Ѓne the pushdown automaton
Grammars
166

M2 = ( 2 , Q 2 , s2 , I2 , П’, F2 ) where s2 = (s, q0 ), 2 = 1 в€Є , I2 = I, Q 2 =
Q Г— Q 1 , and F2 = F Г— F1 . Deп¬Ѓne П’2 by (((si , q j ), a, X ), ((sm , qn ), b)) в€€ П’2
if and only if ((si , a, X ), (sm , b)) in M and П’1 (q j , u) = qn in M1 . A word is
w accepted in M2 if and only if ((s, q0 ), w, О») в€— ((sa , qb ), О») in M2 , where sa
2
and qb are acceptance states in M and M1 respectively. Thus w is accepted by
the pushdown automaton M and also accepted by M1 .
To show M2 (L) = M(L) в€© M1 (L), the reader is asked to п¬Ѓrst show that
((s, q0 ), w, О») в€— ((sm , qn ), О», О±) if and only if (s, w, О») в€— (sm , О», О±) and
(q0 , w) в€— (qn , О»), using induction on the number of operations in в€— . The
1 2
theorem immediately follows.

Deп¬Ѓnition 4.10 A nonterminal in a context-free grammar is useless if it does
not occur in any derivation S в‡’в€— w, for w в€€ в€— . If a nonterminal is not useless,
then it is useful.

Theorem 4.11 Given a context-free grammar, it is possible to п¬Ѓnd and remove
all productions with useless nonterminals.

Proof We п¬Ѓrst remove any nonterminal U so that there is no derivation U
в‡’в€— w , for w в€€ в€— . To п¬Ѓnd such nonterminals, let X be deп¬Ѓned as follows:
(1) For each nonterminal V such that V в†’ w is a production for w в€€ в€— , let
V в€€ X . (2) If V в†’ V1 V2 В· В· В· Vn where Vi в€€ X or в€— for 1 в‰¤ i в‰¤ n, let V в€€ X .
Continue step (2) until no new nonterminals are added to X . Any nonterminal
U not in X has no derivation U в‡’в€— w. If S is not in X , then the language
generated by the context-free grammar is empty and we are done. There are no
useful nonterminals. Assume S is in X . All productions containing nonterminals
not in X are removed from the set of productions P.
Assume such productions have been removed. We now have to remove any
nonterminal U which is not reachable by S, i.e. there is no production S в‡’в€— W
where U is in the string W . To test each nonterminal U we form a set YU as
follows: (1) If V в†’ W and U is in the string W , then V в€€ YU . (2) if R в‡’ T ,
where an element of YU is in the string T , then R в€€ YU . Continue step (2)
until no new nonterminals are added to YU . If S в€€ YU , then U is reachable
by S. If not, U is not reachable by S. Remove all productions which contain
nonterminals not reachable by S. The context-free grammar created contains
no useless nonterminals.

Theorem 4.12 It is possible to determine whether a context-free language L
is empty.

Proof A context-free language L is empty if and only if it contains no useful
nonterminals.
4.4 The Pumping Lemma and decidability 167

Theorem 4.13 Given w в€€ в€— , and a context-free grammar G, it is possible
to determine whether w is in the language generated by G.

Proof Assume that G is in Chomsky normal form. Let w be a word of length
n в‰Ґ 1. Each step of the derived string becomes longer except when the nonter-
minal is replaced by a terminal. Therefore the derivation S в‡’в€— w has length k
в‰¤ 2nв€’1 since G is in Chomsky normal form. Therefore check all derivations of
length k в‰¤ 2nв€’1 .

The above proof shows that it is possible to determine whether a word is in
the language generated by a grammar; it really is not a practical way.

Theorem 4.14 Let G be a context-free grammar in Chomsky normal form
with exactly p productions. The language L(G) is inп¬Ѓnite if and only if there
exists a word П‰ in L(G) such that 2 p < |П‰| < 2 p+1 .

Proof If there is a word with length greater than 2 p then by the proof of the
Pumping Lemma, L(G) is inп¬Ѓnite. Conversely, let П‰ be the shortest word with
length greater than 2 p+1 . By the Pumping Lemma, П‰ = xu i wv i y, where the
length of uvw в‰¤ 2 p and Ој = xu iв€’1 wv iв€’1 y is in L(G). But |Ој| > |П‰| в€’ |uv| в‰Ґ
2 p . Also |Ој| < |П‰| and w is the shortest word with length greater than or equal
to 2 p+1 . Therefore |Ој|. < 2 p+1 .

Theorem 4.15 It is possible to determine whether a language generated by
a context-free grammar is п¬Ѓnite or inп¬Ѓnite.

Proof Since it is possible to determine whether a word is in the language of
a context-free grammar, simply try all words with length between 2 p and 2 p+1
to see if one of them is in the context-free grammar. If one is, the grammar is
inп¬Ѓnite. If not the grammar is п¬Ѓnite.

Exercises
= (N , П’, S, P) be deп¬Ѓned by N = {S, A, B}, П’ =
(1) Let grammar
{a, b, c}, and the set of productions P given by

S в†’ AB A в†’ ac A B в†’ bcB B в†’ bB
A в†’ a Ba Bв†’О» A в†’ a.

Let L be the language generated by . Find the grammar that generates L в€— .
(2) Let L 1 be the language generated by the grammar 1 = (N , , S, P)
deп¬Ѓned by N = {S, A, B}, = {a, b, c}, and the set of productions P
Grammars
168

given by
S в†’ aA A в†’ a AB Aв†’a Bв†’b B в†’ О»,
and L 2 be the language generated by the grammar 2 = (N , , S, P)
deп¬Ѓned by N = {S, A, B}, = {a, b, c}, and the set of productions P
given by
S в†’ AB A в†’ aba A Aв†’О» B в†’ Bcacc B в†’ О».
Find the grammar that generates L 1 L 2 .
(3) Let L 1 be the language generated by the grammar 1 = (N , П’, S, P)
deп¬Ѓned by N = {S, A, B}, = {a, b, c}, and the set of productions P
given by
S в†’ Ad B A в†’ aba A Aв†’О» B в†’ Bcacb B в†’ О»,
and L 2 be the language generated by the grammar 2 = (N , П’, S, P)
deп¬Ѓned by N = {S, A, B}, П’ = {a, b, c}, and the set of productions P
given by
S в†’ AB A в†’ ac A B в†’ bcB B в†’ bB
A в†’ a Aa Bв†’О» A в†’ О».
Find the grammar that generates L 1 в€Є L 2 .
Determine whether the following languages are context-free. If the lan-
guage is context-free, construct a grammar that generates it. If it is not
context-free, prove that it is not.
L = {a m bn c2n : m, n = 1, 2, . . . }.
(4)
L = {ww R w : w в€€ {a, b}в€— }.
(5)
L = {w в€€ {a, b, c}в€— } : w has an equal number of as and bs }.
(6)
L = {a n b2n cn : n = 1, 2, . . . }.
(7)
(8) Prove the induction step in Theorem 4.10.
5
Turing machines

5.1 Deterministic Turing machines
The Turing machine is certainly the most powerful of the machines that we
have considered and, in a sense, is the most powerful machine that we can
consider. It is believed that every well-deп¬Ѓned algorithm that people can be
taught to perform or that can be performed by any computer can be performed
on a Turing machine. This is essentially the statement made by Alonzo Church
in 1936 and is known as ChurchвЂ™s Thesis. This is not a theorem. It has not been
mathematically proven. However, no one has found any reason for doubting it.
It is interesting that although the computer, as we know it, had not yet been
invented when the Turing machine was created, the Turing machine contains
the theory on which computers are based. Many students have been amazed to
п¬Ѓnd that, using a Turing machine, they are actually writing computer programs.
Thus computer programs preceded the computer.
We warn the reader in advance that if they look at different books on Turing
machines, they will п¬Ѓnd the descriptions to be quite different. One author will
state a certain property to be required of their machine. Another author will
strictly prohibit the same property on their machine. Nevertheless, the machines,
although different, have the same capabilities.
The Turing machine has an input alphabet , a set of tape symbols,
 << стр. 6(всего 9)СОДЕРЖАНИЕ >>