CS402 Quiz Solved and Reference-1

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 140

1.

Between two consecutive joints on a path, ___________ is/are popped and ___________
can be pushed
Exactly one character, Any number of characters (388)
Exactly one character, Exactly one character
Any number of characters, Exactly one character
Any number of characters, Any number of characters

2. START, READ, HERE, and ACCEPT states are called ___________ of machine
items
plugs

om
joints (388)
interface

3. ___________ states are called the halt states


ACCEPT and REJECT (335)

.c
ACCEPT and READ
ACCEPT AND START

ns
ACCEPT AND WRITE

4. Identify the correct statement from the following:


tio
There always exists a corresponding PDA for every CFG
There does not exist a corresponding PDA for every CFG
For a CFG to convert to PDA, the CFG should have at least 3 Nonterminals
ca
For a CFG to convert to PDA, the CFG should have at least 5 Nonterminals

5. In conversion form of PDA, no two ___________ states exist in a row without ___________
du

state
PUSH, READ
POP, READ (382) ??
PUSH, START
-e

POP, REJECT

6. In new format of FA (discussed in lecture 37), ___________ state is like initial state of an FA
xt

ACCEPT
REJECT
ne

START
READ

7. A PDA is called nondeterministic PDA if ___________


there are more than one outgoing edges at READ or POP states with one label
there are more than one PUSH states
there are more than one POP states
every READ state is followed by a HERE state

8. The PDA is called non-deterministic PDA when there are more than one out going edges
from ___________ state
START or READ
POP or REJECT
READ or POP
PUSH or POP

9. Which of the following states is not part of PDA?


START
ACCEPT
WRITE ???
REJECT

om
10. Before the CFG corresponding to the given PDA is determined, the PDA is converted into
the standard form which is called
Finite Automation
Chomsky Normal Form (CNF)

.c
Conversion form
Non of given options

ns
11. A PDA consists of the following:
An alphabet (Sigma) of input letters
tio
An input TAPE with infinite many locations in one direction
One START state with only one out-edge and no in-edge
All of the given options
ca

12. ___________ states are called the halt states


ACCEPT and REJECT
du

ACCEPT and READ


ACCEPT AND START
ACCEPT AND WRITE
-e

13. Given a PDA that accepts the language L ___________


There exists a CFG that generates exactly L
There does not exist any CFG that generates exactly L
xt

that PDA will also accept Language L* (complement of L)


None of given options
ne

14. In conversion form of PDA no two ___________states exist in a row without ___________
state
PUSH, READ
POP, READ
PUSH, START
POP, REJECT

15. Before the PDA is converted into conversion form, a new state ___________ is defined
which is placed in the middle of any edge
REJECT
HERE
START
STOP

16. A CFG must be converted into ___________ form for the development of corresponding
PDA
derivation
conversion ??
descriptive
recursive

om
17. In new format of an FA (discussed in lecture 37), ___________ state is like a final state of
an FA
ACCEPT
REJECT

.c
START
READ

ns
18. Like TG, the ___________ can be considered as a PDA in conversion form
set of joints
set of Forks
tio
set of Plugs
set of blocks
ca

19. Which path sequence follows the rules of “conversion form” of PDA?
READ → POP → POP
POP → POP → POP
du

READ → POP → PUSH a


HERE → PUSH a → PUSH a
-e

20. A(n) ___________ operator adds a new letter at the top of STACK
PUSH
POP
xt

READ
APPEND
ne

21. Identify the TRUE statement


A PDA is non-deterministic, if there are more than one READ states in PDA
A PDA is never non-deterministic
Like TG, A PDA can also be non-deterministic ??
A PDA is non-deterministic, if there are more than one REJECT states in PDA

22. Tape and Stack alphabets


are same
may be different
must be same
must be different

23. A PDA consists of the following:


An alphabet (Sigma) of input letters
An input TAPE with infinite many locations in one direction
One START state with only one out-edge and no in-edge
All of the given options

24. Identify the TRUE statement about following CFG


S → SB|AB
A → CC

om
B→b
C→a
The given CFG has 8 Nonterminals
The given CFG has 8 Terminals

.c
The given CFG is in CNF
The given CFG is not in CNF

ns
25. In conversion form of PDA, there is ___________ accept state(s)
More than One
At least one
tio
Exactly one
At most one
ca

26. Identify the correct statement from the following


There always exists a corresponding PDA for every CFG
du

There does not exist a corresponding PDA for every CFG


For a CFG to convert to PDA, the CFG should have at least 3 Nonterminals
For a CFG to convert to PDA, the CFG should have at least 5 Nonterminals
-e

27. Before running the input string on PDA, it is first placed on ___________
Stack
Memory
xt

Tape
Ram
ne

28. PDA stands for ___________


Push and Drop Automation
Pop and Drop Automation
Push Down Automation
Push Deterministic Automation

29. Which of the following are called as Halt states in PDA?


Start and Accept
Accept and Reject
Start and Reject
Read and Reject

30. Between the two consecutive joints on a path


One character can be pushed and one character can be popped
Any no. of characters can be pushed and one character can be popped
One character can be pushed and any no of chapters can be popped
Any no. of characters can be pushed and any no. of characters can be popped

31. The part of an FA, where the input string is placed before it is run, is called ___________.
State

om
Transition
Input Tape
Output Tape

32. A PDA is in conversion form if it fulfills the following condition:

.c
There is only one ACCEPT state
There are more than one ACCEPT states

ns
There is only one REJECT state
There are more than one REJECT states tio
33. ___________ is an operation that takes out a letter from the top of the STACK
WRITE
PUSH
ca
POP
APPEND
du

34. A ___________ is the one for which every input string has a unique path through the
machine.
deterministic PDA
nondeterministic PDA
-e

PUSHDOWN store
Input Tape
xt

35. Choice of path can be determined by left most derivation of the string belonging to CFL
at ___________ state.
ne

ACCEPT
REJECT
PUSH
POP

36. In conversion form of PDA, there is no ___________ state.


PUSH
READ
ACCEPT
REJECT
37. Identify the FALSE statement about following CFG:
S → SB|AB
A → CC
B→b
C→a
CFG generates NULL string ?? (115 pdf)
CFG is not in CNF
CFG has 8 Nonterminals
ALL of the given options

om
38. The locations into which we put the input letters on “Input Tape” are called ___________
words
alphabets
cells
elements

.c
39. In the conversion form of PDA, a path must be ___________ to ___________

ns
consistent.
Joint, stack
joint, joint
stack, stack
tio
CFG, PDA
ca
40. In conversion form of PDA, there is no ___________ state.
PUSH
READ
du

ACCEPT
REJECT

41. Left hand Side of Production in CFG may consist of ___________ ????
-e

Exactly one Nonterminal


More than one Nonterminals
Exactly one Terminals
xt

More than one Terminals


ne

42. In nondeterministic PDA, a string is supposed to be accepted if there exists at least one
path traced by the string, leading to ___________ state.
ACCEPT
REJECT
START

43. Before running the input string on PDA, it is first placed on ___________
Stack
Memory
Tape
44. In new format of an FA (discussed in lecture 37), ___________ state is like dead-end non
final state.
ACCEPT
REJECT
START

45. Left hand Side of a Production in CFG may consist of ___________


Exactly one Nonterminal
More than one Nonterminals
Exactly one Terminals

om
More than one Terminals

46. In the conversion form of PDA, a path must be ___________ to ___________ consistent
joint, stack
joint, joint

.c
stack, stack
CFG, PDA

ns
tio
ca
du
-e
xt
ne
Theory of Automata (CS402)

Theory of Automata

Lecture N0. 34
Reading Material

Introduction to Computer Theory Chapter 12,13

Summary
Example of Ambiguous Grammar, Example of Unambiguous Grammer (PALINDROME), Total Language tree
with examples (Finite and infinite trees), Regular Grammar, FA to CFG, Semi word and Word, Theorem,
Defining Regular Grammar, Method to build TG for Regular Grammar

Example

om
Consider the following CFG
S → aS | bS | aaS | Λ
It can be observed that the word aaa can be derived from more than one production trees. Thus, the above CFG
is ambiguous. This ambiguity can be removed by removing the production S → aaS

.c
Example
Consider the CFG of the language PALINDROME
S→aSa|bSb|a|b|Λ
It may be noted that this CFG is unambiguous as all the words of the language PALINDROME can only be

ns
generated by a unique production tree.
It may be noted that if the production S → aaSaa is added to the given CFG, the CFG thus obtained will be no
more unambiguous.

Total language tree


tio
For a given CFG, a tree with the start symbol S as its root and whose nodes are working strings of terminals and
non'terminals. The descendants of each node are all possible results of applying every production to the working
string. This tree is called total language tree.
ca

Example
Consider the following CFG
S → aa|bX|aXX
X → ab|b
du

then the total language tree for the given CFG may be
S
-e

aa aXX
bX
aXb
xt

abX abb
bab bb
aabb
aabX aXab
ne

abab abb

aabab aabb aabab abab

It may be observed from the above total language tree that dropping the repeated words, the language generated
by the given CFG is {aa, bab, bb, aabab, aabb, abab, abb}

Example
Consider the following CFG
S → X|b, X → aX
then following will be the total language tree of the above CFG

96
© Copyright Virtual University of Pakistan
Theory of Automata (CS402)

X b

aX
Note: It is to be
noted that the aaX
only word in
this language ∂

om
is b.

aaa …aX
Regular Grammar
All regular languages can be generated by CFGs. Some nonregular languages can be generated by CFGs but not

.c
all possible languages can be generated by CFG, the CFG S → aSb|ab generates the language
{anbn:n=1,2,3, …}, which is nonregular.

ns
Note: It is to be noted that for every FA, there exists a CFG that generates the language accepted by this FA.

Example
Consider the language L expressed by (a+b)*aa(a+b)* the language of strings, defined over ∑ ={a,b},
tio
containing aa. To construct the CFG corresponding to L, consider the FA accepting L, as follows
b a a,b

S) a B+
A
ca

b
CFG corresponding to the above FA may be
S → bS|aA
du

A → aB|bS
B → aB|bB|Λ
It may be noted that the number of terminals in above CFG is equal to the number of states of corresponding FA
where the nonterminal S corresponds to the initial state and each transition defines a production.
-e

Semiword
A semiword is a string of terminals (may be none) concatenated with exactly one nonterminal on the right a
semiword, in general, is of the following form
xt

(terminal)(terminal)… (terminal)(nonterminal)

word
ne

A word is a string of terminals. Λ is also a word.

Theorem
If every production in a CFG is one of the following forms
Nonterminal → semiword
Nonterminal → word
then the language generated by that CFG is regular.

Regular grammar
Definition
A CFG is said to be a regular grammar if it generates the regular language a CFG is said to be a regular
grammar in which each production is one of the two forms
Nonterminal → semiword

97
© Copyright Virtual University of Pakistan
Theory of Automata (CS402)

Nonterminal → word

Examples
The CFG S → aaS|bbS|Λ is a regular grammar. It may be observed that the above CFG generates the language
of strings expressed by the RE (aa+bb)*.
The CFG S → aA|bB, A → aS|a, B → bS|b is a regular grammar. It may be observed that the above CFG
generates the language of strings expressed by RE (aa+bb)+.

Following is a method of building TG corresponding to the regular grammar.

TG for Regular Grammar


For every regular grammar there exists a TG corresponding to the regular grammar.
Following is the method to build a TG from the given regular grammar

om
Define the states, of the required TG, equal in number to that of nonterminals of the given regular grammar. An
additional state is also defined to be the final state. The initial state should correspond to the nonterminal S.
For every production of the given regular grammar, there are two possibilities for the transitions of the required
TG

If the production is of the form nonterminal → semiword, then transition of the required TG would start from

.c
the state corresponding to the nonterminal on the left side of the production and would end in the state
corresponding to the nonterminal on the right side of the production, labeled by string of terminals in semiword.
If the production is of the form nonterminal → word, then transition of the TG would start from the state

ns
corresponding to nonterminal on the left side of the production and would end on the final state of the TG,
labeled by the word.

Example
Consider the following CFG
tio
S → aaS|bbS| Λ
The TG accepting the language generated by the above CFG is given below
aa
ca

bb Λ +
S'
du

The corresponding RE may be (aa+bb)*.


-e
xt
ne

98
© Copyright Virtual University of Pakistan
Theory of Automata (CS402)

Theory of Automata

Lecture N0. 35
Reading Material

Introduction to Computer Theory Chapter 13

Summary

Examples of building TG’s corresponding to the Regular Grammar, Null productions with examples, Nullable
productions with examples, Unit production with example, Chomsky Normal Form (Definition)

Example

om
Consider the following CFG
S → aA|bB
A → aS|a
B → bS|b A
then the corresponding TG will be a a

.c
a
S' +
b

ns
b
b
B
The corresponding RE may be (aa+bb)+.

Example
tio
Consider the following CFG
S → aaS|bbS|abX|baX|Λ
X → aaX|bbX|abS|baS, aa,bb aa,bb
ca
then the corresponding TG will be
ab,ba
Λ
+ S' X
ab,ba
du

The corresponding language is EVEN'EVEN.

Null Production
Definition
-e

The production of the form nonterminal → Λ is said to be .

Example: Consider the CFG, S → aA|bB|Λ, A → aa|Λ, B → aS


Here S → Λ and A → Λ are null productions.
xt

Note
ne

If a CFG has a null production, then it is possible to construct another CFG without null production accepting
the same language with the exception of the word Λ if the language contains the word Λ then the new
language cannot have the word Λ.

Following is a method to construct a CFG without null production for a given CFG

Method
Delete all the Null productions and add new productions
consider the productions of a certain CFG X → aNbNa, N → Λ, delete the production N → Λ and using the
production X → aNbNa, add the new productions X → aNba, X → abNa and X → aba
Thus the new CFG will contain the productions X → aNba|abNa|aba|aNbNa
It is to be noted that X → aNbNa will still be included in the new CFG.

Nullable Production
99
© Copyright Virtual University of Pakistan
Theory of Automata (CS402)

Definition
A production is called if it is of the form N → Λ
or
there is a derivation that starts at N and leads to Λ N1 → N2, N2 → N3, N3 → N4, …, Nn →Λ, where N, N1,
N2, …, Nn are non terminals.

Example
Consider the following CFG
S → AA|bB, A → aa|B, B → aS | Λ
Here S → AA and A → B are nullable productions, while B → Λ is null a production.
Following is an example describing the method to convert the given CFG containing null productions and
nullable productions into the one without null productions

om
Example
Consider the following CFG
S → XaY|YY|aX|ZYX
X → Za|bZ|ZZ|Yb
Y → Ya|XY|Λ
Z → aX|YYY

.c
It is to be noted that in the given CFG, the productions S → YY, X → ZZ, Z → YYY are Nullable productions,
while Y → Λ is Null production.

ns
Here the method of removing null productions, as discussed earlier, will be used along with replacing
nonterminals corresponding to nullable productions like nonterminals for null productions are replaced.
Thus the required CFG will be
S →XaY|Xa|aY|a|YY|Y|aX|ZYX|YX|ZX|ZY| X|Z
X → Za|a|bZ|b|ZZ|Z|Yb
Y → Ya|a|XY|X|Y
tio
Z → aX|a|YYY|YY|Y

Example
ca
Consider the following CFG
S → XY, X → Zb, Y → bW
Z → AB, W → Z, A → aA|bA|Λ
B →Ba|Bb|Λ.
du

Here A → Λ and B → Λ are null productions, while Z → AB, W → Z are nullable productions. The new CFG
after, applying the method, will be
S → XY
X → Zb|b
-e

Y → bW|b
Z → AB|A|B
W→Z
A → aA|a|bA|b
xt

B →Ba|a|Bb|b

Note
ne

While adding new productions all Nullable productions should be handled with care. All Nullable productions
will be used to add new productions, but only the Null production will be deleted.

Unit production
The productions of the form nonterminal → one nonterminal, is called the .
Following is an example showing how

Example
Consider the following CFG
S → A|bb
A → B|b
B → S|a
Separate the unit productions from the nonunit productions as shown below
100
© Copyright Virtual University of Pakistan
Theory of Automata (CS402)

unit prods. nonunit prods.


S→A S → bb
A→B A→b
B→S B→a

S → A gives S → b (using A → b)
S → A → B gives S → a (using B → a)
A → B gives A → a (using B → a)
A → B → S gives A → bb (using S → bb)
B → S gives B → bb (using S → bb)
B → S → A gives B → b (using A → b)

Thus the new CFG will be

om
S → a|b|bb, A → a|b|bb, B → a|b|bb.
Which generates the finite language {a,b,bb}.

Chomsky Normal Form


If a CFG has only productions of the form

.c
nonterminal → string of two nonterminals
or
nonterminal → one terminal

ns
then the CFG is said to be in Chomsky Normal Form (CNF).

tio
ca
du
-e
xt
ne

101
© Copyright Virtual University of Pakistan
Theory of Automata (CS402)

Theory of Automata

Lecture N0. 36
Reading Material

Introduction to Computer Theory Chapter 13,14

Summary
Chomsky Normal Form, Theorem regarding CNF, examples of converting CFG to be in CNF, Example of an
FA corresponding to Regular CFG, Left most and Right most derivations, New format of FAs.

Chomsky Normal Form (CNF)


If a CFG has only productions of the form

om
nonterminal → string of two nonterminals
or
nonterminal → one terminal
then the CFG is said to be in Chomsky Normal Form (CNF).

.c
Note
It is to be noted that any CFG can be converted to be in CNF, if the null productions and unit productions are
removed. Also if a CFG contains nullable productions as well, then the corresponding new production are also
to be added. Which leads the following theorem

ns
Theorem
All NONNULL words of the CFL can be generated by the corresponding CFG which is in CNF the
grammar in CNF will generate the same language except the null string.
tio
Following is an example showing that a CFG in CNF generates all nonnull words of corresponding CFL.

Example
ca
Consider the following CFG
S → aSa|bSb|a|b|aa|bb
To convert the above CFG to be in CNF, introduce the new productions as
A → a, B → b, then the new CFG will be
S → ASA|BSB|AA|BB|a|b
du

A→a
B→b
Introduce nonterminals R1 and R2 so that
S → AR1|BR2|AA|BB|a|b
-e

R1 → SA
R2 → SB
A→a
xt

B→b
which is in CNF.
It may be observed that the above CFG which is in CNF generates the NONNULLPALINDROME, which does
ne

not contain the null string.

Example
Consider the following CFG
S → ABAB
A → a|Λ
B → b|Λ
Here S → ABAB is nullable production and A → Λ, B → Λ are null productions. Removing the null
productions
A → Λ and B → Λ, and introducing the new productions as
S → BAB|AAB|ABB|ABA|AA|AB|BA|BB|A|B

Now S → A|B are unit productions to be eliminated as shown below

102
© Copyright Virtual University of Pakistan
Theory of Automata (CS402)

S → A gives S → a (using A → a)
S → B gives S → b (using B → b)
Thus the new resultant CFG takes the form
S → BAB|AAB|ABB|ABA|AA|AB|BA|BB|a|b, A → a, B → b.
Introduce the nonterminal C where C → AB, so that
S → BAB|AAB|ABB|ABA|AA|AB|BA|BB|a|b

S → BC|AC|CB|CA|AA|C|BA|BB|a|b
A→a
B→b
C → AB
is the CFG in CNF.

om
Example
To construct an FA that accepts the grammar
S→abA
A→baB
B→aA|bb

.c
The language can be identified by the three words generated as follows
S ⇒ abA
⇒ abbaB (using A→baB)

ns
⇒ abba bb (using B→bb)
S ⇒ abA
⇒ abbaB (using A→baB)
⇒ abbaaA (using B→ aA)
⇒ abbaabaB
tio
(using A→ baB)
⇒ abbaababb (using B→ bb)
S ⇒ abA
⇒ abbaB (using A→baB)
ca
⇒ abbaaA (using B→ aA)
⇒ abbaabaB (using A→ baB)
⇒ abbaabaaA (using B→ aA)
⇒ abbaabaabaB (using A→ baB)
du

⇒ abbaabaababb (using B→ bb)


which shows that corresponding language has RE abba(aba)*bb. Thus the FA accepting the given CFG may be
-e

a
xt

S' A B +
a b b a b b
ne

a a b
b a a,b

Left most derivation


Definition a,b
The derivation of a word w, generated by a CFG, such that at each step, a production is applied to the left most
nonterminal in the working string, is said to be .
It is to be noted that the nonterminal that occurs first from the left in the working string, is said to be
.

Example
Consider the following CFG

103
© Copyright Virtual University of Pakistan
Theory of Automata (CS402)

S→XY
X → XX|a
Y→YY|b
then following are the two left most derivations of aaabb

S ⇒ XY S ⇒ XY
⇒ XXY ⇒ XXY
⇒ aXY ⇒ XXXY
⇒ aXXY ⇒ aXXY
⇒ aaXY ⇒ aaXY
⇒ aaaY ⇒ aaaY
⇒ aaaYY ⇒ aaaYY

om
⇒ aaabY ⇒ aaabY
= aaabb = aaabb

Theorem
Any word that can be generated by a certain CFG has also a left most derivation.

.c
It is to be noted that the above theorem can be stated for right most derivation as well.
Example
Consider the following CFG

ns
S→YX
X → XX|b
Y→YY|a
Following are the left most and right most derivations of abbbb
tio
S ⇒ YX S ⇒ YX
⇒ aX ⇒ YXX
⇒ aXX ⇒ YXb
ca
⇒ abX ⇒ YXXb
⇒ abXX ⇒ YXbb
⇒ abbX ⇒ YXXbb
⇒ abbXX ⇒ YXbbb
du

⇒ abbbX ⇒ Ybbbb
= abbbb = abbbb

A new format for FAs


A class of machines (FAs) has been discussed accepting the regular language corresponding to a regular
-e

language there is a machine in this class, accepting that language and corresponding to a machine of this class
there is a regular language accepted by this machine. It has also been discussed that there is a CFG
corresponding to regular language and CFGs also define some nonregular languages, as well
xt

There is a question whether there is a class of machines accepting the CFLs? The answer is yes. The new
machines which are to be defined are more powerful and can be constructed with the help of FAs with new
format.
To define the new format of an FA, some terms are defined in the next lecture.
ne

104
© Copyright Virtual University of Pakistan
Theory of Automata (CS402)

Theory of Automata

Lecture N0. 37
Reading Material

Introduction to Computer Theory Chapter 14

Summary
New format for FAs, input TAPE, START, ACCEPT , REJECT, READ states Examples of New Format of FA,
PUSH Down STACK , PUSH and POP, Example of PDA

A new format for FAs


A class of machines (FAs) has been discussed accepting the regular language corresponding to a regular

om
language there is a machine in this class, accepting that language and corresponding to a machine of this class
there is a regular language accepted by this machine. It has also been discussed that there is a CFG
corresponding to regular language and CFGs also define some nonregular languages, as well
There is a question whether there is a class of machines accepting the CFLs? The answer is yes. The new
machines which are to be defined are more powerful and can be constructed with the help of FAs with new
format.

.c
To define the new format of an FA, the following are to be defined
Input TAPE
The part of an FA, where the input string is placed before it is run, is called the input TAPE.

ns
The input TAPE is supposed to accommodate all possible strings. The input TAPE is partitioned with cells, so
that each letter of the input string can be placed in each cell. The input string abbaa is shown in the following
input TAPE.
Cell i Cell ii Cell iii
a b b a
tio
a ∆ ∆ .

The character ∆ indicates a blank in the TAPE. The input string is read from the TAPE starting from the cell (i).
ca
It is assumed that when first ∆ is read, the rest of the TAPE is supposed to be blank.

The START state


This state is like initial state of an FA and is represented by
du

START
-e

An ACCEPT state
This state is like a final state of an FA and is expressed by
xt

ACCEPT
ne

A REJECT state
This state is like dead'end non final state and is expressed by

REJECT

Note: It may be noted that the ACCEPT and REJECT states are called the halt states.

105
© Copyright Virtual University of Pakistan
Theory of Automata (CS402)

A READ state
This state is to read an input letter and lead to some other state. The READ state is expressed by

a
b
READ

Example
Before some other states are defined consider the following example of an FA along with its new format
b a
a

om
x' y+
b

Obviously the above FA accepts the language of strings, expressed by (a+b)*a. Following is the new format of
the above FA

.c
START

ns
a ∆
READ READ
b
tio
a

REJECT ACCEPT
ca

Note
The ∆ edge should not be confused with ∧)labeled edge. The ∆'edges start only from READ boxes and lead to
halt states. a a,b
du

Example b
1 b +

a
-e

The above FA accepts the language expressed by (a+b)*bb(a+b)*


START
a
xt

a,b
ne

a
b b
READ READ READ

∆ ∆ ∆

REJECT REJECT ACCEPT

106
© Copyright Virtual University of Pakistan
Theory of Automata (CS402)

PUSHDOWN STACK or PUSHDOWN STORE


PUSHDOWN STACK is a place where the input letters can be placed until these letters are referred again. It
can store as many letters as one can in a long column.
Initially the STACK is supposed to be empty each of its storage location contains a blank.
PUSH
A PUSH operator adds a new letter at the top of STACK, for if the letters a, b, c and d are pushed to the
STACK that was initially blank, the STACK can be shown as
STACK
d The PUSH state is expressed by
c
b
PUSH a
a

om
When a letter is pushed, it replaces the existing letter and pushes it one position below.

POP and STACK
POP is an operation that takes out a letter from the top of the STACK. The rest of the letters are moved one
location up. POP state is expressed as
b

.c
a
POP

ns

Note
It may be noted that popping an empty STACK is like reading an empty TAPE, popping a blank character ∆.
tio
It may also be noted that when the new format of an FA contains PUSH and POP states, it is called
PUSHDOWN Automata or PDAs. It may be observed that if the PUSHDOWN STACK (the memory structure)
is added to an FA then its language recognizing capabilities are increased considerably. Following is an example
of PDA
ca
Example
Consider the following PDA
START
du

a ∆
READ1
-e

PUSH a

b
b
xt

a ∆
POP1 READ2 POP2
ne

b, ∆ a,b
a ∆
REJECT ACCEPT
REJECT

The string aaabbb is to be run on this machine. Before the string is processed, the string is
supposed to be placed on the TAPE and the STACK is supposed to be empty as shown below
a a a b b b ≡ STACK



107
© Copyright Virtual University of Pakistan
Theory of Automata (CS402)

Reading first a from the TAPE we move from READ1State to PUSH a state, it causes the letter a deleted from
the TAPE and added to the top of the STACK, as shown below
a
TAPE a a a /b b b ≡ STACK ∆

Reading next two a’s successively, will delete further two a’s from the TAPE and add these
letters to the top of the STACK, as shown below a
a
a a a b b b ≡ STACK

om
TAPE a


Then reading the next letter which is b from the TAPE will lead to the POP1 state. The top letter at the STACK
is a, which is popped out and READ2 state is entered. Situation of TAPE and STACK is shown below

.c
TAPE a
a a a b b b ≡ a
STACK
a

ns



tio
Reading the next two b’s successively will delete two b’s from the TAPE, will lead to the
POP1 state and these b’s will be removed from the STACK as shown below
ca
TAPE a a a b b b ≡ ∆
STACK 


du

Now there is only blank character ∆ left, to be read from the TAPE, which leads to POP2 state. While the only
-e

blank characters is left in the STACK to be popped out and the ACCEPT state is entered, which shows that the
string aaabbb is accepted by this PDA. It may be observed that the above PDA accepts the language
{anbn: n = 0,1,2, … }.
xt

Since the null string is like a blank character, so to determine how the null string is accepted, it can be placed in
the TAPE as shown below

TAPE ≡
ne

Reading ∆ at state READ1 leads to POP2 state and POP2 state contains only ∆, hence it leads to ACCEPT state
and the null string is accepted.

Note: The process of running the string aaabbb can also be expressed in the table given in the next lecture.

108
© Copyright Virtual University of Pakistan
Theory of Automata (CS402)

Theory of Automata

Lecture N0. 38
Reading Material

Introduction to Computer Theory Chapter 14

Summary
Example of PDA with table for running a string, Equivalent PDA, PDA for EVEN EVEN Language. Non'
Derterministic PDA, Example of Non'Derterministic PDA, Definition of PUSH DOWN Automata, Example of
Non'Derterministic PDA.

om
START

a ∆

.c
PUSH a READ1

ns
b

a ∆
POP1 READ2 POP2
tio
b, ∆ a,b ∆
a
REJECT ACCEPT
ca
REJECT

Note
du

The process of running the string aaabbb can also be expressed in the following table

STATE STACK TAPE


START ∆… aaabbb∆ …
-e

READ1 ∆… aaabbb∆ …
PUSH a a∆ … aaabbb∆ …
xt

READ1 a∆ … aaabbb∆ …
PUSH a aa∆ … aaabbb∆ …
ne

READ1 aa∆ … aaabbb∆ …


PUSH a aaa∆ … aaabbb∆ …
READ1 aaa∆ … aaabbb∆ …
POP1 aa∆ … aaabbb∆ …

109
© Copyright Virtual University of Pakistan
Theory of Automata (CS402)

STATE STACK TAPE


READ2 aa∆ … aaabbb∆ …
POP1 a∆ … aaabbb∆ …

READ2 a∆ … aaabbb∆ …

POP1 ∆… aaabbb∆ …

READ2 ∆… aaabbb∆ …

POP2 ∆… aaabbb∆ …

om
ACCEPT ∆… aaabbb∆ …

It may be observed that the above PDA accepts the language {anbn : n=0,1,2,3, …}.

.c
Note
It may be noted that the TAPE alphabet Σ and STACK alphabet Γ, may be different in general and hence the
PDA equivalent to that accepting {anbn: n=0,1,2,3…} discussed above may be

ns
START
tio
a ∆
PUSH X READ1
ca
b
b

∆ X
X POP2
du

POP1 READ2


a ∆
-e

REJECT ACCEPT

Following is an example of PDA corresponding to an FA


xt

Example
Consider the following FA corresponding to the EVEN'EVEN language
ne

±
a

b b b b

a
The corresponding PDA will be
110
© Copyright Virtual University of Pakistan
Theory of Automata (CS402)

ACCEPT REJECT

START READ1 READ2

a
b b
b b
a

om
READ4 READ3

.c
REJECT
REJECT

ns
Nondeterministic PDA
Like TGs and NFAs, if in a PDA there are more than one outgoing edges at READ or POP states with one label,
then it creates nondeterminism and the PDA is called nondeterministic PDA.
In nondeterministic PDA no edge is labeled by string of terminals or nonterminals, like that can be observed in
tio
TGs. Also if there is no edge for any letter to be read from the TAPE, the machine crashes and the string is
rejected.
In nondeterministic PDA a string may trace more than one path. If there exists at least one path traced by a
string leading to ACCEPT state, then the string is supposed to be accepted, otherwise rejected.
ca
Following is an example of nondeterministic PDA

START a
POP1
du

a a
b

a
PUSH a READ1 b
-e

b
POP2 READ2
PUSH b b ∆
xt


ne

POP3

ACCEPT
Here the nondeterminism can be observed at state READ1. It can be observed that the above PDA accepts the
language
EVENPALINDROME={w reverse(w): w∈{a, b}*}
={Λ, aa, bb, aaaa, abba, baab, bbbb, …}
Now the definition of PDA including the possibility of nondeterminism may be given as follows

111
© Copyright Virtual University of Pakistan
Theory of Automata (CS402)

PUSHDOWN AUTOMATON (PDA)[ including the possibility of non determinism]


Pushdown Automaton (PDA), consists of the following
1. An alphabet Σ of input letters.
2. An input TAPE with infinite many locations in one direction. Initially the input string is placed in it
starting from first cell, the remaining part of the TAPE is empty.
3. An alphabet Γ of STACK characters.
4. A pushdown STACK which is initially empty, with infinite many locations in one direction. Initially
the STACK contains blanks.
5. One START state with only one out'edge and no in'edge.
6. Two halt states ACCEPT and REJECT states, with in'edges and no out'edges.
7. A PUSH state that introduces characters onto the top of the STACK.
8. A POP state that reads the top character of the STACK, (may contain more than one out'edges with
same label).

om
9. A READ state that reads the next unused letter from the TAPE, (may contain more than one out'edges
with same label).

Example: Consider the CFG


S → S+S|S*S|4
Following is the PDA accepting the corresponding CFL

.c
4 + *

ns
READ1 READ3
READ2
ACCEPT
START
tio +
S * ∆

PUSH1 S ∆
POP READ4
ca
S S
PUSH2 S PUSH5 S
du

PUSH3 + PUSH6 *
-e

PUSH4 S PUSH7 S
xt

The string 4 + 4 * 4 traces the path shown in the following table


STATE STACK TAPE
ne

START ∆ 4+4*4
PUSH1 S S 4+4*4
POP ∆ 4+4*4
PUSH2 S
PUSH3 +
PUSH4 S
POP
READ1
POP
112
© Copyright Virtual University of Pakistan
Theory of Automata (CS402)

STATE STACK TAPE

READ2

POP ∆

PUSH5 S

PUSH6 *

PUSH7 S

POP

om
READ1

POP

READ3

.c
POP ∆

READ1 ∆ ∆

ns
POP ∆ ∆

READ4 tio ∆ ∆

ACCEPT ∆ ∆

Note
It may be noted that the letters are deleted from the TAPE instead of underlined.
ca
It may also be noted that the choice of path at POP state can be determined by the left most deviation of the
string belonging to the CFL.
du
-e
xt
ne

113
© Copyright Virtual University of Pakistan
Theory of Automata (CS402)

Theory of Automata

Lecture N0. 39
Reading Material

Introduction to Computer Theory Chapter 15

Summary
PDA corresponding to CFG, Examples of PDA corresponding to CFG

PDA corresponding to CFG


Theorem
Corresponding to any CFG there exists a PDA accepting the language generated by the CFG.

om
Since an algorithm has already been discussed to convert the CFG in CNF, so the PDA can be constructed
corresponding to the CFG. As the CFG in CNF generates all the nonnull words of the corresponding CFL, so
accepting the null string (if it is contained in the CFL), can be managed separately.

Example

.c
Consider the following CFG which is in CNF and does not generate the null string
S → SB|AB
A → CC

ns
B→b
C→a
The corresponding PDA will be
tio
a b

READ1
ca
READ2 ACCEPT
START
C B ∆
du


PUSH S POP READ3

S
A
S
-e

PUSH B PUSH B PUSH C


xt

PUSH S PUSH A PUSH C


ne

Here the STACK alphabet Γ= {S, A, B, C}, where the TAPE alphabet ∑={a, b}

Note: It may be noted that when the POP state is entered either a nonterminal is replaced by two nonterminals at
the top of the STACK accommodating a production, or a nonterminal is popped out from the top of the stack
and a READ state is entered to read a specified letter from the TAPE or else the machine crashes.
The choice of path taken at POP state to accommodate the word belonging to the CFL can be determined by the
left most derivation of the word. Consider the word aab with its left most derivation, as follows

114
© Copyright Virtual University of Pakistan
Theory of Automata (CS402)

Working'String Generation Production Used


S ⇒ AB S → AB step 1
⇒ CCB A → CC step 2
⇒ aCB C→a step 3
⇒ aaB C→a step 4
⇒ aab B→b step 5

First of all the START state is entered

STACK TAPE
∆… aab∆…

om
The PUSH S state is entered

STACK TAPE
∆…

.c
The POP state is entered and to accommodate the production S → AB, PUSH B and PUSH A states are entered.

STACK TAPE

ns
∆…
Then the POP state is entered and to accommodate the production A → CC, PUSH C, PUSH C states are
entered
tio
STACK TAPE
ca
The POP state is entered and to accommodate the production C → a, READ1 is entered and the letter a is read
from the TAPE.
STACK TAPE
du

The POP state is entered and to accommodate the production C → a, READ1 state is entered and the letter a is
read from the TAPE
STACK TAPE
-e

The POP state is entered and to accommodate the production B → b, READ2 state is entered and the letter b is
xt

read from the TAPE


STACK TAPE
ne

The shown in the STACK indicates that there are no nonterminals in the working string and is read from the
STACK which leads to READ3 state where the is read from the TAPE and the ACCEPT state is entered
which shows that the word aab is accepted by the PDA.
Following is the table showing all the observations discussed above, for the word aab

115
© Copyright Virtual University of Pakistan
Theory of Automata (CS402)

Left most STATE STACK TAPE


derivation
START aab
S PUSH S S aab
POP aab
PUSH B B aab
⇒AB PUSH A AB aab
POP B aab
PUSH C CB aab

om
⇒CCB PUSH C CCB aab
POP CB aab
⇒aCB READ1 CB aab

POP B aab

.c
⇒aaB READ1 B aab

POP aab

ns
⇒aab READ2 aab

POP aab
tio
READ3 aab

ACCEPT aab
ca
Following is an example of building the PDA corresponding to the given CFG

Example
Consider the following CFG
du

S → XY
X → aX | bX |a
Y → Ya | Yb | a
First of all, converting the CFG to be in CNF, introduce the nonterminals A and B as
A→a
-e

B→b
The following CFG is in CNF
S → XY
xt

X → AX | BX |a
Y → YA | YB | a
A→a
ne

B→b

116
© Copyright Virtual University of Pakistan
Theory of Automata (CS402)

The PDA corresponding to the above CFG will be

a a a b
READ3 READ4
READ1
READ2
B
START A ACCEPT
X Y


PUSH S POP READ5

om
S
X Y
X
Y
PUSH X PUSH X PUSH A PUSH B
PUSH Y

.c
PUSH X PUSH A PUSH B PUSH Y PUSH Y

ns
The word aaab can be generated as
Working'String Generation
tio
Production Used
S ⇒ XY S → XY step 1
⇒ AXY X → AX step 2
⇒ aXY A→a step 3
⇒ aaY X→a
ca
step 4
⇒ aaYB Y → YB step 5
⇒ aaaB Y→a step 6
⇒ aaab B→b step 7
du

STACK TAPE STACK TAPE


(START) ∆ aaab (POP) Y aaab
-e

(PUSH S) S aaab (READ1) Y aaab

(POP) ∆ aaab ∆ aabb


xt

(PUSH Y) Y aaab (PUSH B) B aabb

(PUSH X) XY aaab (PUSH Y) YB aabb


ne

(POP) Y aaab (POP) B aaab

(PUSH X) XY aaab (READ2) B aaab

(PUSH A) AXY aaab ∆ aaab

(POP) XY aaab (READ4) ∆ aaab

(READ3) XY aaab ∆ ∆

117
© Copyright Virtual University of Pakistan
Theory of Automata (CS402)

Theory of Automata

Lecture N0. 40
Reading Material

Introduction to Computer Theory Chapter 15

Summary
Recap of example of PDA corresponding to CFG, CFG corresponding to PDA. Theorem, HERE state,
Definition of Conversion form, different situations of PDA to be converted into conversion form

Example
Consider the following CFG

om
S → XY
X → aX | bX |a
Y → Ya | Yb | a
First of all, converting the CFG to be in CNF, introduce the nonterminals A and B as
A→a
B→b

.c
The following CFG is in CNF
S → XY
X → AX | BX |a

ns
Y → YA | YB | a
A→a
B→b
The PDA corresponding to the above CFG will be
tio
a a a b
READ3 READ4
ca
READ1
READ2
B
START A ACCEPT
du

X Y


PUSH S POP READ5
-e

S
X Y
X
Y
xt

PUSH X PUSH X PUSH A PUSH B


PUSH Y
ne

PUSH X PUSH A PUSH B PUSH Y PUSH Y

Theorem
Given a PDA that accepts the language L, there exists a CFG that generates exactly L.

Before the CFG corresponding to the given PDA is determined, the PDA is converted into the standard form
which is called the conversion form.
Before the PDA is converted into conversion form a new state HERE is defined which is placed in the middle of
any edge.
Like READ and POP states, HERE states are also numbered

118
© Copyright Virtual University of Pakistan
Theory of Automata (CS402)

READ7 a READ9 b
becomes

READ7 a HERE3 READ9 b

Conversion form of PDA


Definition
A PDA is in conversion form if it fulfills the following conditions:
There is only one ACCEPT state.
There are no REJECT states.

om
Every READ or HERE is followed immediately by a POP every edge leading out of any READ or HERE
state goes directly into a POP state.
No two POPs exist in a row on the same path without a READ or HERE between them whether or not there are
any intervening PUSH states ( the POP states must be separated by READs or HEREs).
All branching, deterministic or nondeterministic occurs at READ or HERE states, none at POP states and every
edge has only one label.

.c
Even before we get to START, a “bottom of STACK” symbol $ is placed on the STACK. If this symbol is ever
popped in the processing it must be replaced immediately. The STACK is never popped beneath this symbol.
Right before entering ACCEPT this symbol is popped out and left.

ns
The PDA must begin with the sequence

START $
POP PUSH $ HERE
tio
The entire input string must be read before the machine can accept the word.
Different situations of a PDA to be converted into conversion form are discussed as follows
ca
To satisfy condition 3,
READ7 a READ8 b

b
du

becomes
PUSH a
a
-e

a b b
READ7 POP PUSH b READ7
$
b PUSH $
xt

To satisfy condition 4,
a b
ne

POP4 POP5

becomes

a b
POP4 HERE POP5

119
© Copyright Virtual University of Pakistan
Theory of Automata (CS402)

To satisfy condition 5
a b
READ1 POP1 READ2

READ3

becomes

om
a b
READ1 POP2 READ2

.c
a
POP3 READ3

ns
To satisfy condition 5
PUSH a
a
a
READ1 READ2
POP
tio b

PUSH b
ca
becomes
a
POP1 PUSH a
a
du

READ1 READ2

a b
POP2 PUSH b
-e

To satisfy condition 6, it is supposed that the STACK is initially in the position shown below
STACK
xt

$
ne

120
© Copyright Virtual University of Pakistan
Theory of Automata (CS402)

Theory of Automata

Lecture N0. 41
Reading Material

Introduction to Computer Theory Chapter 15

Summary
Recap of PDA in conversion form, example of PDA in conversion form, joints of the machine, new pictorial
representation of PDA in conversion form, summary table, row sequence, row language.

Conversion form of PDA


Definition

om
A PDA is in conversion form if it fulfills the following conditions:
There is only one ACCEPT state.
There are no REJECT states.
Every READ or HERE is followed immediately by a POP every edge leading out of any READ or HERE
state goes directly into a POP state.
No two POPs exist in a row on the same path without a READ or HERE between them whether or not there are

.c
any intervening PUSH states ( the POP states must be separated by READs or HEREs).
All branching, deterministic or nondeterministic occurs at READ or HERE states, none at POP states and every
edge has only one label.

ns
Even before we get to START, a “bottom of STACK” symbol $ is placed on the STACK. If this symbol is ever
popped in the processing it must be replaced immediately. The STACK is never popped beneath this symbol.
Right before entering ACCEPT this symbol is popped out and left.
The PDA must begin with the sequence
tio
$
START POP PUSH $ HERE
ca
The entire input string must be read before the machine can accept the word.

Example
du

Consider the following PDA accepting the language {a2nbn : n = 1,2,3, …}

START
-e

b a a
READ1 POP1 POP2 READ2
xt

b
a
ne

PUSH a
$
ACCEPT POP3
Which may be converted to

121
© Copyright Virtual University of Pakistan
Theory of Automata (CS402)

$
PUSH $ POP4 START

b a
READ1 POP1 HERE POP2

a a
a b
READ2

om
POP5 POP6

a $ $
PUSH a PUSH $ ACCEPT POP3

.c
PUSH a PUSH a

ns
The above PDA accepts exactly the same language tio
Note
It may be noted that any PDA which is in conversion form can be considered to be the collection of path
ca
segments, where each path segment is of the following form

FROM TO READ POP PUSH


START READ ONE or Exactly Any
du

or READ or HERE no input one string


or HERE or letter STACK onto the
ACCEPT character STACK
-e

START, READ, HERE and ACCEPT states are called the joints of the machine. Between two consecutive
joints on a path exactly one character is popped and any number of characters can be pushed.
The PDA which is in the conversion form can be supposed to be the set of joints with path segments in
between, similar to a TG
xt

START READ1 HERE


ne

READ2

ACCEPT

122
© Copyright Virtual University of Pakistan
Theory of Automata (CS402)

The above entire machine can be described as a list of all joint'to'joint path segments, called summary table.
The PDA converted to the conversion form has the following summary table

FROM TO READ POP PUSH ROW


Where Where What What What Number

START READ1 Λ $ $ 1

READ1 READ1 a $ a$ 2

READ1 READ1 a a aa 3

READ1 HERE b a '' 4

om
HERE READ2 Λ a '' 5

READ2 HERE b a '' 6

READ2 AT $ '' 7

.c
Consider the word aaaabb. This word is accepted by the above PDA through the following path
START−POP4−PUSH$−READ1−POP6−PUSH $−PUSH a−READ1−POP5−PUSH a−PUSH a− READ1−
POP5−PUSH a−PUSH a−READ1−POP5−PUSH a−PUSH a−READ1−POP1− HERE−POP2−READ2−POP1−

ns
HERE−POP2−READ2−POP3−ACCEPT.
The above path can also be expressed by the following path in terms of sequence of rows
Row1 –Row2 –Row3 –Row3 –Row3 –Row4 –Row5 –Row6 –Row5 –Row7
It can be observed that the above path is not only joint)to)joint consistent but STACK consistent as well.
tio
It may be noted that in FAs, paths correspond to strings of letters, while in PDAs, paths correspond to strings of
rows from the summary table.

Note
It may be noted that since the HERE state reads nothing from the TAPE, therefore Λ is kept in the READ what
ca

column.
It may also be noted that the summary table contains all the information of the PDA which is in the pictorial
representation. Every path through the PDA is a sequence of rows of the summary table. However, not every
sequence of rows from the summary table represents a viable path, . every sequence of rows may not be
du

STACK consistent.
It is very important to determine which sequences of rows do correspond to possible paths through the PDA,
because the paths are directly related to the language accepted, Row4 cannot be immediately followed by
Row6 because Row4 leaves in HERE, while Row6 begins in Read2. Some information must be kept about the
-e

STACK before rows are concatenated.


To represent a path, a sequence of rows must be joint)consistent (the rows meet up end to end) and STACK)
consistent (when a row pops a character it should be there at the top of the STACK).
The next target is to define row language whose alphabet is ∑ = {Row1, Row2, …, Row7} the alphabet
xt

consists of the letters which are the names of the rows in the summary table.

Note
ne

It may be noted that the words of the row language trace joint)to)joint and STACK consistent paths, which
shows that all the words of this language begin with Row1 and end in Row7. Consider the following row
sequence Row5 Row5 Row3 Row6
This is string of 4 letters, but not word of the row language because
It does not represent a path starting from START and ending in ACCEPT state.
It is not joint consistent.
It is not STACK consistent.
Before the CFG that generates the language accepted by the given PDA, is determined, the CFG that generates
the row language is to be determined. For this purpose new nonterminals are to be introduced that contain the
information needed to ensure joint and STACK consistency.
It is not needed to maintain any information about what characters are read from the TAPE.

123
© Copyright Virtual University of Pakistan
Theory of Automata (CS402)

Theory of Automata

Lecture N0. 42
Reading Material

Introduction to Computer Theory Chapter 15

Summary
Row language, nonterminals defined from summary table, productions defined by rows, rules for defining
productions, all possible productions of CFG for row language of the example under consideration, CFG
corresponding to the given PDA

Note

om
As has already been discussed that the Row language is the language whose alphabet
∑ = {Row1, Row2, …, Row7 }, for the example under consideration, so to determine the CFG of Row language,
the nonterminals of this CFG are introduced in the form Net(X, Y, Z)
where X and Y are joints and Z is any STACK character. Following is an example of Net(X, Y, Z)

Z b a

.c
POP PUSH a PUSH b POP POP

If the above is the path segment between two joints then, the net STACK effect is same as POP Z.

ns
For a given PDA, some sets of all possible sentences Net(X, Y, Z) are true, while other are false. For this
purpose every row of the summary table is examined whether the net effect of popping is exactly one letter.
Consider the Row4 of the summary table developed for the PDA of the language {a2nbn}

FROM TO
tio
READ POP PUSH ROW
Where Where What What What Number

READ1 HERE b a '' 4


ca

The nonterminal corresponding to the above row may be written as Net (READ1, HERE, a) Row4 is a single
Net row.
Consider the following row from an arbitrary summary table
du

FROM TO READ POP PUSH ROW


Where Where What What What Number
-e

READ9 READ3 b b abb 11

which shows that Row11 is not Net style sentence because the trip from READ9 to READ3 does not pop one
letter form the STACK, while it adds two letters to the STACK. However Row11 can be concatenated with some
xt

other Net style sentences Row11Net(READ3, READ7, a)Net(READ7, READ1, b)Net(READ1, READ8, b)
Which gives the nonterminal
Net(READ9, READ8, b), now the whole process can be written as
ne

Net(READ9, READ8, b) → Row11Net(READ3, READ7,a) Net(READ7, READ1, b)Net(READ1, READ8, b)


Which will be a production in the CFG of the corresponding row language.
In general to create productions from rows of summary table, consider the following row in certain summary
table

FROM TO READ POP PUSH ROW


Where Where What What What Number

READx READy u w m1m2…mn i

then for any sequence of joint states S1, S2, …Sn, the production in the row language can be included as
124
© Copyright Virtual University of Pakistan
Theory of Automata (CS402)

Net(READx, Sn, w) → RowiNet(READy, S1, m1)…Net(Sn'1, Sn, mn)


It may be noted that in CFG, in general, replacing a nonterminal with string of some other nonterminals does not
always lead to a word in the corresponding CFL S → X|Y, X → ab, Y → aYY
Here Y → aYY does not lead to any word of the language.
Following are the three rules of defining all possible productions of CFG of the row language
The trip starting from START state and ending in ACCEPT state with the NET style
Net(START, ACCEPT, $) gives the production of the form S → Net(START, ACCEPT, $)
From the summary table the row of the following form
FROM TO READ POP PUSH ROW
Where Where What What What Number

X Y anything z i

om
))
Defines the productions of the form Net(X,Y,z) → Rowi
For each row that pushes string of characters on to the STACK of the form

FROM TO READ POP PUSH ROW


Where

.c
Where What What What Number

READx READy u w m1 m2…mn i

ns
then for any sequence of joint states S1, S2, …Sn, the production in the row language can be included as
Net(READX,Sn, w) → RowiNet(READY, S1,m1) …Net(Sn'1, Sn, mn)
It may be noted that this rule introduces new productions. It does not mean that each production of the form
tio
Nonterminal → string of nonterminals, helps in defining some word of the language.

Note
Considering the example of PDA accepting the language {a2nbn:n=1, 2, 3, …}, using rule1, rule2 and rule3 the
ca
possible productions for the CFG of the row language are
S → Net(START, ACCEPT, $)
Net(READ1, HERE, a) → Row4
Net(HERE, READ2, a) → Row5
Net(READ2, HERE, a) → Row6
du

Net(READ2, ACCEPT, $) → Row7


Net(START, READ1, $) → Row1Net(READ1, READ1, $)
Net(START, READ2, $) → Row1Net(READ1,READ2, $)
Net(START, HERE, $) → Row1Net(READ1, HERE, $)
-e

Net(START, ACCEPT, $) → Row1Net(READ1, ACCEPT, $)


Net(READ1, READ1, $) → Row2Net( READ1, READ1, a)Net(READ1, READ1, $)
Net(READ1, READ1, $) → Row2Net( READ1, READ2, a)Net(READ2, READ1, $)
xt

Net(READ1, READ1, $) → Row2Net( READ1, HERE, a)Net(HERE, READ1, $)


Net(READ1, READ2, $) → Row2Net( READ1, READ1, a)Net(READ1, READ2, $)
Net(READ1, READ2, $) → Row2Net( READ1, READ2, a)Net(READ2, READ2, $)
ne

Net(READ1, READ2, $) → Row2Net( READ1, HERE, a)Net(HERE, READ2, $)


Net(READ1, HERE, $) → Row2Net( READ1, READ1, a)Net(READ1, HERE, $)
Net(READ1, HERE, $) → Row2Net( READ1, READ2, a)Net(READ2, HERE, $)
Net(READ1, HERE, $) → Row2Net( READ1, HERE, a)Net(HERE, HERE, $)
Net(READ1, ACCEPT, $) → Row2Net( READ1,READ1,a)Net(READ1,ACCEPT, $)
Net(READ1,ACCEPT, $) → Row2Net( READ1,READ2,a)Net(READ2,ACCEPT, $)
Net(READ1, ACCEPT, $) → Row2Net( READ1, HERE, a)Net(HERE, ACCEPT, $)
Net(READ1, READ1, a) → Row3Net( READ1, READ1, a)Net(READ1, READ1, a)
Net(READ1, READ1, a) → Row3Net( READ1, READ2, a)Net(READ2, READ1, a)
Net(READ1, READ1, a) → Row3Net( READ1, HERE, a)Net(HERE, READ1, a)
Net(READ1, READ2, a) → Row3Net( READ1, READ1, a)Net(READ1, READ2, a)
Net(READ1, READ2, a) → Row3Net( READ1, READ2, a)Net(READ2, READ2, a)
Net(READ1, READ2, a) → Row3Net( READ1, HERE, a)Net(HERE, READ2, a)
125
© Copyright Virtual University of Pakistan
Theory of Automata (CS402)

Net(READ1, HERE, a) → Row3Net( READ1, READ1, a)Net(READ1, HERE, a)


Net(READ1, HERE, a) → Row3Net( READ1, READ2, a)Net(READ2, HERE, a)
Net(READ1, HERE, a) → Row3Net( READ1, HERE, a)Net(HERE, HERE, a)
Net(READ1, ACCEPT, a) → Row3Net( READ1, READ1,a)Net(READ1,ACCEPT,a)
Net(READ1, ACCEPT, a) → Row3Net( READ1, READ2,a)Net(READ2,ACCEPT,a)
Net(READ1, ACCEPT, a) → Row3Net (READ1, HERE,a)Net(HERE,ACCEPT,a)
Following is a left most derivation of a word of row language
S ⇒ Net(START, ACCEPT, $) … using 1
⇒ Row1Net(READ1, ACCEPT, $) … using 9
⇒ Row1Row2Net(RD1,RD2, a)Net(RD2,AT, $) … using 20
⇒ Row1Row2Row3Net(RD1, HERE,a)Net (RD2,HERE,a)Net(RD2,AT,$)… using 27
⇒ Row1Row2Row3Row4Net(HERE, RD2, a)Net(RD2, ACCEPT, $) … using 2
⇒ Row1Row2Row3Row4Row5Net(HERE, ACCEPT, $)

om
… using 3
⇒ Row1Row2Row3Row4Row5Row7 … using 5
Which is the shortest word in the whole row language.
It can be observed that each left most derivation generates the sequence of rows of the summary table, which are
both joint) and STACK) consistent.
So far the rules have been defined to create all possible productions for the CFG of the row language.

.c
Since in each row in the summary table, the READ column contains Λ and in addition to the letters of the
alphabet of the language accepted by the PDA, so each word of the row language generates the word of the
language accepted by the given PDA.

ns
Thus the following rule 4 helps in completing the CFG corresponding to the given PDA
Each row of the summary table defines a production of the form Rowi → a where in Rowi the READ column
consists of letter a.
Application of rule 4 to the summary table for the PDA accepting {a2nbn : n=1,2,3,…} under consideration adds
the following productions
tio
Row1 → Λ
Row2 → a
Row3 → a
Row4 → b
ca

Row5 → Λ
Row6 → b
Row7 →
Which shows that the word Row1Row2Row3Row4Row5Row7 of the row language is converted to ΛaabΛ = abb
du
-e
xt
ne

126
© Copyright Virtual University of Pakistan
Theory of Automata (CS402)

Theory of Automata

Lecture N0. 43
Reading Material

Introduction to Computer Theory Chapter 16

Summary
Non'Context'Free'languages, Live Production, Dead Production, Theorem, self' embedded nonterminal,
Pumping lemma for CFLs, Examples

Non)Context)Free language
There arises a question, whether all languages are CFL? The answer is no.

om
Languages which are not Context'Free, are called Non'CFL.

To prove the claim that all languages are not Context'Free, the study of machines of word production from the
grammar is needed

Live production: A production of the form nonterminal → string of two nonterminals is called a live production.

.c
Dead production: A production of the form nonterminal → terminal is called a dead production.

ns
It may be noted that every CFG in CNF has only these types of productions.

Theorem
If a CFG is in CNF and if there is restriction to use the live production at most once each, then only the finite
many words can be generated.
tio
It may be noted that every time a live production is applied during the derivation of a word it increases the
number of nonterminals by one.
Similarly applying dead production decreases the nonterminals by one. Which shows that to generate a word,
ca
one more dead production are applied than the live productions
S ⇒ XY
⇒aY
⇒aa
du

Here one live and two dead productions are used.


In general, if a CFG in CNF has p live and q dead productions then all words generated without repeating any
live production have at most (p+1) letters.

Theorem
-e

If a CFG is in CNF with p live and q dead productions and if w is word generated by the CFG, having more than
2p letters then any derivation tree for w has a nonterminal z which is used twice, where the second z is the
descended from the first z.
xt

It can be observed from the above theorem that generation tree of word w has more than p rows.
Self'embedded nonterminal S
A nonterminal is said to be self'embedded, if in a given derivation
ne

of a word, it ever occurs as a tree


descendant of itself, as shown in figure aside A X

a S A

A X a

a S A

b a

127
© Copyright Virtual University of Pakistan
Theory of Automata (CS402)

Here the nonterminal X is self'embedded.

Note S
Consider the following CFG in CNF
S → AB
A → BC
A B
C → AB
A→a
B→b
and the derivation tree of the word bbabbb B C
b

om
b A B

B C b

.c
b A B

ns
a b
Note
The part of tree enclosed in upper triangle is identical to that enclosed in lower triangle, there is still another
tio
option of replacing A by the same sequence of production shown in lower triangle.
The above fact provides the following pumping lemma for the CFLs.

Pumping lemma for CFLs


ca
Theorem
If G is any CFG in CNF with p live productions, then every word w of length more than 2p can be partitioned
into five substrings as w = uvxyz, where x is not null string and v and y are not both null string.
Then all the words of the form uvnxynz, n = 1,2,3,… can also be generated by G.
du

Example
Consider the following CFG which is in CNF
S → PQ
Q → QS|b
-e

P→a
and a word abab generated by the above CFG with the S
following derivation tree
xt

P Q
ne

a Q S
u

P Q
b
x

a b
y

128
© Copyright Virtual University of Pakistan
Theory of Automata (CS402)

Then w can be broken up as w = uvxyz where u = a, v = Λ, x = b, y = ab, z = Λ


Repeating the triangle from the second Q just as it descends from the first Q, the corresponding tree may be
expressed as follows
S

P S

om
Q S
a
u
Q S P Q

.c
P Q
b
x

ns
a b a b
tio y y

Which shows that uvvxyyz=aΛΛbababΛ=ababab belongs to the language generated by the given CFG.
So, it can be generalized that words of the form uvnxynz, n=1,2,3,… belong to the language generated by the
given CFG.
ca

Note
It may be noted that the pumping lemma is satisfied by all CFLs and the languages which don’t hold this
pumping lemma, can’t be Context Free languages. Such languages are non'CFLs.
du

Example
Consider the language
L={anbncn :n=1,2,3,…}, let the language L be Context Free language and let the word w=a200b200c200 of length
more than 2p, where p is the number of live productions of its CFG in CNF.
-e

Note
It can be observed that no matter what choices are made for the substrings u,v,x,y and z, uv2xy2z can’t belong to
L, as all the words in anbncn have
xt

Only one substring ab


Only one substring bc
ne

No substring ac
No substring ba
No substring ca
No substring cb
For any n=1,2,3,…

The above observations shows that if v or y is not single letter or Λ, then uv2xy2z may contain either two or
more substrings ab or bc or one or more substrings ac or ba or ca or cb these strings may be in the number
more than the number they are supposed to be.
Moreover, if v and y are either single letter or Λ, then one or two of letters a,b,c will be increased, where as the
other letter will not be increased in uv2xy2z, which shows uv2xy2z does not belong to L.
Thus pumping lemma is not satisfied. Hence L is non CFL.

129
© Copyright Virtual University of Pakistan
Theory of Automata (CS402)

It may be noted that the pumping lemma discussed for infinite regular language L, the word w can be
decomposed into three parts w=xyz, such that all words of the form xynz, n=1,2,3,…, belong to L.
Similarly, the pumping lemma discussed for CFLs can also stated as
If w is a large enough word in a CF: then, w can be decomposed into w=uvxyz such that all words of the
form uvnxynz belong to L
It may be noted that proof of pumping lemma for regular languages needed that path of word w to be so large
enough so that it contains a circuit and circuit can be looped as many times as one can. The proof of the
pumping lemma for CFLs needs the derivation for w to be so large that it contains a sequence of productions
that can be repeated as many times as one can.
Moreover, the pumping lemma for regular languages does not hold for non regular language as that language
does not contain both xyz and xyyz.
Similarly pumping lemma for CFLs does not hold for non'CFL as that language does not contain both uvxyz
and uvvxyyz.

om
There is another difference between the pumping lemma for regular languages and that for CFLs that first one
acts on machines while other acts on algebraic structures grammar.
To achieve full power the pumping lemma for regular languages has modified by pumping lemma version II.
Similarly, full power for pumping lemma for CFLs is achieved by stating the following theorem

Theorem

.c
If L is a CFL in CNF with p live productions then any word W in L of length more than 2p can be decomposed
as w=uvxyz length(vxy) ≤ 2p, length(x) > 0, length(v)+length(y) > 0
then the words of the form uvnxynz : n=1,2,3,… belong to L.

ns
Example
Consider the language
L= {anbmanbm :m,n=1,2,3,…}
tio
={abab,aabaab, abbabb, aabbaabb, aaabaaab,… }
The first version of pumping lemma for CFLs may be satisfied by L, but to apply the second version of pumping
lemma to L, let L be generated by CFG which is in CNF and has p live productions.
Consider the word decomposing w into uvxyz where length(vxy) < 2p which shows that v and y can’t be single
letters separated by clumps of other letter because the separator letter is longer than the length of whole
ca
substring vxy, which shows that uvvxyyz is not contained in L. Thus pumping lemma is not satisfied and L is
non CFL.

Example
du

Consider the language EVENA


EVENA=(aa)n =a2n ={aa, aaaa, aaaaaa, …}
The grammar for this language must be
S → SS|aa and its CNF will be
-e

S → SS|AA, A → a, the PDA for this grammar will be as under

PUSH S START
xt

a A
ne

READ1 POP READ2 ACCEPT

S
S

PUSH A PUSH S

PUSH A PUSH S

Its corresponding conversion form will be

130
© Copyright Virtual University of Pakistan
Theory of Automata (CS402)

$ START
PUSH $ POP

A $
PUSH S
POP POP POP
S
a a a
PUSH S
READ1

om
PUSH A A

POP
PUSH $

.c
$
HERE POP

ns
PUSH $
tio
POP POP

S S READ2
ca

PUSH A PUSH S
du

POP
PUSH A PUSH S

$
-e

ACCEPT
xt

The summary table corresponding to the above PDA in conversion form can be expressed as
FROM TO READ POP PUSH ROW
START HERE Λ $ S$ 1
ne

HERE HERE Λ S SS 2
HERE HERE Λ S AA 3
HERE READ1 Λ A '' 4
READ1 HERE a S S 5
RAED1 HERE a $ $ 6
READ1 HERE a A A 7
HERE READ2 Λ $ $ 8
READ2 ACCEPT $ '' 9

Following are the productions defined from the summary table


131
© Copyright Virtual University of Pakistan
Theory of Automata (CS402)

S → Net(START, ACCEPT, $)
Net(HERE, READ1, A) → Row4
Net(READ2, ACCEPT, $) → Row9
Net(START, X, $) → Row1 Net(HERE, Y, S)Net(Y, X, $)
Net(HERE, X, S) → Row2 Net(HERE, Y, S)Net(Y, X, S)
Net(START, X, S) → Row3 Net(HERE, Y, A)Net(Y, X, A)
Net(READ1, X, S) → Row5 Net(HERE, X, S) gives four productions
Net(READ1, X, $) → Row6 Net(HERE, X, $) gives four productions
Net(READ1, X, A) → Row7 Net(HERE, X, A) gives four productions
Net(HERE, ACCEPT, $) → Row8 Net(READ2, ACCEPT, $)
Where X and Y are the corresponding joints
In addition to 44 productions following 9 productions complete the required CFG
Row1 → Λ

om
Row2 → Λ
Row3 → Λ
Row4 → Λ
Row5 → a
Row6 → a

.c
Row7 → a
Row8 → Λ
Row9 → Λ

ns
tio
ca
du
-e
xt
ne

132
© Copyright Virtual University of Pakistan
Theory of Automata (CS402)

Theory of Automata

Lecture N0. 44
Reading Material

Introduction to Computer Theory Chapter 18

Summary
Decidability, whether a CFG generates certain string (emptiness), examples, whether a nonterminal is used in
the derivation of some word (uselessness), examples, whether a CFL is finite (finiteness), example, whether the
given string is generated by the given CFG (membership), example, parsing techniques, top down parsing,
example

om
Decidablity
Following are the decidable problems w.r.t. CFG
Whether or not the given CFG generates any word? Problem of emptiness of CFL.
Whether or not the given CFG generates the finite language? Problem of finiteness.
Whether or not the given string w can be generated by the given CFG? Problem of membership.

.c
Following are algorithms showing that the answers to the above three questions are yes.

Algorithm 1 (Emptiness)

ns
If the given CFG contains a production of the form S→Λ, then obviously the corresponding CFL is not empty.
If the CFG contains the production of the form S→t, where t is a terminal or string of terminal then t is a word
of the corresponding CFL and CFL is not empty.
If the CFG contains no such production then
tio
For each nonterminal N with N→t, pick one production for N (if there are more than one) and replace N by t in
the right side of each production wherever it lies. Remove all such productions from the CFG. Doing so the
CFG will be changed, it will generate atleast one word of the old CFL.
ca
Repeat the process until either it eliminates S or no new nonterminal is eliminated.
If S has been eliminated then CFG generates some words otherwise not.

Example
S→AB, A →BSB, B→CC
du

C→SS
A→a|b
C →b|bb
Step (1). Picking A→a, C →b, it can be written as
-e

S→aB
A→BSB
A→bb
xt

B→aaS
B→bb
C→SS
ne

Step (1). Picking B→bb and A→bb, it can be written as


S→abb
A→bbSbb
B→aaS
C→SS
Since S→abb has been obtained so, abb is a word in the corresponding CFL.

To determine whether the nonterminal X is ever used in the derivation of word from the given CFG, following
algorithm is used

Algorithm 2 (Uselessness)
Find all unproductive nonterminals (the nonterminal is unproductive if it cannot produce a string of terminals).
Eliminate all productions involving unproductive nonterminals.

133
© Copyright Virtual University of Pakistan
Theory of Automata (CS402)

Paint all X’s blue.


If any nonterminal is in the left side of the production with any blue nonterminal in the right side, paint that
nonterminal blue and paint that nonterminal blue at all occurrences of it throughout the grammar.
Repeat step 4 until no new nonterminal is painted.
If S is blue then X is useful member of CFG, otherwise not.

Example
Consider the following CFG
S→Aba | bAZ | b
A→Xb | bZa
B→bAA
X→aZa|aaa
Z→ZAbA

om
To determine whether X is ever used to generate some words, unproductive nonterminals are determined. Z is
unproductive nonterminal, so eliminating the productions involving Z.
S→Aba|b
A→Xb
B→bAA

.c
X→aaa
X is blue, so A is blue. Thus B and S are also blue. Since S is blue so X can be used to generate certain word
from the given CFG.

ns
: It may be noted that a nonterminal is called useless if it cannot be used in a production of some word.
Following algorithm is used to determine whether the given CFG generate the finite language

Algorithm 3 (Finiteness)
Determine all useless nonterminals and eliminate all productions involving these nonterminals.
tio
For each of the remaining nonterminals, determine whether they are self'embedded (using the following steps).
Stop if a self'embedded nonterminal is discovered.
To test whether X is self'embedded
Change all X’s on the left side of the productions into a Greek letter Ψ and keep all X’s on the right side as such.
ca
Paint all X’s blue.
If Y is any nonterminal on the left side of the production with X in the right side, then paint Y blue.
Repeat step (c) until no new nonterminal is painted.
If Ψ is painted, then the X is self'embedded, otherwise not.
du

If any nonterminal, left in the grammar, after step 1, is self'embedded then the language generated is infinite,
otherwise finite.

Example
Consider the CFG
-e

S→ABa|bAZ|b
A→Xb|bZa
B→bAA
xt

X→aZa|bA|aaa
Z→ZAbA
Here the nonterminal Z is useless, while all other are used in the derivation of some word. So eliminating the
ne

productions involving Z
S→ABa|b
A→Xb
B→bAA
X→bA|aaa
Starting with nonterminal X. Replacing X on left side of the production by Ψ
S→ABa|b
A→Xb
B→bAA
Ψ →bA|aaa
X is blue so A is blue and so Ψ is blue. Since A is blue, so B is blue and so S is blue. Since Ψ is blue so X is
self'embedded and hence the CFG generates the infinite language.
To determine whether a string is generated by the given CFG, following algorithm is used

134
© Copyright Virtual University of Pakistan
Theory of Automata (CS402)

Algorithm 4 (The CYK algorithm)


This algorithm was invented by John Cocke and later was published by Tandao Kasami and Daniel H. Younger.
Convert the given CFG in CNF.
Let the string x under consideration has the form x=x1x2x3…xn where all xis may not be different. List all the
nonterminals in the given CFG, say, S, N1,N2, …
List the nonterminals that generates single letter substrings of x

Substring All producing nonterminals

x1 N…

x2 N…

om
x3 ∂

∂ ∂

xn N…

.c
List the nonterminals that generates substrings of length 2 .

ns
Substring All producing nonterminals

x1 x2 tio N…

x2 x3 N…

x3 x4 ∂
ca
∂ ∂

xn'1 xn N…
du

Similarly, list of nonterminals generating substring of x of length 3

Substring All producing nonterminals


-e

x1 x2 x3 N…

x2 x3 x4 N…
xt

x3 x4 x5 ∂
ne

∂ ∂

xn'2 xn'1 xn N…

Continuing the process, the nonterminals that generate x1x2x3…xn can be determined as

Substring All producing nonterminals

x1 x2 x3…xn N…

If S is among the set of all producing nonterminals, then x can be generated by the CFG, otherwise not.

135
© Copyright Virtual University of Pakistan
Theory of Automata (CS402)

Example
Consider the following CFG in CNF
S→AA
A→AA
A→a
Let x = aaa. To determine whether x can be generated from the given CFG let x = x1x2x3 where x1 = x2 = x3 = a
According to CYK algorithm, the list of nonterminals producing single letter double letter substrings of x and
the string x itself, can be determined as follows

Substring All producing nonterminals

x1 = a A

om
x2 = a A

x3 = a A

x1 x2 S, A

.c
x2 x3 S, A

x = x1 x2 x3 S, A

ns
Since S is in the list of producing nonterminals, so aaa can be generated by the given CFG.

Parsing Techniques
Recall the CFG for arithmetic expression
tio
S→S+S|S*S|number
It was observed that the word 3+4*5 created ambiguity by considering its value either 23 or 35. To remove this
ambiguity, the CFG was modified to
ca
S→(S+S)|(S*S)|number
There arises a question that whether a new CFG can be defined without having parentheses with operator
hierarchy ( * before +)? The answer is yes. Following is the required PLUS'TIMES grammar
S→E, E→T+E|T, T→F*T|F, F→(E)|i
du

Where i stands for any identifier . number or of storage location name (variable). Following is the derivation
of i+i*i
S ⇒E
⇒T+E
⇒F+E
-e

⇒i+E
⇒i+T
⇒i+F*T
xt

⇒i+i*T
⇒i+i*F
⇒i+i*i
ne

Parsing of word
Definition
The process of finding the derivation of word generated by particular grammar is called parsing.
There are different parsing techniques, containing the following three
Top down parsing.
Bottom up parsing.
Parsing technique for particular grammar of arithmetic expression.

Top down parsing


Following is an example showing top down parsing technique

Example
Consider PLUS'TIMES grammar and a word i+i*i.
136
© Copyright Virtual University of Pakistan
Theory of Automata (CS402)

As can be observed from the name of top down parsing, the parsing starts from the nonterminal S and the
structure similar to that of total language tree is developed. The branches of the tree are extended till the
required word is found as a branch.
Some unwanted branches ( the branches that don't lead to the required word) are dropped. For the word i+i*i ,
the total language tree can be started as
S

E
Which can further be extended to

om
T+E T

F*T+E F+E F*T F

.c
ns
tio
ca
!

!
du

" $! !
")$ "%$
" $! !
"'$ "&$ " $
" $ "*$ "+$
-e

"($ "#$

Dropping the unwanted branches 1,3,5,7and 8


xt
ne

!
"%$
!
"&$ "#$ 137
© Copyright Virtual University of Pakistan
Theory of Automata (CS402)

Since first two letters in branches 2 and 6 are not that in i+i*i , so 2 and 6 can be dropped and using left most
derivation the nonterminal T is replaced as

om
.c
ns
! !
",$ "'-$ "'&$
tio "''$

since (9) gives more than five letters and (10) contains two + so (9) and (10) are dropped and left most
nonterminal F is replaced as
ca
du
-e
xt

!
ne

" $! ! " $
"'($ "'#$ "')$ "'%$

138
© Copyright Virtual University of Pakistan
Theory of Automata (CS402)

(13), (15) and (16) are again unwanted, so it can be written as

om
!

.c
!

! ! !

ns
!" $ !

The above tree confirms the required derivation


tio
S ⇒E
⇒T+E
⇒F+E
ca
⇒i+E
⇒i+T
⇒i+F*T
⇒i+i*T
du

⇒i+i*F
⇒i+i*i
Note
It can be noted that Bottom Up Parsing can be determined similar to that of Top Down Parsing with the change
-e

that in this case, the process is started with the given string and the tree is extended till S is obtained.
xt
ne

139
© Copyright Virtual University of Pakistan
Theory of Automata (CS402)

Theory of Automata

Lecture N0. 45
Reading Material

Introduction to Computer Theory Chapter 19

Summary
Turing machine, examples, DELETE subprogram, example, INSERT subprogram, example.

Turing machine
The mathematical models (FAs, TGs, PDAs) that have been discussed so far can decide whether a string is
accepted or not by them . these models are language identifiers. However, there are still some languages

om
which can’t be accepted by them there does not exist any FA or TG or PDA accepting any non'CFLs.
Alan Mathison Turing developed the machines called Turing machines, which accept some non'CFLs as well,
in addition to CFLs.

Definition
A Turing machine (TM) consists of the following

.c
An alphabet ∑ of input letters.
An input TAPE partitioned into cells, having infinite many locations in one direction. The input string is placed
on the TAPE starting its first letter on the cell i, the rest of the TAPE is initially filled with blanks ( ’s).

ns
Input TAPE
i ii iii iv
a b a ∝
tio
TAPE Head

A tape Head can read the contents of cell on the TAPE in one step. It can replace the character at any cell and
ca
can reposition itself to the next cell to the right or to the left of that it has just read. Initially the TAPE Head is at
the cell i. The TAPE Head can’t move to the left of cell i. the location of the TAPE Head is denoted by .

An alphabet Γ of characters that can be printed on the TAPE by the TAPE Head. Γ may include the letters of ∑.
du

Even the TAPE Head can print blank , which means to erase some character from the TAPE.
Finite set of states containing exactly one START state and some (may be none) HALT states that cause
execution to terminate when the HALT states are entered.
A program which is the set of rules, which show that which state is to be entered when a letter is read form the
TAPE and what character is to be printed. This program is shown by the states connected by directed edges
-e

labeled by triplet (letter, letter, direction). It may be noted that the first letter is the character the TAPE Head
reads from the cell to which it is pointing. The second letter is what the TAPE Head prints the cell before it
leaves. The direction tells the TAPE Head whether to move one cell to the right, R, or one cell to the left, L.
xt

Note
It may be noted that there may not be any outgoing edge at certain state for certain letter to be read from the
ne

TAPE, which creates nondeterminism in Turing machines. It may also be noted that at certain state, there can’t
be more than one out going edges for certain letter to be read from the TAPE. The machine crashes if there is
not path for a letter to be read from the TAPE and the corresponding string is supposed to be rejected.
To terminate execution of certain input string successfully, a HALT state must be entered and the corresponding
string is supposed to be accepted by the TM. The machine also crashes when the TAPE Head is instructed to
move one cell to the left of cell i.
Following is an example of TM

Example
Consider the following Turing machine

140
© Copyright Virtual University of Pakistan
Theory of Automata (CS402)

(a,a,R)
(b,b,R)
(a,a,R)
(b,b,R) ( , ,R)
1 START 2 3 4 HALT

(b,b,R)

Let the input string aba be run over this TM


Input TAPE
i ii iii iv

om
a b a ∝

TAPE Head
Starting from the START state, reading a form the TAPE and according to the TM program, a will be printed

.c
a will be replaced by a and the TAPE Head will be moved one cell to the right.
Which can be seen as

ns
Input TAPE
i ii iii iv
a b tio a ∝

TAPE Head
This process can be expressed as

1 2
ca
aba aba

At state 2 reading b, state 3 is entered and the letter b is replaced by b,


du

1 2 3
aba aba aba

At state 3 reading a, will keep the state of the TM unchanged. Lastly, the blank is read and is replaced by
-e

and the HALT state is entered. Which can be expressed as


1 2 3 3
HALT
xt

aba aba aba aba


Which shows that the string aba is accepted by this machine. It can be observed, from the program of the TM,
that the machine accepts the language expressed by (a+b)b(a+b)*.
ne

Theorem
Every regular language is accepted by some TM.
Example (b,b,R)
( , ,R) 2
5 HALT 1 START
(b,b,R)

(a,a,R) (a,a,R) (a,a,R)


(a,a,R)

(b,b,R)

3 4
(b,b,R)
141
© Copyright Virtual University of Pakistan
Theory of Automata (CS402)

Consider the EVEN'EVEN language. Following is a TM accepting the EVEN'EVEN language.

It may be noted that the above diagram is similar to that of FA corresponding to EVEN'EVEN language.
Following is another example

Example
Consider the following TM (a,a,R) (b,b,R)

(b,b,R) (a,a,L) 4
2 3
(b,a,R)
(a,*,R) (a,a,R)

om
( , ,R)
9 HALT 1 START 5

(*,*,R) ( , ,L)
(a,a,L)
(b,b,L)

.c
(a, ,L) (a, ,L) 6
8 7

ns
The string aaabbbaaa can be observed to be accepted by the above TM. It can also be observed that the above
TM accepts the non'CFL {anbnan}.

INSERT subprogram
tio
Sometimes, a character is required to be inserted on the TAPE exactly at the spot where the TAPE Head is
pointing, so that the character occupies the required cell and the other characters on the TAPE are moved one
cell right. The characters to the left of the pointed cell are also required to remain as such.
In the situation stated above, the part of TM program that executes the process of insertion does not affect the
function that the TM is performing. The subprogram of insertion is independent and can be incorporated at any
ca
time with any TM program specifying what character to be inserted at what location. The subprogram of
insertion can be expressed as

INSERT a
du

INSERT b
-e

INSERT #

The above diagrams show that the characters a,b and # are to be inserted, respectively. Following is an example
xt

showing how does the subprogram INSERT perform its function

Example
ne

If the letter b is inserted at the cell where the TAPE Head is pointing as shown below

∝ b X a b b X ∝

then, it is expressed as

∝ b X a b b X ∝

INSERT b

142
© Copyright Virtual University of Pakistan
Theory of Automata (CS402)

The function of subprogram INSERT b can be observed from the following diagram

∝ b X b a b b X ∝

Following is the INSERT subprogram

The subprogram INSERT


Keeping in view the same example of inserting b at specified location, to determine the required subprogram,
first Q will be inserted as marker at the required location, so that the TAPE Head must be able to locate the
proper cell to the right of the insertion cell. The whole subprogram INSERT is given as
(a,a,R)
2

om
(a,Q,R) (b,a,R)
(a,b,R)

.c
(b,Q,R) (b,b,R) ( ,a,R)
1 (a,X,R)
In 3
(X,a,R)

ns
(b,X,R)

(X,b,R) ( , b,R)
(X,Q,R) tio ( , X,R) 5
4
( , b,R) ( , ,L)
(X,X,R)
(b,b,L)
(Q, b,R)
ca
7 6 (a,a,L)
(X,X,L)
Out
du

It is supposed that machine is at state 1, when b is to be inserted. All three possibilities of reading a, b or X are
considered by introducing the states 2,3 and 4 respectively. These states remember what letter displaced during
the insertion of Q.
Consider the same location where b is to be inserted
-e

∝ b X a b b X ∝
xt

After reading a from the TAPE, the program replaces a by Q and the TAPE Head will be moved one step right.
Here the state 2 is entered. Reading b at state 2, b will be replaced by a and state 3 will be entered. At state 3, b
is read which is not replaced by any character and the state 3 will not be left.
ne

At state 3, the next letter to be read is X, which will be replaced by b and the state 4 will be entered. At state 4,
will be read, which will be replaced by X and state 5 will be entered. At state 5, will be read and without any
change state 6 will be entered, while TAPE Head will be moved one step left. The state 6 makes no change
whatever (except Q) is read at that state. However at each step, the TAPE Head is moved one step left. Finally,
Q is read which is replaced by b and the TAPE Head is moved to one step right.
Hence, the required situation of the TAPE can be shown as

∝ b X a b b X ∝

DELETE subprogram
143
© Copyright Virtual University of Pakistan
Theory of Automata (CS402)

Sometimes, a character is required to be DELETED on the TAPE exactly at the spot where the TAPE Head is
pointing, so that the other characters on the right of the TAPE Head are moved one cell left. The characters to
the left of the pointed cell are also required to remain as such.
In the situation stated above, the part of TM program that executes the process of deletion does not affect the
function that the TM is performing. The subprogram of deletion is independent and can be incorporated at any
time with any TM program specifying what character to be deleted at what location. The subprogram of deletion
can be expressed as

Example
If the letter a is to be deleted from the string bcabbc, shown below

∝ b c a b b c ∝

om
then, it is expressed as

∝ b c a b b X ∝

.c
DELETE

ns
The function of subprogram DELETE can be observed from the following diagram

∝ b c b b c ⋅ ⋅⋅
tio
(a,a,R)
(b,b,R)
ca
In (c,c,R)
1 2 3
(c, ,R) ( , ,L)
(b, ,R)
(a, ,R)
du

Following is the DELETE subprogram (c, ,L)


(b, ,L)

(a,a,L) (c,c,L)
(b,b,L)
(b,a,L)
-e

(c,b,L)
4 5 6
(a,b,L) (b,c,L)
xt

(a,c,L)
ne

( ,a,R) (c,a,L) ( ,c,R)


( ,b,R)

Out 7

The process of deletion of letter a from the string bcabbc can easily be checked, giving the TAPE situation as
shown below

∝ b c b b c ⋅ ⋅⋅

144
© Copyright Virtual University of Pakistan
CHAPTER 12

om
.c
ns
tio
DECIDABILITY
ca
du

In this part of the book we have laid the foundations of the Theory of Finite
Automata. The pictures and tables that we have called "machines" can actually
-e

be built out of electronic components and operate exactly as we have described.


Certain parts of a computer and certain aspects of a computer obey the rules
we have made up for FA's. We have not yet arrived, though, at the math-
xt

ematical model for a whole computer. That we shall present in Part III. But
before we leave this topic, we have some unfinished business to clear up.
ne

Along the way we asked some very basic questions that we deferred consid-
ering. We now face three of these issues.

1. How can we tell if two regular expressions define the same language?
2. How can we tell if two FA's are equivalent?
3. How can we tell if the language defined by an FA has finitely many or
infinitely many words in it?

In mathematical logic we say that a problem is effectively solvable if there


is an algorithm that provides the answer in a finite number of steps, no matter
what the particular inputs are. The maximum number of steps the algorithm
will take must be predictable before we begin to execute the procedure. For
example, if the problem was, "What is the solution to a quadratic equation?"

216
DECIDABILITY 217

then the quadratic formula provides an algorithm for calculating the answer
in a predetermined number of arithmetic operations: four multiplications, two
subtractions, one square root, and one division. The number of steps in the
algorithm is never greater than this no matter what the particular coefficients
of the polynomial are. Other suggestions for solving a quadratic equation (such
as "keep guessing until you find a number that satisfies the equation") that
do not guarantee to work in a fixed number of steps are not considered effective
solutions.

om
DEFINITION

An effective solution to a problem that has a yes or no answer is called a


decision procedure. A problem that has a decision procedure is called

.c
decidable. U

ns
We want to decide whether two regular expressions determine the exact
same language. We might, very simply, use the two expressions to generate
many words from each language until we find one that obviously is not in
io
the language of the other. To be even more organized, we may generate the
words in size order, smallest first. In practice, this method works fairly well
at

but there is no mathematical guarantee that we find such an obvious benchmark


word at any time in the next six years. Suppose we begin with the two expres-
c

sions:
du

a(a + b)* and (b + A)(baa + ba*)*.


t-e

It is obvious that all the words in the language represented by the first expres-
sion begin with the letter a and all the words in the language represented by
the second expression begin with the letter b. These expressions have no word
x

in common; and this fact is very clear. However, consider these two expres-
sions:
ne

(aa + ab + ba + bb)* and ((ba + ab)*(aa + bb)*)*

They both define the language of all strings over I = {a, b} with an even
number of letters. If we did not recognize this, how could we decide the
question of whether they are equivalent? We could generate many examples
of words from the languages each represents, but we would not find a dif-
ference. Could we then conclude that they are equivalent? It is logically pos-
sible that the smallest example of a word that is in one language but not in
the other has 96 letters. Maybe the smallest example has 2 million letters.
This is not an effective procedure, and it does not decide the problem.
The following two expressions are even less clear:

((b*a)*ab*)* and A +a(a + b)* + (a + b)*aa(a + b)*


218 AUTOMATA THEORY

They both define the language of all words that either start with an a or else
have a double a in them somewhere or else are null..The suggestion that we
should "interpret what the regular expressions mean and see if they are the
same," is, of course hopeless.
Before we answer the first major question of this chapter, let us note that
it is virtually the same as the second question. If we had a decision procedure
to determine whether two regular expressions were equivalent, we could use
it to determine whether two FA's were equivalent. First, we would convert
the FA's into regular expressions and then decide about the regular expressions.
The process of converting FA's into regular expressions is an effective pro-
cedure that we developed in the proof of Kleene's Theorem in Chapter 7. The

om
number of steps required can be predicted in advance based on the size of
the machine to be converted. Since the conversion process eliminates at least
one state with each step, a machine with 15 states will take at most 16 steps

.c
to convert into a regular expression (counting the step that creates a unique
- and a unique +).
ns
Similarly, if we had an effective procedure to determine whether two FA's
were equivalent, we could use it to decide the problem for regular expressions
by converting them into FA's.
io
Fortunately we have already developed all the algorithms necessary to decide
the "equivalency problem" for FA's and regular expressions. We need only
at

recognize how to apply them.


Given two languages L 1 and L 2 defined either by regular expressions or by
c

FA's, we have developed (in Chapter 10) the procedures necessary to produce
finite automata for the languages Ll', L2', L 1 f L2', and L 2 fl L1 '. Therefore,
du

we can produce an FA that accepts the language

(L, n L2') + (L2 n Li')


t-e

This machine accepts the language of all words that are in L, but n6t L2 or
else in L2 but not L1. If L, and L 2 are the same language, this machine cannot
x

accept any words. If this machine accepts even one word, then L, is not equal
ne

to L2; even if the one word is the null word. If L, is equal to L2, then the
machine for the language above accepts nothing at all.
To make this discussion into an effective decision procedure, we must show
that we can tell by some algorithm when an FA accepts no words at all. This
is not a very hard task, and there are several good ways to do it. We make
a big fuss about this since it is so simple that it might seem unimportant,
which is wrong. It is a basic question in its own right-not just as part of
the decidability of the equivalence of regular languages.
How to determine whether an FA accepts any words:

Method I Convert the FA into a regular expression. Every regular expression


defines some words. We can prove this by an algorithm. First delete
all stars. Then for each + we throw away the right half of the sum
DECIDABILITY 219
and the + sign itself. When we have no more *'s or + 's, we remove
the parentheses and we have a concatenation of a's, b's, and A's.
These taken together form a word. For example:

(a + A)(ab* + ba*)*(A + b*)*

becomes (after removing *'s)

(a + A)(ab + ba) (A + b)

om
which becomes (throwing away right halves)

(a)(ab)(A)

.c
which becomes (eliminating parentheses)

ns a ab A

which is the word


io
aab
at

This word must be in the language of the regular expression since


c

the operations of choosing * = power 1, and + = left half, are both


du

legal choices for forming words. Ifevery regular expression defines


at least one word, it seems at first glance that this means that every
FA must accept at least one word. How then could we ever show that
t-e

two languages are equal? If we first build an FA for the language

(L1 n L2') + (L 2 n LI')


x
ne

and then when we convert this machine into a regular expression, is


it not true that by the argument above we must find some word in the
language of the regular expression and therefore L, * L 2 no matter
what they are? No. The hole in this reasoning is that the process of
converting this FA into a regular expression breaks down. We come
down to the last step where we usually have several edges running
from - to + that we add together to form the regular expression.

ri

r73
220 AUTOMATA THEORY

However, when we get to this last step we suddenly realize that


there are no paths from - to + at all.
This could happen theoretically in three different ways: the machine
has no final states, such as this one:

a1 b a

om
or the final state is disconnected from the start state, as with this one:

a, b a, b

.c
ns
or the final state is unreachable from the start state, as with this one:
io
a, b
at

ab ab
c
du

We shall see later in this chapter which of these situations does


t-e

arise if the languages are actually equal.


Method 2 Examine the FA to see if there is any path from - to +. If there is
any path, then the machine must accept some words-for one, the
x

word that is the concatenation of the labels of the edges in the path
from - to + just discovered. In a large FA with thousands of states
ne

and millions of directed edges, it may be impossible to decide if there


is a path from - to + without the aid of an effective procedure. One
such procedure is this:

Step 1 Paint the start state blue.


Step 2 From every blue state follow each edge that leads out of it and paint the
connecting state blue, then delete this edge from the machine.
Step 3 Repeat Step 2 until no new state is painted blue, then stop.
Step 4 When the procedure has stopped, if any of the final states are painted
blue, then the machine accepts some words and, if not, it does not.
Let us look at this procedure at work on the machine
DECIDABILITY 221

baa

b
a

after Step 1:
a
a b
a a b

om
b a

.c
after Step 2: aia
aba
ns a b
io
at

b a

after Step 2 again:


c

a b
a
du

N sablueblue blue +
t-e

after Step 2 again:


x

ab
ne

blue 6+

No new states were painted blue this time, so the procedure stops and

no words.

While we were examining the second method we might have noticed that
Step 2 cannot be repeated more times than there are total states in the machine.
If the machine has N states, after N iterations of Step 2 either they are all
colored blue or we have already stopped. We can summarize this as a theorem.
222 AUTOMATA THEORY

THEOREM 15

Let F be an FA with N states. Then if F accepts any words at all it accepts


some word with N or fewer letters.

PROOF

The shortest path from - to + (if there is any) cannot contain a circuit
because if we go from - to state 7 and then around a circuit back to state

om
7 and then to + it would have been shorter to go from - to state 7 to +
directly. If there is a path from - to + without a circuit, then it can visit
each state at most one time. The path can then have at most N edges and
the word that generates it can have at most N letters. S

.c
ns
The proof actually shows that the shortest word must have at most N - I
letters, since if the start state is a final state, then the word A is accepted
and with N - I letters we can visit the other N - 1 states. The FA below
io
has four states, but it accepts no word with fewer than three letters, so we
see that the bound N - 1 is the best possible.
at

a, b
c
du
t-e

This gives us a third method for determining whether an FA accepts any


words.
x
ne

Method 3 Test all words with fewer than N letters by running them on the FA.
If the FA accepts none of them, then it accepts no words at all. There
are a predictable number of words to test, and each word takes a finite
predictable time to run, so this is an effective decision procedure.

These methods are all effective; the question of which is more efficient is
a whole other issue, one that we do not (often) raise in this book. As soon
as we know that there is at least one way to accomplish a certain task we
lose interest because our ultimate concern is the question, "What can be done
and what cannot?" The only motivation we have for investigating alternative
methods is that maybe they can be generalized to apply to new problems that
our original approach could not be extended to cover.
DECIDABILITY 223

EXAMPLE

Let us illustrate the effective decision procedure described above that deter-
mines whether two regular expressions are equivalent. We shall laboriously
execute the entire process on a very simple example. Let the two regular
expressions be:

r, = a* and r 2 = A + aa*

Luckily in this case we can understand that these two define the same language.

om
Let us see how the decision procedure proves this. Some machines for FAI,
FA1 ', FA2, and FA2 ' are shown below:

.c
a a, a ab

FA 1 P
nsFA q
io
at

ab ab

b b
c

FA 2 r , r2 FA 7 ý /
du

Va a b
t-e

r3 + S3
x

a a
ne

If we did not know how to produce these, algorithms in previous chapters


would show us how. We have labeled the states with the letters p, q, r, and
s for clarity. Instead of using the logical formula:

(L, fl L 2') + (L2 fl El')

we build our machine based on the equivalent Set Theory formula

(Ll' + L 2 )' + (L 2 ' + L 1)'

The machine for the first half of this formula is (FA1 ' + FA2)'
224 AUTOMATA THEORY
(I

q1 or r, q, or r3

V 1l2 or r2

ai. h

om
The machine for the second half is (FA2' + FA 1)'
(I

aa

.c
p, or s, 1), or , 3

ns
b h
io
at

p. or s.

a1, 1)
c
du

It was not an oversight that we failed to mark any of the states in these
two machines with a +. Neither machine has any final states. For (FAI' + FA2)'
to have a final state, the machine (FAI' + FA2) must have a nonfinal state.
t-e

The start state for this machine is (ql or r1 ). From there, if we read an a
we go to (ql or r 3 ), and if we read instead a b we go to (q2 or r2). If we
ever get to (q2 or r2) we must stay there. From (q, or r3) an input b takes
x

us to (q 2 or r 2) and an input a leaves us at (q, or r3 ). All in all, from -


we cannot get to any other combination of states, such as the potential (q2
ne

or rj) or (q, or r2). Now since q2 is a + and r, and r 3 are both +, all
three states (q, or rl), (ql or r 3 ), and (q 2 or r2) are all +, which means that
the complement has no final states.
The exact same thing is true for the machine for the second half of the
formula. Clearly, if we added these two machines together we would get a
machine with nine states and no final state. Since it has no final state, it
accepts no words and the two languages L, and L2 are equivalent. This ends
the decision procedure. There are no words in one language that are not in
the other, so the two regular expressions define the same language and are
equivalent. U
This example is a paradigm for the general situation. The machine for
(Ll' + L2)' accepts only those words in L, but not in L 2. If the languages
DECIDABILITY 225

are in fact equal, this machine will have no final states. The same will be
true for the machine for (L2' + L )'. It will never be necessary to combine
these two machines, since if either accepts a word, then L, * L2.
When we listed three ways that a machine could accept no words the first
way was that there be no final states and the second and third ways were
that the final states not be reachable from the start state. We counted these
situations separately. When we form a machine by adding two machines to-
gether, we do not usually bother describing the states that are not reachable
from the start state. The algorithm that we described in Chapter 7 never gets
to consider combinations of states of the component machines that are never
referred to. However, if we used a different algorithm, based on writing down

om
the whole table of possible combinations and then drawing edges between the
resultant states as indicated, we would, in this example, produce a picture
with a final state but it would be unreachable from the start state. In the

.c
example above, the full machine for (FA1 ' + FA2)' is this:

q, or r
3
ns rq2 or r
io
a1 a
a
at

(11 or r

b h
c

q2 Or r 3
du
x t-e
ne

The only final state, (ql or r2), cannot be reached from anywhere (in par-

ticular not from the start state (q, or r1 ). So the machine accepts no words.
We can summarize what we have learned so far in the following theorem.

THEOREM 16

(i) There is an effective procedure to decide whether a given FA accepts any


words.
(ii) There is an effective procedure to decide whether two FA's are equivalent.
(iii) There is an effective procedure to decide whether two regular expressions
are equivalent. N
226 AUTOMATA THEORY

Let us now answer our last question of decidability. How can we tell whether
an FA, or regular expression, accepts a finite language or an infinite language?
With regular expressions this is easy. The closure of any nonempty set,
whether finite or infinite, is itself infinite. Even the closure of one letter is
infinite. Therefore, if when building the regular expression from the recursive
definition we have ever had to use the closure operator, the resulting language
is infinite. This can be determined by scanning the expression itself to see
whether it contains the symbol *. If the regular expression does contain a *,
then the language is infinite. The one exception to this rule is A*, which is
just A. This one exception can however be very tricky. Of the two regular
expressions:

om
(A + a A*) (A* + A)* and (A + a A)* (A* + A)*

only the second defines an infinite language.

.c
If the regular expression does not contain a *, then the language is nec-
essarily finite. This is because the other rules of building regular expressions
ns
(any letter, sum, and product) cannot produce an infinite set from finite ones.
Therefore, as we could prove recursively, the result must be finite.
io
If we want to decide this question for an FA, we could first convert it to
a regular expression. On the other hand, there are ways to determine whether
an FA accepts an infinite language without having to perform the conversion.
c at

THEOREM 17
du

Let F be an FA with N states. Then


t-e

(i) If F accepts an input string w such that

N -- length (w) < 2N


x
ne

then F accepts an infinite language.

(ii) If F accepts infinitely many words, than F accepts some word w such that

N -- length (w) < 2N

PROOF

(i) If there is some word w with N or more letters, then by the second version
of the Pumping Lemma, we can break it into three parts:
w= xyz
DECIDABILITY 227

The infinitely many different words x yfn z for n = 1, 2, 3 . . . are all


accepted by F.
(ii) Now we are supposing that F does accept infinitely many words. Then it
must accept a word so large that its path must contain a circuit, maybe
several circuits. Each circuit can contain at most N states because F has
only N states in total. Let us change the path of this long word by keeping
the first circuit we come to and bypassing all the others. To bypass a circuit
means to come up to it, go no more than part way around it, and leave at
the first occurrence of the state from which the path previously exited.
This one-circuit path corresponds to some word accepted by F. The word

om
can have at most 2N letters, since at most N states are on the one circuit
and at most N states are encountered off that circuit. If the length of this
word is more than N, then we have found a word whose length is in the
range that the theorem specifies. If, on the other hand, the length of this

.c
word is less than N, we can increase it by looping around the one circuit
until the length is greater than N. The first time the length of the word (and
ns
path) becomes greater than N, it is still less than 2N, since we have increased
the word only by the length of the circuit, which is less than N. Eventually,
io
we come to an accepted word with a length in the proper range. U
at

EXAMPLE
c

Consider this example:


du
x t-e
ne

The first circuit is 2-3-4. It stays. The second circuit is 5-6-7-8. It is bypassed
to become 5-6-7-9.
The path that used to be

1-2-3-4-2-3-5-6-7-8-5-6-7-8-5-6-7-9-10

becomes

1-2-3-4-2-3-5-6-7-9-10

This demonstrates the existence of a simple one-circuit path in any FA that


accepts infinitely many words. E
228 AUTOMATA THEORY

This theorem provides us with an effective procedure for determining whether


F accepts a finite language or an infinite language. We simply test the finitely
many strings with lengths between N and 2N by running them on the machine
and seeing if any reach a final state. If none does, the language is finite.
Otherwise it is infinite.

THEOREM 18

There is an effective procedure to decide whether a given FA accepts a finite


or an infinite language.

om
PROOF

.c
If the machine has N states and the alphabet has m letters, then in total there
are
mN + mN+
ns
-+- mN+2 +k .. ÷m2N-I
+
tio
different input strings in the range
ca

N -- length of string < 2N.

We can test them all by running them on the machine. If any are accepted,
du

the language is infinite. If none are accepted, the language is finite. 0

It may often be more efficient to convert the FA to a regular expression.


-e

However, suppose the FA is an actual physical machine that is sitting in front


of us. We may not know its exact structure inside or it may be extremely
xt

complicated. Even though we have an effective procedure for converting it


into a regular expression, we may not have the capacity (storage or time or
ne

inclination) to do so. Yet there might be an automatic way of feeding in all


combinations of letters in the interesting range. Even if this situation never
arises, the theorem we have covered is a prototype for decidability of more
complex questions.
In the case where the machine has 3 states and the alphabet has 2 letters,
the number of strings we have to test is

2' + 2 4 + 25 = 8 + 16 + 32 = 56

which is not too bad. However, an FA with 3 states can be converted into
a regular expression in very few steps.
DECIDABILITY 229

PROBLEMS

Show by the method described in this chapter that the following pairs of FA's
are equivalent.

FA,1 +Y FA,
aa

om
b~~

.c
a,
ns
tio
2. FA
1 FA2 b
a ba
ca

h a FA a a
du

aa ab

b alb a
2. FAl ~ a FA, b ,
-e

b a
xt

4. K4 al
ne

aFA

b a a,a

a +
230 AUTOMATA THEORY
5. FA,
h FA2
+ b+ a

a
a b

om
6. Using the method of intersecting each machine with the complement of
the other show that:

.c
-V2 '2

a a
ns
and a
io
a &a
h
at

C&
c
du

do not accept the same language.


t-e

7. Using the method of intersecting each machine with the complement of


the other, show that:
x

FA1 FA2
ne

a a

a a (I
'V and

do not accept the same language.

8. List the 56 strings that will suffice to test whether a 3-state FA over
= {a, b} has a finite language.
DECIDABILITY 231

By using blue paint, determine which of the following FA's accept any
words.

9. a, b

om
a.

.c
a, b

ns
a. b
tio
10.
ca
du
-e

a. 6
xt

11.b
ne

a a, b

a
232 AUTOMATA THEORY

12.

Which of the following FA's accepts a finite language and which an

om
infinite one?

13. (i) b

.c
aa ns
a b
tio
aa

h
ca

a, b
du

(ii) (.,
/,
-e
xt
ne

(iii) b

1 1
DECIDABILITY 233

(iv)

a Z
b

ba

om
a

14. Without converting it into a regular expression or an FA, give an al-

.c
gorithm that decides whether a TG accepts any words.
15. Without converting it into a regular expression or an FA, give an al-
gorithm that decides whether
or infinite.
ns
the language of an NFA is empty, finite,

16. Do the same as Problem 15 for NFA-A's (see Chapter 8, Problem 9).
io
Be careful. The machine
at

a
c
du

has an infinite language, whereas the machine


t-e

A
x
ne

has a one-word language.


17. Consider the following simplified algorithm to decide if an FA with
exactly N states has an empty language.

Step 1 Take the edges coming out of each. final state and turn them into
loops going back to the state they started from.
Step 2 Relabel all edges with the letter x. (We now have an NFA.)
Step 3 The original FA has a nonempty language if and only if this new
NFA accepts the word x"v.

Illustrate this algorithm and prove it always works.


Is this an effective procedure?
234 AUTOMATA THEORY

18. By moving the start state, construct a decision procedure to determine


whether a given FA accepts at least one word that starts with an a.

19. (i) Construct a decision procedure to determine whether a given FA


accepts at least one word that contains the letter b.
(ii) Construct a decision procedure to determine whether a given FA
accepts some words of even length.

20. Given two regular expressions r, and r 2, construct a decision procedure


to determine whether the language of r1 is contained in the language of

om
r 2.

.c
ns
io
c at
du
x t-e
ne
PART II

om
.c
ns
tio
ca
du
-e
xt
ne

PUSHDOWN
AUTOMATA
THEORY
CHAPTER 13

om
.c
ns
tio
CONTEXT-FREE
ca

GRAMMARS
du

Our overall goal is the study of computers: What are they? Of what are they
-e

composed? What can they and what can't they do? What will they be able
to do in ten thousand years? The machines of Part I could not do much. This
is because they did not have enough components. We shall soon rectify this
xt

shortcoming. In Part II we shall connect the topics of Part I directly to some


of the problems in Computer Science with which we are already familiar.
ne

The earliest computers, just like the programmable hand calculators of the
1970s, accepted no instructions except those in their own machine language
or (almost equivalently) in their own assembly language. Every procedure, no
matter how complicated, had to be spelled out in the crudest set of instructions:
LOAD this, STORE that, ADD the contents of these two registers, and so
forth. It could take dozens of these primitive instructions to do anything useful.
In particular, it could take quite a few to evaluate one complicated arithmetic
expression.
For example, in statistics, we often want to calculate something like

S(7 - 9)2 + (9 - 9)2 + (11 - 9)2


S23 2

237
238 PUSHDOWN AUTOMATA THEORY

Unfortunately, we cannot just throw this expression into the machine. One
of the first hurdles the inventors of computers had to overcome was the problem
of formula display. A human sees the picture of this formula drawn on five
different levels. A 2 is on the bottom. Above it is a division line. Above
that are the three subtractions. Above them and to the right are exponents
indicating that these subtractions are to be squared. Above all of that and to
the left is the square root sign. We must check the tail on the left of the
square root sign to be sure that the bottom 2 is included in the root taking.
This converts to numerous assembly language instructions, especially since
square roots are not standard hard-wired instructions on most computers.
The problem of making computers attractive to a wide variety of potential

om
users (a problem important to those people who wanted to sell computers)
clearly required that some "higher-level" language be invented-a language in
which one mathematical step, such as evaluating the formula above, could be

.c
converted into one single computer instruction.
Because of the nature of early computer input devices, such as keypunches,
ns
paper tape, magnetic tape, and typewriters, it was necessary to develop a way
of writing this expression in one line of standard typewriter symbols. Some
few new symbols could be invented if necessary, but the whole expression
tio
had to be encoded in a way that did not require a five-level display or depend
on the perception of spatial arrangement. Formulas had to be converted into
linear strings of characters.
ca

Several of the adjustments that had to be made were already in use in the
scientific literature for various other reasons. For example, the use of the slash
as a divide sign was already accepted by the mathematical public. Most pub-
du

lishers have special symbols for the popular fractions such as A and 4, but
eight-elevenths was customarily written as 8/11.
Still, before the days of the computer no one would ever have dreamed of
-e

writing a complicated compound fraction such as


xt

I
-+9
2
ne

8 5
4 + 8- +---
21 3
3+- 1
2

in the parentheses-laden one-line notation

((1/2) + 9)/(4 + (8/21) + (5/(3 + (1/2))))

The most important reason for not using the one-line version unless nec-
essary is that in the picture version we can easily see that the number we are
looking at is a little more than 9 divided by a little more than 5, so it obviously
has a value between 1 and 2. Looking at the parentheses notation, it is not
CONTEXT-FREE GRAMMARS 239

even obvious which of the slash marks separates the numerator from the de-
nominator of the major division.
The main advantage of the one-level version is that we can feed it into a
computer. At some time in the future a computer might be able to digest the
multilevel expression and the one-line nightmare can be forgotten forever.
How can a computer scan over this one-line string of typewriter characters
and figure out what is going on? That is, how can a computer convert this
string into its personal language of LOAD this, STORE that, and so on?
The conversion from a high-level language into assembler language code
is done by a program called the compiler. This is a superprogram. Its data
are other people's programs. It processes them and prints out an equivalent

m
program written in assembler language. To do this it must figure out in what
order to perform the complicated set of arithmetic operations that it finds writ-

co
ten out in the one-line formula. It must do this in a mechanical, algorithmic
way. It cannot just look at the expression and understand it. Rules must be
given by which this string can be processed-rules, perhaps, like those the

.
machines we discussed in Part I could follow. ns
Consider the language of valid input strings over the alphabet
tio
0123456789+ -
ca

Could some FA, directed from state to state by the input of a one-line formula,
print out the equivalent assembly-language program? This would have to be
an output FA like the Mealy or Moore machines for which the output alphabet
du

would be assembler language statements.


The solution is not quite as easy as that, but it is within our reach.
We also want our machine to be able to reject strings of symbols that make
-e

no sense as arithmetic expressions, such as "((9)+". This input string should


not take us to a final state in the machine. However, we cannot know that
xt

this is a bad input string until we have reached the last letter. If the + were
changed to a ), the formula would be valid. An FA that translated expressions
ne

into instructions as it scanned left to right would already be turning out code
before it realized that the whole expression is nonsense.
Before we try to build a compiling machine, let us return to the discussion
of what is and what is not a valid arithmetic expression as defined in Chapter
3 by recursive definition.
All valid arithmetic expressions can be built up from the following rules.
Anything not produceable from these rules is not a valid arithmetic expression:

Rule 1 Any number is in the set AE.


Rule 2 If x and y are in AE then so are:

(x) -(x) (x + y) (x - y) (x*y) (x/y) (x**y)


240 PUSHDOWN AUTOMATA THEORY
This time we have included parentheses around every component factor. This
avoids the ambiguity of expressions like 3 + 4 * 5 and 8/4/2 by making them
illegal. We shall present a better definition of this set later.
First we must design a machine that can figure out how a given input string
was built up from these basic rules. Then we should be able to translate this
sequence of rules into an assembler language program, since all of these rules
are pure assembler language instructions (with the exception of exponentiation,
which presents a totally different problem; but since this is not a course in
compiler design, we ignore this embarrassing fact).
For example, if we present the input string

om
((3 + 4) * (6 + 7))

and the machine discovers that the way this can be produced from the rules

.c
is by the sequence

ns
3 is in AE
4 is in AE
(3 + 4) is in AE
tio
6 is in AE
7 is in AE
ca

(6 + 7) is in AE
((3 + 4) * (6 + 7)) is in AE
du

we can automatically convert this into


-e

LOAD 3 in Register 1
LOAD 4 in Register 2
xt

ADD the contents of Register 2 into Register 1


ne

LOAD 6 in Register 3
LOAD 7 in Register 4
ADD the contents of Register 3 into Register 4
MULTIPLY Register 1 by Register 4

or some such sequence of instructions depending on the architecture of the


particular machine (not all computers have so many arithmetic registers) and
the requirements of the particular assembler language (multiplication is not
always available).
The hard part of the problem is to figure out by mechanical means how
the input string can be produced from the rules. The second part-given the
sequence of rules that create the expression to write a computer program to
duplicate this process-is easy.
CONTEXT-FREE GRAMMARS 241

The designers of the first high-level languages realized that this problem is
analogous to the problem humans have to face hundreds of times every day
when they must decipher the sentences that they hear or read in English. Here
we have again the ever-present parallelism: Recognizing what a computer lan-
guage instruction is saying is analogous to recognizing what a sentence in a
human language means.
Elementary School used to be called Grammar School because one of the
most important subjects taught was English Grammar. A grammar is the set
of rules by which the valid sentences in a language are constructed. Our ability
to understand what a sentence means is based on our ability to understand

om
how it could be formed from the rules of grammar. Determining how a sen-
tence can be formed from the rules of grammar is called parsing the sentence.
When we hear or read a sentence in our native language, we do not go
through a conscious act of parsing. Exactly why this is the case is a question

.c
for other sciences. Perhaps it is because we learned to speak as infants by a
trial and error method that was not as mathematical and rigorous as the way
ns
in which we learn foreign languages later in life. When we were born we
spoke no language in which the grammar of our native tongue could be de-
tio
scribed to us. However, when we learn a second language the rules of grammar
for that language can be explained to us in English. How we can possibly
learn our first language is a problem discussed by linguists, psychologists,
ca

philosophers, and worried parents. It does not concern us here because when
we come to teach a computer how to understand the languages it must rec-
ognize, a few printed circuits will do the trick.
du

Even though human languages have rules of grammar that can be stated
explicitly, it is still true that many invalid sentences, those that are not strictly
speaking grammatical, can be understood. Perhaps this is because there are
-e

tacit alternative rules of grammar that, although not taught in school, never-
theless are rules people live by. But this will not concern us either. No com-
xt

puter yet can forgive the mess, "Let x equal two times the radius times that
funny looking Greek letter with the squiggly top that sounds like a pastry,
ne

you know what I mean?" The rules of computer language grammar are pre-
scriptive-no ungrammatical strings are accepted.
Since the English word "grammar" can mean the study of grammar as well
as the set of rules themselves, we sometimes refer to the set of rules as forming
a "generative grammar." This emphasizes the point that from them and a dic-
tionary (the alphabet) we can generate all the sentences (words) in the language.
Let us look at the rule in English grammar that allows us to form a sentence
by juxtaposing a noun and a verb (assuming that the verb is in the correct
person and number). We might produce

Birds sing.

However, using the same rule might also produce

Wednesday sings. or Coal mines sing.


242 PUSHDOWN AUTOMATA THEORY

If these are not meant to be poetical or metaphoric, they are just bad sentences.
They violate a different kind of rule of grammar, one that takes into account
the meaning of words as well as their person, number, gender, and case.
Rules that involve the meaning of words we call semantics and rules that
do not involve the meaning of words we call syntax. In English the meaning
of words can be relevant but in arithmetic the meaning of numbers is rarely
cataclysmic. In the high-level computer languages, one number is as good as
another. If

X=B+9

m
is a valid formulation then so are

co
X = B + 8 X = B + 473 X = B + 9999

So long as the constants do not become so large that they are out of range,

.
ns
and we do not try to divide by 0, and we do not try to take the square root
of a negative number, and we do not mix fixed-point numbers with floating-
point numbers in bad ways, one number is as good as another. It could be
tio
argued that such rules as "thou shalt not divide by zero" as well as the other
restrictions mentioned are actually semantic laws, but this is another interesting
point that we shall not discuss.
ca

In general, the rules of computer language grammar are all syntactic and
not semantic.
There is another way in which the parsing of arithmetic expressions is easier
du

than the parsing of English sentences. To parse the English sentence, "Birds
sing." it is necessary to look up in a dictionary whether "birds" is a noun or
a verb. To parse the arithmetic expression "(3 + 5)*6" it is not necessary to
-e

know any other characteristics of the numbers 3, 5, and 6. We shall see more
differences between simple languages and hard languages as we progress through
xt

Part II.
Let us go back to the analogy between computer languages and English.
ne

Some of the rules of English grammar are these:

1. A sentence can be a subject followed by a predicate.


2. A subject can be a noun-phrase.
3. A noun-phrase can be an adjective followed by a noun-phrase.
4. A noun-phrase can be an article followed by a noun-phrase.
5. A noun-phrase can be a noun.
6. A predicate can be a verb followed by a noun-phrase.
7. A noun can be:
apple bear cat dog
CONTEXT-FREE GRAMMARS 243

8. A verb can be:


eats follows gets hugs
9. An adjective can be:
itchy jumpy
10. An article can be:
a an the

Let us, for the moment, restrict the possibility of forming sentences to the
laws stated above. Within this small model of English there are hundreds of

om
sentences we can form. For example:

The itchy bear hugs the jumpy dog.

.c
The method by which this sentence can be generated is outlined below:

1. sentence => subject predict


ns Rule 1
2. > noun-phrase predicate Rule 2
tio
3. :: noun-phrase verb noun-phrase Rule 6
4. > article noun-phrase verb noun-phrase Rule 4
ca

5. > article adjective noun-phrase verb noun-phrase Rule 3


6. > article adjective noun verb noun-phrase Rule 5
du

7. > article adjective noun verb article noun-phrase Rule 4


8. > article adjective noun verb article adjective noun-phrase Rule 3
9. > article adjective noun verb article adjective noun Rule 5
-e

10. > the adjective noun verb article adjective noun Rule 10
11. > the itchy noun verb article adjective noun Rule 9
xt

12. > the itchy bear verb article adjective noun Rule 7
13. => the itchy bear hugs article adjective noun Rule 8
ne

14. > the itchy bear hugs the adjective noun Rule 10
15. > the itchy bear hugs the jumpy noun Rule 9
16. = the itchy bear hugs the jumpy dog Rule 7

The arrow indicates that a substitution was made according to the rules of
grammar stated above.
A law of grammar is in reality a suggestion for possible substitutions. What
happened above is that we started out with the initial symbol Sentence. We
then applied the rules for producing sentences listed in the generative grammar.
In most cases we had some choice in selecting which rule we wanted to apply.
244 PUSHDOWN AUTOMATA THEORY

There is a qualitative distinction between the word "noun" and the word "bear."
To show this we have underlined the words that stand for parts of speech and
are not to be considered themselves as words for the finished sentences. Of
course, in the complete set of rules for English the words "verb," "adjective,"
and so on are all perfectly good words and would be included in our final
set of rules as usable words. They are all nouns. But in this model the term
verb is a transitory place holder. It means, "stick a verb here." It must even-
tually be replaced to form a finished sentence.
Once we have put in the word "bear" we are stuck with it. No rule of
grammar says that a bear can be replaced by anything else. The words that
cannot be replaced by anything are called terminals. Words that must be

om
replaced by other things we call nonterminals. We will give a more general
definition of this shortly.
Midway through the production procedure we developed the sentence into
as many nonterminals as it was going to become.

.c
ns
9. article adjective noun verb article adjective noun

From this point on the procedure was only one of selecting which terminals
tio
were to be inserted in place of the nonterminals. This middle stage in which
all the terminals are identified by their nonterminal names is the "grammatical
parse" of the sentence. We can tell what noun each adjective modifies because
ca

we know how it got into the sentence in the first place. We know which
noun-phrase produced it. "Itchy" modifies "bear" because they were both in-
troduced in Step 5 by application of Rule 3.
du

There is an element of recursive definition lurking in these rules. We have


allowed a noun-phrase to be an adjective followed by a noun-phrase. This
could lead to:
-e

noun-phrase 4 adjective noun-phrase


xt

Sadjective adjective noun-phrase


ne

> adjective adjectiv adjective noun-phrase


4 adjectiv adjective adjective noun
> itchy adjective adjective noun
4 itchy itchy adjective noun
4 itchy itchy itchy noun
4 itchy itchy itchy bear

If we so desired, we could produce fifty itchy's. Using the Kleene closure


operator we could write

noun-phrase 4 adjective* noun


CONTEXT-FREE GRAMMARS 245

But now we are getting ahead of ourselves.


The rules we have given above for this simplified version of English allow
for many dumb sentences such as:

Itchy the apple eats a jumpy jumpy jumpy bear.

Because we are not considering the limitations of semantics, diction, or good


sense, we must consider this string of terminals as a good sentence. This is
what we mean by the phrase "formal language," which we used in Part I. It
is a funny phrase because it sounds as if we mean the stuffy language used

m
in diplomatic circles. In reality it means that any string of symbols satisfying
the rules of grammar (syntax alone) is as good as any other. The word "formal"

co
here means "strictly formed by the rules," not "highly proper." The Queen of
England is unlikely to have made the remark above about Itchy the apple.
We can follow the same model for defining arithmetic expressions. We can

.
write the whole system of rules of formation as the list of possible substitutions
shown below: ns
tio
Start -- (AE)
AE-- (AE + AE)
ca

AE --> (AE - AE)


AE- (AE *AE)
du

AE -(AE AE)
AE ---> (AE ** AE)
-e

AE -- (AE)
AE - (AE)
xt

--

AE -- ANY-NUMBER
ne

Here we have used the word "Start" to begin the process, as we used the
word "Sentence" in the example of English. Aside from Start, the only other
nonterminal is AE. The terminals are the phrase "any number" and the symbols
+ - * /** ()

Either we could be satisfied that we know what is meant by the words "any
number" or else we could define this phrase by a set of rules, thus converting
it from a terminal into a nonterminal.

Rule 1 ANY-NUMBER ---> FIRST-DIGIT


Rule 2 FIRST-DIGIT ---> FIRST-DIGIT OTHER-DIGIT
246 PUSHDOWN AUTOMATA THEORY
Rule3 FIRST-DIGIT--* 123456789
Rule4 OTHER-DIGIT-->0 123456789

,Rules 3 and 4 offer choices of terminals. We put spaces between them to


indicate "choose one," but we soon shall introduce another disjunctive symbol.
We can produce the number 1066 as follows:

Rule 1 ANY-NUMBER > FIRST-DIGIT


Rule 2 > FIRST-DIGIT OTHER-DIGIT

om
Rule 2 • FIRST-DIGIT OTHER-DIGIT
OTHER-DIGIT
Rule 2 • FIRST-DIGIT OTHER-DIGIT

.c
OTHER-DIGIT OTHER-DIGIT
Rules 3 and 4 ns • 1066

Here we have made all our substitutions of terminals for nonterminals in


tio
one swoop, but without any possible confusion. One thing we should note
about the definition of AE is that some of the grammatical rules involve both
terminals and nonterminals together. In English, the rules were either of the
ca

form

one Nonterminal string of Nonterminals


du

or
-e

one Nonterminal - choice of terminals


xt

In our present study, we shall see that the form of the grammar has great
significance.
ne

The sequence of applications of the rules that produces the finished string
of terminals from the starting symbol is called a derivation. The grammatical
rules are often called productions. They all indicate possible substitutions. The
derivation may or may not be unique, by which we mean that by applying
productions to the start symbol in two different ways we may still produce
the same finished product. (See Problem 6 below.)

We are now ready to define the general concept of which all these examples
have been special cases. We call this new structure a context-free grammar
or CFG. The full meaning of the term "context-free" will be made clear later.
The concept of CFG's was invented by the linguist Noam Chomsky in 1956.
CONTEXT-FREE GRAMMARS 247

Chomsky gave several mathematical models for languages, and we shall see
more of his work later.

DEFINITION

A context-free grammar, called a CFG, is a collection of three things:

1 An alphabet Y of letters called terminals from which we are going to


make strings that will be the words of a language.

m
2 A set of symbols called nonterminals, one of which is the symbol S,
standing for "start here."

co
3 A finite set of productions of the form

.
one nonterminal -) finite string of terminals and/or nonterminals
ns
where the strings of terminals and nonterminals can consist of only terminals
or of only nonterminals, or any mixture of terminals and nonterminals or even
tio
the empty string. We require that at least one production has the nonterminal
S as its left side. 0
ca

So as not to confuse terminals and nonterminals, we always insist that


nonterminals be designated by capital letters while terminals are usually des-
ignated by lowercase letters and special symbols.
du
-e

DEFINITION
xt

The language generated by a CFG is the set of all strings of terminals that
can be produced from the start symbol S using the productions as substitutions.
ne

A language generated by a CFG is called a context-free language, abbreviated


CFL. U
There is no great uniformity of opinion among experts about the terminology
to be used here. The language generated by a CFG is sometimes called the
language defined by the CFG, or the language derived from the CFG, or
the language produced by the CFG. This is similar to the problem with regular
expressions. We should say "the language defined by the regular expression"
although the phrase "the language of the regular expression" has a clear mean-
ing. We usually call the sequence of productions that form a word a derivation
or a generation of the word.
248 PUSHDOWN AUTOMATA THEORY

EXAMPLE

Let the only terminal be a.


Let the productions be:

PROD I S---) aS
PROD2 S--->A

If we apply Production 1 six times and then apply Production 2, we generate


the following:

om
S4 aS
> aaS
= aaaS

.c
> aaaaS
>
>
ns
aaaaaS
aaaaaaS
> aaaaaaA
tio
= aaaaaa

This is a derivation of a6 in this CFG. The string a" comes from n ap-
ca

plications of Production 1 followed by one application of Production 2. If we


apply Production 2 without Production 1, we find that the null string is itself
du

in the language of this CFG. Since the only terminal is a it is clear that no
words outside of a* can possibly be generated. The language generated by
this CFG is exactly a*. 0
-e

In the examples above, we used two different arrow symbols. The symbol
xt

"--->" we employ in the statement of the productions. It means "can be replaced


by", as in S -- aS. The other arrow symbol "4" we employ between the
unfinished stages in the generation of our word. It means "can develop into"
ne

as in aaS 4 aaaS. These "unfinished stages" are strings of terminals and non-
terminals that we shall call working strings.
Notice that in this last example we have both S - aS as a production in
the abstract and S 4 aS as the first step in a particular derivation.

EXAMPLE

Let the only terminal be a.


Let the productions be:

PROD S -" SS
PROD2 S-*a
PROD3 S- A
CONTEXT-FREE GRAMMARS 249
In this language we can have the following derivation.

S :: 55

SSaS
SSaSS
•AaSS
•AaaS
•AaaA

m
= aa

co
The language generated by this set of productions is also just the language
a*, but in this case the string aa can be obtained in many (actually infinitely
many) ways. In the first example there was a unique way to produce every

.
ns
word in the language. This also illustrates that the same language can have
more than one CFG generating it. Notice above that there are two ways to
go from SS to SSS-either of the two S's can be doubled. U
tio
In the previous example the only terminal is a and the only nonterminal
is S. What then is A? It is not a nonterminal since there is no production of
ca

the form
A --> something
du

Yet it is not a terminal since it vanishes from the finished string AaaA = aa.
As always, A is a very special symbol and has its own status. In the definition
-e

of a CFG we said a nonterminal could be replaced by any string of terminals


and/or nonterminals even the empty string. To replace a nonterminal by A is
to delete it without leaving any tangible remains. For the nonterminal N the
xt

production
ne

N ----> A

means that whenever we want, N can simply be deleted from any place in
a working string.

EXAMPLE

Let the terminals be a and b, let the only nonterminal be S, and let the
productions be

PROD I S - aS
PROD2 S- bS
PROD3 S--->a
PROD 4 S -- > b
250 PUSHDOWN AUTOMATA THEORY
We can produce the word baab as follows:

S > bS (by PROD 2)


> baS (by PROD 1)
> baaS (by PROD 1)
> baab (by PROD 4)

The language generated by this CFG is the set of all possible strings of the
letters a and b except for the null string, which we cannot generate.
We can generate any word by the following algorithm:

om
At the beginning the working string is the start symbol S. Select a word to
be generated. Read the letters of the desired word from left to right one at
a time. If an a is read that is not the last letter of the word, apply PROD 1

.c
to the working string. If a b is read that is not the last letter of the word,
apply PROD 2 to the working string. If the last letter is read and it is an a,
ns
apply PROD 3 to the working string. If the last letter is read and it is a b,
apply PROD 4 to the working string.
tio
At every stage in the derivation before the last, the working string has the
form
ca

(string of terminals) S

At every stage in the derivation, to apply a production means to replace


du

the final nonterminal S. Productions 3 and 4 can be used only once and only
one of them can be used. For example, to generate babb we apply in order
Prods 2, 1, 2, 4, as below:
-e

S > bS = baS > babS > babb U


xt
ne

EXAMPLE

Let the terminals be a and b. Let the nonterminals be S, X, and Y. Let the
productions be:

S-- X
S- y
X-*A
Y-- aY
Y-- bY
Y-- a
Y-- b
CONTEXT-FREE GRAMMARS 251
All the words in this language are either of type X, if the first production in
their derivation is

S --> X
or of type Y, if the first production in their derivation is

S-- Y

The only possible continuation for words of type X is the production

om
X-->A

Therefore A is the only word of type X.

.c
The productions whose left side is Y form a collection identical to the
productions in the previous example except that the start symbol S has been
ns
replaced by the symbol Y. We can carry on from Y the same way we carried
on from S before. This does not change the language generated, which contains
only strings of terminals. Therefore, the words of type Y are exactly the same
io
as the words in the previous example. That means, any string of a's and b's
except the null string can be produced from Y as these strings were produced
at

before from S.
Putting the type X and the type Y words together, we see that the total
c

language generated by this CFG is all strings of a's and b's, null or otherwise.
du

The language generated is (a + b)*. U


t-e

EXAMPLE

Let the terminals be a and b. Let the only nonterminal be S.


x

Let the productions be


ne

S-- aS
S -- bS
S-. a
S--- b
S- A

The word ab can be generated by the derivation

S z aS
> abS
> abA
= ab
252 PUSHDOWN AUTOMATA THEORY
or by the derivation

S > aS
: ab

The language of this CFG is also (a + b)*, but the sequence of productions
that is used to generate a specific word is not unique.
If we deleted the third and fourth productions, the language generated would
be the same. U

om
.c
EXAMPLE

ns
Let the terminals be a and b, let the nonterminals be S and X, and let the
productions be
io
S -- XaaX
X-- aX
at

X-- bX
c

X--A
du

We already know from the previous example that the last three productions
will allow us to generate any word we want from the nonterminal X. If the
t-e

nonterminal X appears in any working string we can apply productions to turn


it into any word we want. Therefore, the words generated from S have the
form
x

anything aa anything
ne

or

(a + b)*aa(a + b)*

which is the language of all words with a double a in them somewhere.


For example, to generate baabaab we can proceed as follows:

S > XaaX : bXaaX > baXaaX => baaXaaX > baabXaaX


> baabAaaX = baabaaX 4 baabaabX 4 baabaabA = baabaab

There are other sequences that also derive the word baabaab. U
CONTEXT-FREE GRAMMARS 253

EXAMPLE

Let the terminals be a and b, let the nonterminals be S, X, and Y and let
the productions be

S ---> XY
X-- aX
X-- bX
X- a
Y-- Ya

om
Y-- Yb
Y a

.c
What can be derived from X? Let us look at the X productions alone.

ns
X-- aX
X-- bX
io
X- a
at

Beginning with the nonterminal X and starting a derivation using the first two
productions we always keep a nonterminal X on the right end. To get rid of
c

the X for good we must eventually replace it with an a by the third production.
We can see that any string of terminals that comes from X must end in an
du

a and any words ending in an a can be derived from X. For example, to


derive the word babba from X we can proceed as follows:
t-e

X > bX > baX > babX > babbX > babba

Similarly, the words that can be derived from Y are exactly those that begin
x

with an a. To derive abbab, for example, we can proceed:


ne

Y z Yb = Yab = Ybab > Ybbab > abbab

A Y always stays on the left end until it is replaced by an a. When an


X part is concatenated with a Y part, a double a is formed.
We can conclude that starting from S we can derive only words with a
double a in them, and all of these words can be derived.
For example, to derive babaabb we know that the X part must end at the
first a of the double a and that the Y part must begin with the second a.

S > XY 4 bXY > baXY 4 babXY 4 babaY


4 babaYb 4 babaYbb 4 babaabb U
254 PUSHDOWN AUTOMATA THEORY

EXAMPLE

Let the terminals be a and b. Let the three nonterminals be S, BALANCED,


and UNBALANCED. We treat these nonterminals as if they were each a single
symbol and nothing more confusing. Let the productions be

S-- SS
S -- BALANCED S
S -- S BALANCED
S--A

om
S -- UNBALANCED S UNBALANCED
BALANCED -- aa
BALANCED -- bb

.c
UNBALANCED -- ab
UNBALANCED ba
--
ns
We shall show that the language generated from these productions is the set
of all words with an even number of a's and an even number of b's. This
tio
is our old friend, the language EVEN-EVEN.
To prove this we must show two things: that all the words in EVEN-EVEN
ca

can be generated from these productions and that every word generated from
these productions is in fact in the language EVEN-EVEN.
First we show that every word in EVEN-EVEN can be generated by these
du

productions. From our earlier discussion of the language EVEN-EVEN we


know that every word in this language can be written as a collection of substr-
ings of
-e

type aa or type bb
xt

or type (ab + ba) (aa + bb)* (ab + ba).


ne

All three types can be generated from the nonterminal S from productions
above. The various substrings can be put together by repeated application of
the production

S--SS

This production is very useful. If we apply it four times we can turn one S
into five S's. Each of these S's can be a syllable of any of the three types.
For example, the EVEN-EVEN word aababbab can be produced as follows:

S 4 BALANCED S
> aaS
> aa UNBALANCED S UNBALANCED
CONTEXT-FREE GRAMMARS 255

ba S UNBALANCED Saa
Saa ba S ab
Saa ba BALANCED S ab
Saa ba bb S ab
Saa ba bb A ab
= aababbab

To see that all the words that are generated by these productions are in the
language EVEN-EVEN we need only to observe that words derived from S

om
can be decomposed into two-letter syllables and the unbalanced syllables, ab
and ba, come into the working string in pairs,, which add two a's and two
b's. Also, the balanced syllables add two of one letter and zero of the other
letter. The sum total of a's and b's will be the sum of even numbers of a's

.c
and even numbers of b's. Both the a's and the b's in total will be even.
Therefore, the language generated by this CFG is exactly EVEN-EVEN.
ns
io
at

EXAMPLE
c

Let the terminals be a and b. Let the nonterminals be S, A, and B. Let the
du

productions be

S-. aB
t-e

S --- bA
A- a
x

A-- aS
A -- bAA
ne

B-- b
B-- bS
B ---> aBB

The language that this CFG generates is the language EQUAL of all strings
that have an equal number of a and b's in them. This language begins

EQUAL = {ab ba aabb abab abba baab baba bbaa aaabbb... }


(Notice that previously we included A in this language, but for now it has
been dropped.)
To prove that this is the language that is generated by these productions we
need to demonstrate two things: first, that every word in EQUAL can be
256 PUSHDOWN AUTOMATA THEORY
derived from S by these productions and, second, that every word generated
by these productions is in EQUAL.
To do this we should note that the nonterminal A stands for any word that
is a-heavy, that is, a word that has one more a than it has b's (for example,
7 a's and 6 b's). The nonterminal B stands for any word that is b-heavy,
that is, that has one more b than it has a's, (for example 4 b's and 3 a's).
We are really making three claims at once.

Claim 1 All words in EQUAL can be generated by some sequence of productions


beginning with the start symbol S.

om
Claim 2 All words that have one more a than b's can be generated from these
productions by starting with the symbol A.
Claim 3 All words that have one more b than a's can be generated from these

.c
productions by starting with the symbol B.

If one of these three claims is false, then there is a smallest word of one
ns
of these three types that cannot be generated as we claim it can. We are
looking for the smallest counterexample to any of these three claims. Let w
tio
be the smallest counterexample. For all words shorter than w the three claims
must be true.
Which of these three claims does w disprove?
ca

Let us first challenge Claim 1, by assuming that the word w is in the


language EQUAL, but it cannot be produced from these productions starting
with the symbol S. The word w either begins with an a or a b. Let us say
du

that it begins with an a. It then is of the form a(rest). Since w is in the


language EQUAL, the string (rest) must have exactly one more b in it than
a's. By our claim (which holds for all words with fewer letters than w has),
-e

we know that (rest) can then be generated from these productions, starting
with the symbol B. But then w can be generated from these productions starting
xt

with the symbol S, since the production


ne

S > aB

then leads to

Sa(rest) = w
A similar contradiction arises if we assume that w started with the letter b.
In this case the letters of w after the b form an A-heavy string that can
be generated from A. S-->bA then generates w. Therefore the smallest coun-
terexample to these claims cannot be a word in EQUAL. That means that w
does not disprove Claim 1.
Let us now entertain the possibility that w disproves Claim 2. That means
that there is a word w that has one more a than b's but that cannot be produced
CONTEXT-FREE GRAMMARS 257

from these productions starting with the symbol A, and further that all words
smaller than w satisfy all three claims.
There are two cases we need to consider. The first is that the word w
begins with the letter a and the second is that the word w begins with the
letter b.
In the first case w must be of the form a(rest). Since w has one more a
than b's, the substring (rest) has the same number of a's and b's. This means
that it can be generated from these rules starting with the letter S, because
(rest) has fewer letters than w does, and so Claim 1 applies to it.
However, if (rest) can be produced from S, then w can be produced from
A starting with the production

m
A = aS

co
which leads to

.
Sa(rest) = w ns
This contradicts the premise of our counterexample. Therefore, w cannot
tio
start with an a.
Now let us treat the second case. Suppose w begins with the letter b. The
word w is still of the form b(rest), but now (rest) does not have the same
ca

number of a's and b's. The string (rest) has two more a's than it has b's.
Let us scan down the string (rest) from left to right until we find a substring
that has exactly one more a than it has b's. Call this the first half. What is
du

left must also have exactly one more a than it has b's. Call it the second
half. Now we know that the word w is of the form
-e

b(first half(second half)


xt

Both halves are of type A and can be generated from the symbol A since they
both have fewer letters than w has and Claim 2 must apply to them.
ne

This time we can generate w starting with the production

A > bAA

leading eventually to

Sb(first half)(second half)


Again, this contradicts the assumption that w is a counterexample to our
second claim.
The case where the smallest counterexample is of type B is practically iden-
tical to the case where w is of type A. If we reverse the letters a and b and
the letters A and B in the argument above we have the proof of this case.
258 PUSHDOWN AUTOMATA THEORY

We have now covered all possibilities, and we can conclude that there can
be no smallest counterexample to any of our claims. Therefore, all three claims
are true. In particular, Claim 1 is true: All the words in EQUAL can be
generated from the symbol S.
Even though we have worked hard we are only half done. We still need
to show that all the words that can be generated from S are in the language
EQUAL.
Again we make three claims:

Claim 4 All words generated from S are in EQUAL.


Claim 5 All words generated from A have one more a than b's.

om
Claim 6 All words generated from B have one more b than a's.

Let us say that w is the smallest counterexample to any of these three claims.

.c
Let us first consider whether w can violate Claim 4. Let us say that w is
produced from S but has unequal a's and b's. We are assuming that these
ns
three claims are true when applied to all words with fewer letters than w.
If w is produced from S, it either comes from S ---> aB or from S -- bA.
Since these cases are symmetric, let us say that w comes from S ---> aB. Now
tio
since this B generates a word with one fewer letter than w, we know that the
three claims apply to the production that proceeds from this B. This means
in particular that what is generated from B satisfies Claim 6 above and that
ca

it therefore generates a word with one more b than a. Therefore, w will have
exactly the same number of b's and a's. The word w, then, satisfies Claim
4 and is not a counterexample.
du

Now let us treat the case where the smallest counterexample, is a word
called w that disproves Claim 6; that is, it is generated from the symbol B
but does not have exactly one more b than a's. It could not have come from
-e

B ---> b since then it would have one more b than a (one b, no a's). It could
not come from the production B --- bS, since whatever is produced from the
xt

S part is a string of length less than w, which must then satisfy Claim 4 and
have equal a's and b's leaving w in proper form. Lastly, it could not come
ne

from B ---> aBB, since each of the B's is known by Claim 6 to produce words
with one more b than a as long as the words are shorter than w. Taken
together, they have two more b's than a's and with an a in front they have
exactly one more b than a. But then w is not a counterexample to Claim 6.
All together, this contradicts the existence of any counterexample to Claim
6.
The case where the counterexample may be a word that disproves Claim
5 similarly leads to a contradiction. Therefore, there is no smallest counter-
example, and the three claims are true, and in particular Claim 4, which is
the one we needed. This concludes the proof that the language generated by
these productions is the language EQUAL. U
CONTEXT-FREE GRAMMARS 259
It is common for the same nonterminal to be the left side of more than
one production. We now introduce the symbol, 1, a vertical line, to mean
disjunction (or). Using it we can combine all the productions that have the
same left side. For example,

S- aS
S-- A

can be written simply as:

om
S -- aS I A

The CFG

.c
S ---> X
S-.--Y
SX--- >A ns
Y-- aY
tio
Y-- bY
Y-- a
ca

Y--- b
du

can be written as:

S-- XIY
-e

Y--- aYIbY a)b


xt

We have committed a small sloppiness here. We have called a set of pro-


ne

ductions a CFG when we know that by definition a CFG has three other parts.
This error is common and forgivable since the sets of terminals and nonter-
minals can be deduced by examining the productions.
The notation we are using for CFG's is practically universal with the fol-
lowing minor changes:
Some authors use the symbol

":: =" instead of "--

Some authors call nonterminals variables.


Some authors use a small epsilon, e, or small lambda, X, instead of A to
denote the null string.
260 PUSHDOWN AUTOMATA THEORY
Some authors indicate nonterminals by writing them in angle brackets:

(S) -~ WX IYM
(X)-- A
(YM-- a(Y) I b(Y) I a I b

We shall be careful to use capital letters for nonterminals and small letters
for terminals. Even if we did not do this, it would not be hard to determine
when a symbol is a terminal. All symbols that do not appear as the left parts
of productions are terminals with the exception of A.

om
Aside from these minor variations, we call this format-arrows, vertical
bars, terminals, and nonterminals-for presenting a CFG, BNF standing for
Backus Normal Form or Backus-Naur Form. It was invented by John W.
Backus for describing the high-level language ALGOL. Peter Naur was the

.c
editor of the report in which it appeared, and that is why BNF has two possible
meanings.
ns
A FORTRAN identifier (variable or storage location name) can, by defi-
nition, be up to six alphanumeric characters long but must start with a letter.
We can generate the language of all FORTRAN identifiers by a CFG.
tio
S-- LETTER XXXXX
ca

X LETTER I DIGIT! A
LETTER -AIBICI.. . Iz
DIGIT-* 011121. . . 19
du

Not just the language of identifiers but the language of all proper FORTRAN
-e

instructions can be defined by a CFG. This is also true of all the statements
in the languages PASCAL, BASIC, PL/I, and so on. This is not an accident.
As we shall see in Chapter 22, if we are given a word generated by a specified
xt

CFG we can determine how the word was produced. This in turn enables us
to understand the meaning of the word just as identifying the parts of speech
ne

helps us to understand the meaning of an English sentence. A computer must


determine the grammatical structure of a computer language statement before
it can execute the instruction.
Regular languages were easy to understand in the sense that we were able
to determine how a given word could be accepted by an FA. But the class
of languages they define is too restrictive for us. By this we mean that regular
languages cannot express all of the deep ideas we may wish to communicate.
Context free languages can handle more of these-enough for computer pro-
gramming. And even this is not the ultimate language class, as we see in
Chapter 20. We shall return to such philosophical issues in Part III.
CONTEXT-FREE GRAMMARS 261
PROBLEMS

1. Consider the CFG:

S-- aS I bb

Prove that this generates the language defined by the regular expression

a*bb

om
2. Consider the CFG:

S -- XYX
X aX I bXI A

.c
Y -- bbb
ns
Prove that this generates the language of all strings with a triple b in
them, which is the language defined by
tio
(a + b)*bbb(a + b)*
ca

3. Consider the CFG:

S-- aX
du

X- aX I bX IA

What is the language this CFG generates?


-e

4. Consider the CFG:


xt

S -- XaXaX
ne

X aX I bX IA

What is the language this CFG generates?

5. Consider the CFG:

S -SS IXaXaX IA
X-- bXI A

(i) Prove that X can generate any b*.


262 PUSHDOWN AUTOMATA THEORY
(ii) Prove that XaXaX can generate any b*ab*ab*.
(iii) Prove that S can generate (b*ab*ab*)*.
(iv) Prove that the language of this CFG is the set of all words in (a + b)*
with an even number of a's with the following exception: We consider
the word A to have an even number of a's, as do all words with no
a's, but of the words with no a's only A can be generated.
(v) Show how the difficulty in part (iv) can be alleviated by adding the
production

S-- XS

om
6. (i) For each of the CFG's in Problems 1 through 5 determine whether
there is a word in the language that can be generated in two
substantially different ways. By "substantially," we mean that if

.c
two steps are interchangeable and it does not matter which comes
first, then the different derivations they give are considered "sub-

(ii)
ns
stantially the same" otherwise they are "substantially different."
For those CFG's that do have two ways of generating the same word,
tio
show how the productions can be changed so that the language gen-
erated stays the same but all words are now generated by substantially
only one possible derivation.
ca

7. Consider the CFG:


du

S -XbaaX IaX
X -Xa I Xb I A
-e

What is the language this generates? Find a word in this language that can
be generated in two substantially different ways.
xt

8. (i) Consider the CFG for "some English" given in this chapter. Show
ne

how these productions can generate the sentence:

Itchy the bear hugs jumpy the dog.

(ii) Change the productions so that an article cannot come between an


adjective and its noun.
9. (i) Show how in the CFG for "some English" we can generate the
sentence:

The the the cat follows cat.

(ii) Change the productions so that the same noun cannot have more than
one article. Do this for the modification in Problem 8 also.
CONTEXT-FREE GRAMMARS 263
10. Show that in the CFG for AE given in, this chapter we can eliminate
the nonterminal AE. In which other CFG's in this chapter can we elim-
inate a nonterminal?

Find a CFG for each of the languages defined by the following regular expres-
sions.

11. ab*

12. a*b*

om
13. (baa + abb)*

.c
Find CFG's for the following languages over the alphabet I = {a,b}.

14. (i)
ns
All words in which the letter b is never tripled.
io
(ii) All words that have exactly two or three b's.
at

15. (i) All words that do not have the substring ab.
(ii) All words that do not have the substring baa.
c
du

16. All words that have different first and last letters:
t-e

{ab ba aab abb baa bba . . . }


x

17. Consider the CFG:


ne

S-- AA
A -- AAA
A -- bA I Ab Ia

Prove that the language generated by these productions is the set of all words
with an even number of a's, but not no a's. Contrast this grammar with the
CFG in Problem 5.

18. Describe the language generated by the following CFG:

S -- SS
S - XXX
X-- aX I Xa l b
264 PUSHDOWN AUTOMATA THEORY
19. Write a CFG to generate the language of all strings that have more a's
than b's (not necessarily only one more, as with the nonterminal A for
the language EQUAL, but any number more a's than b's).

{a aa aab aba baa aaaa aaab ... }


20. Let L be any language. Define the transpose of L to be the language
of all the words in L spelled backward (see Chapter 6, Problem 17).
For example, if

m
L = {a baa bbaab bbbaa}

co
then

transpose (L) = {a aab baabb aabbb}

.
ns
Show that if L is a context-free language then the transpose of L is
context-free also.
tio
ca
du
-e
xt
ne
CHAPTER 14

om
.c
ns
io
TREES
c at
du

In old-fashioned English grammar courses students were often asked to diagram


a sentence. This meant that they were to draw a parse tree, which is a picture
with the base line divided into subject and predicate. All words or phrases
t-e

modifying these were drawn as appendages on connecting lines. For example,

over the lazy dog.


The quick brown fox jumps
x

becomes:
ne

fox jumps

-
~0
dog

265
266 PUSHDOWN AUTOMATA THEORY

If the fox is dappled grey, then the parse tree would be:
fox jumps

dog

om
.c
since dappled modifies grey and therefore is drawn as a branch off the grey
line.
ns
The sentence, "I shot the man with the gun." can be diagrammed in two
ways:
tio
I shot man
ca
du

gun
-e
xt

or
ne

shot man

gun
TREES 267
In the first diagram "with the gun" explains how I shot. In the second
diagram "with the gun" explains who I shot.
These diagrams help us straighten out ambiguity. They turn a string of words
into an interpretable idea by identifying who does what to whom.
A famous case of ambiguity is the sentence, "Time flies like an arrow."
We humans have no difficulty identifying this as a poetic statement, technically
a simile, meaning, "Time passes all too quickly, just as a speeding arrow
darts across the endless skies"--or some such euphuism.
This is diagrammed by the following parse tree:

om
time flies

.c
ns
arrow
tio
ca

Notice how the picture grows like a tree when "an" branches from "arrow."
A Graph Theory tree, unlike an arboreal tree, can grow sideways or upside
du

down.
A nonnative speaker of English with no poetry in her soul (a computer,
-e

for example) who has just yesterday read the sentence, "Horse flies like a
banana." might think the sentence should be diagrammed as
xt

flies like arrow


ne

where she thinks "time flies" may have even shorter lives than drosophilae.
Looking in our dictionary, we see that "time" is also a verb, and if so in
this case, the sentence could be in the imperative mood with the understood
subject "you," in the same way that "you" is the understood subject of the
sentence "Close the door." A race track tout may ask a jockey to do a favor
268 PUSHDOWN AUTOMATA THEORY

and "Time horses like a trainer" for him. The computer might think this sen-
tence should be diagrammed:

(you) time flies

Co

om
arrow

.c
ns
Someone is being asked to take a stopwatch and "time" some racing "flies"
just as "an arrow" might do the same job, although one is unlikely to meet
a straight arrow at the race track.
io
The idea of diagramming a sentence to show how it should be parsed carries
over easily to CFG's. We start with the symbol S. Every time we use a
at

production to replace a nonterminal by a string, we draw downward lines from


the nonterminal to each character in the string.
c

Let us illustrate this on the CFG


du

S -* AA
A--- AAA I bA I Ab I a
t-e

We begin with S and apply the production S-- AA.


x

/S\
ne

A A

To the left-hand A let us apply the production A - bA. To the right-hand A


let us apply A -- AAA.

A A

/I /1\
b A A A A
TREES 269
The b that we have on the bottom line is a terminal, so it does not descend
further. In the terminology of trees it is called a terminal node. Let the four
A's, left to right, undergo the productions A ---> bA, A --+ a, A --* a, A ---> Ab
respectively. We now have

S"1_
A A
/ I /1\

om
b A A A A

/\ II /\
b A a a A b

.c
Let us finish off the generation of a word with the productions A a and
A -. a:
ns --
io
S
at

/ I / I\A A
c

b A A A A
du

/\ I I/\
h A a a A b
ii I
t-e

a 0
x
ne

Reading from left to right, the word we have produced is bbaaaab.


As was the case with diagramming a sentence, we understand more about
the finished word if we see the whole tree. The third and fourth letters are
both a's, but they are produced by completely different branches of the tree.
These tree diagrams are called syntax trees or parse trees or generation
trees or production trees or derivation trees. The variety of names comes
from the multiplicity of applications to linguistics, compiler design, and math-
ematical logic.
The only rule for formation of such a tree is that every nonterminal sprouts
branches leading to every character in the right side of the production that
replaces it. If the nonterminal N can be replaced by the string abcde:

N -- abcde
270 PUSHDOWN AUTOMATA THEORY

then in the tree we draw:

a b c d e

There is no need to put arrow heads on the edges because the direction of
production is always downward.

om
EXAMPLE

One CFG for a subsystem of Propositional Calculus is:

.c
S--> (S) I SDS I -S I p I q
ns
The only nonterminal is S. The terminals are p q
the symbol for implication.
D ( ) where "D" is
tio
In this grammar consider the diagram:

S
ca

S
du
-e

sS (D S
xt
ne

I I/\ S

This is a derivation tree for the 13-letter word

- pD(pD -- q))
TREES 271

We often say that to know the derivation tree for a given word in a given
grammar is to understand the meaning of that word.
The concept of "meaning" is one that we shall not deal with mathematically
in this book. We never presumed that the languages generated by our CFG's
have any significance beyond being formal strings of symbols. However, in
some languages the meaning of a string of symbols is important to us for
reasons of computation. We shall soon see that knowing the tree helps us
determine how to evaluate and compute.

om
EXAMPLE

Let us concentrate for a moment on an example of a CFG for a simplified

.c
version of arithmetic expressions:
ns
S -> S + S IS * S I number

Let us presume that we know precisely what is meant by "number."


io
We are all familiar with the ambiguity inherent in the expression
at

3 + 4*5
c

Does it mean (3 + 4) * 5, which is 35, or does it mean 3 + (4 * 5), which


du

is 23?
In the language defined by this particular CFG we do not have the option
of putting in parentheses for clarification. Parentheses are not generated by
t-e

any of the productions and are therefore not letters in the derived language.
There is no question that 3 + 4 * 5 is a word in the language of this CFG.
The only queston is what does this word mean in terms of calculation?
x

It is true that if we insisted on parentheses by using the grammar:


ne

S ---* (S + S) I (S * S) I number

we could not produce the string 3 + 4 * 5 at all. We could only produce

Sz (S + S)>(S + (S*S))•>. .>(3 + (4*5))


or
S =(S*S)=((S + S)*S) ... >((3 + 4)* 5)

neither of which is an ambiguous expression.


In the practical world we do not need to use all these cluttering parentheses
because we have adopted the convention of "hierarchy of operators," which
272 PUSHDOWN AUTOMATA THEORY

says that * is to be executed before +. This, unfortunately, is not reflected


in either grammar. Later, in Chapter 20, we present a grammar that generates
unambiguous arithmetic expressions that will mean exactly what we want them
to mean without the need for burdensome parentheses. For now, we can only
distinguish between these two possible meanings for the expression 3 + 4 * 5
by looking at the two possible derivation trees that might have produced it.

S
/1I\ 's
/1I\
S + S S * S

om
S q S 5 + S

.c
4 5 3 4

ns
We can evaluate an expression in parse-tree form from the tree picture itself
by starting at the bottom and working our way up to the top, replacing each
tio
nonterminal as we come to it by the result of the calculation that it produces.
This can be done as follows:

7 7
ca

S
du

S
s +
I1
S 3 +
I1 3 + 20 423

4 5
-e

3 S * S *

I
45
1
xt
ne

orS
S

1\ I S * S 4z I1 5* 5 4>7 5 55

1 1
s + S 5 3 + 4

3 4

These examples show how the derivation tree can explain what the word
means in much the same way that the parse trees in English grammar explain
the meaning of sentences.
TREES 273
In the special case of this particular grammar (not for CFG's in general),
we can draw meaningful trees of terminals alone using the start symbol S only
once. This will enable us to introduce a new notation for arithmetic expres-
sions--one that has direct applications to Computer Science.
The method for drawing the new trees is based on the fact that + and *
are binary operations that combine expressions already in the proper form. The
expression 3 + (4 * 5) is a sum. A sum of what? A sum of a number and
a product. What product? The product of two numbers. Similarly (3 + 4) * 5
is a product of a sum and a number, where the sum is a sum of numbers.
Notice the similarity to the original recursive definition of arithmetic expres-

om
sions. These two situations are depicted in the following trees.

I
S

I
S

.c
ns
3 + 5
tio
4 5 3 4
ca

These are like derivation trees for the CFG:


du

S - S + S IS * S I number
-e

except that we have eliminated most of the S's. We have connected the branches
directly to the operators instead.
xt

The symbols * and + are no longer terminals, since they must be replaced
by numbers. These are actually standard derivation trees taken from a new
ne

CFG in which S, * and + are nonterminals and number is the only terminal.
The productions are:

S -- *I+ Inumber
+ -+ ++ +* I + number *+ I**l*numberlnumber + I
number * number number
* -> ++ +* I + number *+ j**j*numberjnumber + I
number * I number number
As usual number has been underlined because it is only one symbol. The only
words in this language are strings of number. But we are interested in the
derivation trees themselves, not in these dull words.
274 PUSHDOWN AUTOMATA THEORY

From these trees we can construct a new notation for arithmetic expressions.
To do this, we walk around the tree and write down the symbols, once each,
as we encounter them. We begin our trip on the left side of the start symbol
S heading south. As we walk around the tree, we keep our left hand always
on the tree.

m
,/3/" 4/ ,',\ 5

o
I_/ N\\

.c
The first symbol we encounter on the first tree is +. This we write down
ns
as the first symbol of the expression in the new notation. Continuing to walk
around the tree, keeping it on our left, we first meet 3 then + again. We
write down the 3, but this time we do not write + down because we have
tio
already included it in the string we are producing. Walking some more we
meet *, which we write down. Then we meet 4, then * again, then 5. So
ca

we write down 4, then 5. There are no symbols we have not met, so our
trip is done. The string we have produced is:
du

+ 3 * 4 5.
The second derivation tree when converted into the new notation becomes:
-e

*+ 3 4 5.
xt

13t
ne

/ \
/ *\

/ N
/ \ \

/ \\
S3 / \ 4

This tree-walking method produces a string of the symbols +, *, and


number, which summarizes the picture of the tree and thus contains the in-
formation necessary to understand the meaning of the expression. This is in-
formation that is lacking in our usual representation of arithmetic expressions,
TREES 275

unless parentheses are required. We shall show that these strings are unam-
biguous in that each determines a unique calculation without the need for
establishing the convention of times before plus. These representations are said
to be in operator prefix notation because the operator is written in front of
the operands it combines.
Since S --> S + S has changed from

S +

om
+ S to 3 4

3 4

.c
the left-hand tracing changes 3 + 4 into + 3 4.
To evaluate a string of characters in this new notation, we proceed as
ns
follows. We read the string from left to right. When we find the first substring
of the form
io
operator-operand-operand (call this o-o-o for short)
at

we replace these three symbols with the one result of the indicated arithmetic
c

calculation. We then rescan the string from the left. We continue this process
until there is only one number left, which is the value of the entire original
du

expression.
In the case of the expression + 3 * 4 5, the first substring we encounter
-e

of the form operator-operand-operand is * 4 5, so we replace this with the


result of the indicated multiplication, that is, the number 20. The string is
now + 3 20. This itself is in the form o-o-o, and we evaluate it by performing
xt

the addition. When we replace this with the number 23 we see that the process
of evaluation is complete.
ne

In the case of the expression * + 3 4 5 we find that the o-o-o substring


is + 3 4. This we replace with the number 7. The string is then * 7 5, which
itself is in the o-o-o form. When we replace this with 35, the evaluation process
is complete.
Let us see how this process works on a harder example. Let us start with
the arithmetic expression

((1 + 2) * (3 + 4) + 5) * 6.

This is shown in normal notation, which is called operator infix notation


because the operators are placed in between the operands. With infix notation
we often need to use parentheses to avoid ambiguity, as is the case with the
expression above. To convert this to operator prefix notation, we begin by
drawing its derivation tree:
276 PUSHDOWN AUTOMATA THEORY

/\
/ 4
/ + 3

"/\
//

o m
.c
Reading around this tree gives the equivalant prefix notation expression

*±*±12+3456
ns
tio
Notice that the operands are in the same order in prefix notation as they were
in infix notation, only the operators are scrambled and all parentheses are
ca

deleted.
To evaluate this string we see that the first substring of the form operator-
operand-operand is + 1 2, which we replaced with the number 3. The eval-
du

uation continues as follows:

String First o-o-o substring


-e

*+ *3 + 3456 + 34
xt

* + *3756 *37
ne

*+ 21 5 6 + 215

*266 *26 6

156

which is the correct value for the expression we started with.


Since the derivation tree is unambiguous, the prefix notation is also un-
ambiguous and does not rely on the tacit understanding of operator hierarchy
or on the use of parentheses.
This clever parenthesis-free notational scheme was invented by the Polish
logician Jan Lukasiewicz (1878-1956) and is often called Polish notation. There
TREES 277

is a similar operator postfix notation, which is also called Polish notation, in


which the operation symbols (+, *, . . . ) come after the operands. This can
be derived by tracing around the tree from the other side, keeping our right
hand on the tree and then reversing the resultant string. Both of these methods
of notation are useful for computer science, and we consider them again in
Chapter 22. U
Let us return to the more general case of languages other than arithmetic
expressions. These may also suffer from the problem of ambiguity. Substantive
ambiguity is a difficult concept to define.

o m
EXAMPLE

.c
Let us consider the language generated by the following CFG:

PROD I S--*-AB
ns
PROD2 A--a
tio
PROD 3 B ---> b
ca

There are two different sequences of applications of the productions that gen-
erate the word ab. One is PROD 1, PROD 2, PROD 3. The other is PROD 1,
PROD 3, PROD 2.
du

S > AB = aB > ab or S > AB > Ab > ab


-e

However, when we draw the corresponding syntax trees we see that the two
derivations are essentially the same:
xt
ne

s S

A B A B

I
a
I
b
I
a b
I
This example, then, presents no substantive difficulty because there is no
ambiguity of interpretation. This is related to the situation in Chapter 13 in
which we first built up the grammatical structure of an English sentence out
of noun, verb, and so on, and then substituted in the specific words of each
category either one at a time or all at once. When all the possible derivation
trees are the same for a given word then the word is unambiguous. U
278 PUSHDOWN AUTOMATA THEORY

DEFINITION
A CFG is called ambiguous if for at least one word in the language that it
generates there are two possible derivations of the word that correspond to
different syntax trees. U

EXAMPLE
Let us reconsider the language PALINDROME, which we can now define by

om
the CFG below:

S--> aSa I bSb a I b A

.c
At every stage in the generation of a word by this grammar the working string
contains only the one nonterminal S smack dab in the middle. The word grows
ns
like a tree from the center out. For example.:

... baSab => babSbab==babbSbbab=> babbaSabbab ...


tio
When we finally replace S by a center letter (or A if the word has no center
letter) we have completed the production of a palindrome. The word aabaa
ca

has only one possible generation:


du

S = aSa
> aaSaa
> aabaa
-e
xt

S
ne

a S a

a Sa
I
b

If any other production were applied at any stage in the derivation, a different
word would be produced.
We see then that this CFG is unambiguous. Proving this rigorously is left
to Problem 13 below. U
TREES 279

EXAMPLE

The language of all nonnull strings of a's can be defined by a CFG as follows:

S-- aS I Sa I a

In this case the word a3 can be generated by four different trees:

S S S S
/ I / I I\ I\

om
a S S S a S a
/ I II\ / I I\
a S a a

.c
I I I I
a a

This CFG is therefore ambiguous.


ns a a
tio
However the same language can also be defined by the CFG:

S -- aS I a
ca

for which the word a3 has only one production:


du

/ I s
-e

a S
/ I
xt

a S
ne

(See Problem 14 below). This CFG is not ambiguous. U


From this last example we see that we must be careful to say that it is
the CFG that is ambiguous, not that the language itself is ambiguous.
So far in this chapter we have seen that derivation trees carry with them
an additional amount of information that helps resolve ambiguity in cases where
meaning is important. Trees can be useful in the study of formal grammars
in other ways.
For example, it is possible to depict the generation of all the words in the
language of a CFG simultaneously in one big (possibly infinite) tree.
280 PUSHDOWN AUTOMATA THEORY

DEFINITION

For a given CFG we define a tree with the start symbol S as its root and
whose nodes are working strings of terminals and nonterminals. The descen-
dants of each node are all the possible results of applying every production
to the working string, one at a time. A string of all terminals is a terminal
node in the tree. The resultant tree is called the total language tree of the
CFG. U

om
EXAMPLE

For the CFG

.c
S - aa bX I aXX

the total language tree is:


X-- ab b ns
tio
S
ca

aa bX aXX
du

bab bb aabX abX aXab aXb

"ab/ /I I1\ (lb/


-e

aahab aabb abab abb aabah abab aabb abh


xt

This total language has only seven different words. Four of its words (abb,
aabb, abab, aabab) have two different possible derivations because they appear
ne

as terminal nodes in this total language tree in two different places. However,
the words are not generated by two different derivation trees and the grammar
is unambiguous. For example:

/I \

'X X

/\\H
• b U
TREES 281

EXAMPLE

Consider the CFG:

S ---> aSb I bSI a

We have the terminal letters a and b and three possible choices of substitutions
for S at any stage. The total tree of this language begins:
S

om
aSb bS

aaSbb a6S " a bbS

.c
ns
Here we have circled the terminal nodes because they are the words in the
tio
language generated by this CFG. We say "begins" because since the language
is infinite the total language tree is too.
We have already generated all the words in this language with one, two,
ca

or three letters.

L ={a ba aab bba...}


du

These trees may get arbitrarily wide as well as infinitely long. U


-e

EXAMPLE
xt

S -- SAS I b
ne

A -- ba I b

Every string with some S's and some A's has many possible productions that
apply to it, two for each S and two for each A.
S

SAS'

SASAS bAS SbaS SbS SASAS SAb

SASASAS bASAS SbaSAS SbSAS SASASAS...


°.o U
282 PUSHDOWN AUTOMATA THEORY

The essence of recursive definition comes into play in an obvious way when
some nonterminal has a production with a right-side string containing its own
name, as in this case:

X -- (blah) X (blah)

The total tree for such a language then must be infinite since it contains
the branch:

X > (blah) X (blah)

om
=> (blah) (blah) X (blah) (blah)
3
= (blah) 3 X (blah)

.c
This has a deep significance which will be important to us shortly.
ns
Surprisingly, even when the whole language tree is infinite, the language
may have only finitely many words.
tio
ca

EXAMPLE

Consider this CFG:


du

S-- XIb
X-- aX
-e

The total language tree begins:


xt

S
ne

x b

I
(aX
aaaX

Clearly the only word in this language is the single letter b. X is a bad
mistake; it leads to no words. It is a useless symbol in this CFG. We shall
be interested in matters like this again in Chapter 23. N
TREES 283
PROBLEMS

1. Chomsky finds three different interpretations for "I had a book stolen."
Explain them.

Below is a set of words and a set of CFG's. For each word, determine if
the word is in the language of each CFG and, if it is, draw a syntax tree to
prove it.

m
Words CFG's
2. ab CFG 1. S -aSbI ab

o
3. aaaa

.c
CFG 2. S -aS bS Ia
4. aabb

5. abaa
ns CFG 3. S -aS IaSbI X
tio
X -- aXa a
6. abba
ca

7. baaa CFG 4. S- aAS a


A --- SbA ISS I ba
du

8. abab
-e

9. bbaa CFG 5. S- aB I bA
A ---> a aS I bAA
xt

10. baab B --> b bS aBB


ne

11. Find an example of an infinite language that does not have any production
of the form

X ---> (blah) X (blah)


for any nonterminal X

12. Show that the following CFG's are ambiguous by finding a word with
two distinct syntax trees.
(i) S- SaSaSI b
(ii) S- aSb Sb ISa I a
(iii) S-- aaS aaaS I a
(iv) S- aS aSb I X
X-- Xa a
284 PUSHDOWN AUTOMATA THEORY

(v) S -AA
A -- AAA I a I bA I Ab

13. Prove that the CFG

S - aSa I bSb a b I A

does generate exactly the language PALINDROME as claimed in the


chapter and is unambiguous.

14. Prove that the CFG

m
S-- aS I a

co
is unambiguous.

.
15.
(i) S - XaX
ns
Show that the following CFG's that use A are ambiguous

X - aX I bX I A
tio
(ii) S -->aSX I A
X-- aX a
ca

(iii) S aS IbSIaaSIA
du

16. (i) Find unambiguous CFG's that generate the three languages in
Problem 15.
(ii) For each of the three languages generated in Problem 15, find an
-e

unambiguous grammar that generates exactly the same language ex-


cept for the word A. Do this by not employing the symbol A in the
CFG's at all.
xt

17. Begin to draw the total language trees for the following CFG's until we
ne

can be sure we have found all the words in these languages with one,
two, three, or four letters. Which of these CFG's are ambiguous?
(i) S- aS IbSIa
(ii) S-- aSaS I b
(iii) S--- aSa bSb a
(iv) S- aSb IbX
X--> bX b
(v) S- bA laB
A -- bAA I aS a
B -- aBB bS b
TREES 285
18. Convert the following infix expressions into Polish notation.
(i) 1* 2 * 3
(ii) 1* 2 + 3
(iii) 1 (2 + 3)
(iv) 1 (2 + 3)*4
(v) ((1 + 2)* 3) + 4
(vi) 1 + (2* (3 + 4))
(vii) 1 + (2 3) + 4

om
19. Suppose that, while tracing around a derivation tree for an arithmetic
expression to convert it into operator prefix notation, we make the fol-
lowing change: When we encounter a number we write it down, but we

.c
do not write down an operator until the second time we encounter it.
Show that the resulting string is correct operator postfix notation for the

20.
diagrammed arithmetic expression. ns
Invent a form of prefix notation for the system of Propositional Calculus
used in this chapter that enables us to write all well-formed formulas
tio
without the need for parentheses (and without ambiguity).
ca
du
-e
xt
ne
CHAPTER 15

om
.c
ns
REGULAR
io
GRAMMARS
c at
du
t-e

Some of the examples of languages we have generated by CFG's have been


regular languages, that is, they are definable by regular expressions. However,
we have also seen some nonregular languages that can be generated by CFG's
(PALINDROME and EQUAL).
x
ne

EXAMPLE

The CFG:

S ---> ab I aSb

generates the language

{anbr}
Repeated applications of the second production results in the derivation

S > aSb 7 aaSbb 4 aaaSbbb 4 aaaaSbbbb ...

286
REGULAR GRAMMARS 287

Finally the first production will be applied to form a word having the same
number of a's and b's, with all the a's first. This language as we demonstrated
in Chapter 11, is nonregular. U

EXAMPLE

The CFG:

S-- aSa I bSa I A

om
generates the language TRAILING-COUNT of all words of the form:

s alength(s) for all strings s in (a + b)*

.c
that is, any string concatenated with a string of as many a's as the string has
ns
letters. This language is also nonregular (See Chapter 11, Problem 10). E
tio
What then is the relationship between regular languages and context-free
grammars?
Several possibilities come to mind:
ca

1. All languages can be generated by CFG's.


2. All regular languages can be generated by CFG's, and so can some non-
du

regular languages but not all possible languages.


3. Some regular languages can be generated by CFG's and some regular
-e

languages cannot be generated by CFG's. Some nonregular languages can


be generated by CFG's and some nonregular languages cannot.
xt

Of these three possibilities, number 2 is correct. In this chapter we shall


indeed show that all regular languages can be generated by CFG's. We leave
ne

the construction of a language that cannot be generated by any CFG for Chapter
20.
We now present a method for turning an FA into a CFG so that all the
words accepted by the FA can be generated by the CFG and only the words
accepted by the FA are generated by the CFG. The process of conversion is
easier than we might suspect. It is, of course, stated as a constructive algorithm
that we first illustrate on a simple example.

EXAMPLE

Let us consider the FA below, which accepts the language of all words with
a double a:
288 PUSHDOWN AUTOMATA THEORY
b a, b

6 2 ab

We have named the start state S, the middle state M, and the final state F.
The word abbaab is accepted by this machine. Rather than trace through
the machine watching how its input letters are read, as usual, let us see how
its path grows. The path has the following step-by-step development where a

om
path is denoted by the labels of its edges concatenated with the symbol for
the state in which it now sits:

S (We begin in S)

.c
aM (We take an a-edge to M)
abS (We take an a-edge then a b-edge and we are in S)
abbS
abbaM
ns
(An a-edge, a b-edge, and a b-loop back to S)
(Another a-edge and we are in M)
abbaaF (Another a-edge and we are in F)
io
abbaabF (A b-loop back to F)
abbaab (The finished path: an a-edge a b-edge . . . )
c at
du

This path development looks very much like a derivation of a word in a


CFG. What would the rules of production be?
t-e

(From S an a-edge takes us to M) S-- aM


(From S a b-edge takes us to S) S-- bS
x

(From M an a-edge takes us to F) M-- aF


(From
ne

M a b-edge takes us to S) M ---> bS


(From F an a-edge takes us to F) F-- aF
(From F a b-edge takes us to F) F-- bF
(When at the final state F, we can F-- A
stop if we want to).
We shall prove in a moment that the CFG we have just described generates
all paths from S to F and therefore generates all words accepted by the FA.
Let us consider another path from S to F, that of the word babbaaba. The
path development sequence is

(Start here) S
(A b-loop back to S) bS
(An a-edge to M) baM
REGULAR GRAMMARS 289

(A b-edge back to S) babS


(A b-loop back to S) babbS
(An a-edge to M) babbaM
(Another a-edge to F) babbaaF
(A b-loop back to F) babbaabF
(An a-loop back to F) babbaabaF
(Finish up in F) babbaaba

b
a

om
.c
a

ns
This is not only a path development but also a derivation of the word
babbaaba from the CFG above.
tio
The logic of this argument is roughly as follows. Every word accepted by
this FA corresponds to a path from S to F. Every path has a step-by-step
ca

development sequence as above. Every development sequence is a derivation


in the CFG proposed. Therefore, every word accepted by the FA can be gen-
erated by the CFG.
du

The converse must also be true. We must show that any word generated
by this CFG is a word accepted by the FA. Let us take some derivation such
as
-e

Production Used Derivation


xt

S -- aM S>aM
M -bS # abS
ne

S -- aM abaM
M -aF abaaF
F -- bF • abaabF
F -- A = abaab

This can be interpreted as a path development:

Production Used Path Developed


S -> aM Starting at S we take an a-edge to M
M ---> bS Then a b-edge to S
S --> aM Then an a-edge to M
M ---> aF Then an a-edge to F
F -- bF Then a b-edge to F
F -- A Now we stop
290 PUSHDOWN AUTOMATA THEORY
a

The path, of course, corresponds to the word abaab, which must be in the
language accepted by the FA since its corresponding path ends at a final
state. U
The general rules for the algorithm above are:

om
CFG derivation -- path development --> path --- word accepted

and

.c
word accepted --> path -- path development -- CFG derivation
ns
For this correspondence to work, all that is necessary is that:
tio
1. Every edge between states be a production:
ca

x becomes x
du

and
2. Every production correspond to an edge between states:
-e

x > .Y comes from x• •


xt

Y
ne

or to the possible termination at a final state:

X- A

only when X is a final state.

If a certain state Y is not a final state, we do not include a production of


the form

Y---> A
for it.
REGULAR GRAMMARS 291

At every stage in the derivation the working string has this form:

(string of terminals) (one Nonterminal)

until, while in a final state, we apply a production replacing the single non-
terminal with A. It is important to take careful note of the fact that a path
that is not in a final state will be associated with a string that is not all
terminals, (i.e. not a word). These correspond to the working strings in the
middle of derivations, not to words in the language.

om
DEFINITION

For a given CFG a semiword is a string of terminals (maybe none) conca-

.c
tenated with exactly one nonterminal (on the right), for example,

(terminal) (terminal) . . ns
. (terminal) (Nonterminal)
tio
Contrast this with word, which is a string of all terminals, and working
string, which is a string of any number of terminals and nonterminals in any
ca

order.

Let us examine next a case of an FA that has two final states.


du

One easy example of this is the FA for the language of all words without
double a's. This, the complement of the language of the last example, is also
-e

regular and is accepted by the machine FA'.


, a, b
FA' b
xt
ne

Let us retain for the moment the names of the nonterminals we had before:
S for start, M for middle, and F for what used to be the final state, but is
not anymore.
The productions that describe the labels of the edges of the paths are still

S -> aMI bS
M--- bS aF
F--* aF I bF

as before.
292 PUSHDOWN AUTOMATA THEORY

However, now we have a different set of final states. We can accept a


string with its path ending in S or M, so we include the productions:

S -- A
and

M--A
but not

m
F-- A

co
The following paragraph is the explanation for why this algorithm works:
Any path through the machine FA' that starts at - corresponds to a string

.
ns
of edge labels and simultaneously to a sequence of productions generating a
semiword whose terminal section is the edge label string and whose right-end
nonterminal is the name of the state the path ends in. If the path ends in a
tio
final state, then we can accept the input string as a word in the language of
the machine, and simultaneously finish the generation of this word from the
CFG by employing the production:
ca

(Nonterminal corresponding to final state) - A


du

Because our definition of CFG's requires that we always start a derivation


with the particular start symbol S, it is always necessary to label the unique
start state in an FA with the nonterminal name S. The rest of the choice of
-e

names of states is arbitrary.


This discussion was general and complete enough to be considered a proof
xt

of the following theorem:


ne

THEOREM 19
All regular languages can be generated by CFG's.
This can also be stated as: All regular languages are CFL's. U

EXAMPLE

The language of all words with an even number of a's (with at least some
a's) is regular since it can be accepted by this FA:
REGULAR GRAMMARS 293
b b b

Calling the states S, M, and F as before, we have the following corre-


sponding set of productions:

m
S ---> bS IaM
M - bM I aF

o
F -* bF I aM I A

.c
We have already seen two CFG's for this language, but this CFG is sub-
ns
stantially different. (Here we may ask a fundamental question: How can we
tell whether two CFG's generate the same language? But fundamental questions
do not always have satisfactory answers.) U
tio
Theorem 19 was discovered (or perhaps invented) by Noam Chomsky and
ca

George A. Miller in 1958. They also proved the result below, which seems
to be the flip side of the coin.
du

THEOREM 20

If all the productions in a given CFG fit one of the two forms:
-e

Nonterminal --> semiword


xt

or
ne

Nonterminal --)- word

(where the word may be A) then the language generated by this CFG is regular.

PROOF

We shall prove that the language generated by such a CFG is regular by


showing that there is a TG that accepts the same language. We shall build
this TG by constructive algorithm.
Let us consider a general CFG in this form:
294 PUSHDOWN AUTOMATA THEORY
N1 - wIN 2 N 7 W10
N1 - w 2N 3 N 41 W23
N 2 - w 3N 4 . . .

where the N's are the nonterminals, the w's are strings of terminals, and the
parts wyNz are the semiwords used in productions. One of these N's must be
S. Let N, = S.
Draw a small circle for each N and one extra circle labeled +. The circle

om
for S we label -.

(D G
.c
00G
00...0.©..0 ns
tio
For every production rule of the form:
ca

Ux-- wAz
du

draw a directed edge from state Nx to N, and label it with the word wy.
-e
xt

If the two nonterminals above are the same the path is a loop.
ne

For every production rule of the form:

Np - Wq

draw a directed edge from Np to + and label it with the word Wq.

We have now constructed a transition graph. Any path in this TG from -


to + corresponds to a word in the language of the TG (by concatenating
REGULAR GRAMMARS 295

labels) and simultaneously corresponds to a sequence of productions in the


CFG generating the same word. Conversely, every production of a word in
this CFG:
S > wN > wwN > wwwN ... = wwwww

corresponds to a path in this TG from - to +.


Therefore, the language of this TG is exactly the same as the language of
the CFG. Therefore, the language of the CFG is regular. U
We should note that the fact that the productions in some CFG are all in

m
the required format does not guarantee that the grammar generates any words.
If the grammar is totally discombobulated, the TG that we form from it will

co
be crazy too and accept no words. However, if the grammar generates a lan-
guage of some words then the TG produced above for it will accept that
language.

.
ns
DEFINITION
tio
A CFG is called a regular grammar if each of its productions is of one of
the two forms
ca

Nonterminal - semiword
du

or
Nonterminal - word U
-e

The two previous proofs imply that all regular languages can be generated
by regular grammars and all regular grammars generate regular languages.
We must be very careful not to be carried away by the symmetry of these
xt

theorems. Despite both theorems it is still possible that a CFG that is not in
the form of a regular grammar can generate a regular language. In fact we
ne

have seen examples of this very phenomenon in Chapters 13 and 14.

EXAMPLE

Consider the CFG:

S -- aaS I bbS I A

This is a regular grammar and so we may apply the algorithm to it. There
is only one nonterminal, S, so there will be only two states in the TG, -
and the mandated +. The only production of the form
296 PUSHDOWN AUTOMATA THEORY

Np Wq

is
S--A

so there is only one edge into + and that is labeled A. The productions
S - aaS and S -> bbS are of the form N, --- wN 2 where the N's are both S.
Since these are supposed to be made into paths from N, to N2 they become
loops from S back to S. These two productions will become two loops at -
one labeled aa and one labeled bb. The whole TG is shown below:

o m
aa

.c
A
ns bb
tio
ca

By Kleene's theorem, any language accepted by a TG is regular, therefore


the language generated by this CFG (which is the same) is regular.
It corresponds to the regular expression
du

(aa + bb)*
-e
xt

EXAMPLE
ne

Consider the CFG:

S-- aaS l bbS I abXI baX I A


X-- aaX l bbX l abS l baS
The algorithm tells us that there will be three states: -, X, +.

Since there is only one production of the form

gp--> Wq

there is only one edge into +. The TG is:


REGULAR GRAMMARS 297

aa, bb ab aa, bb

om
which we immediately see accepts our old friend the language EVEN-EVEN.
(Do not be fooled by the A edge to the + state. It is the same as relabeling
the - state +.) U

.c
EXAMPLE ns
tio
Consider the CFG:

S --> aA bB
ca

A--> aS a
B --)- bS b
du

The corresponding TG constructed by the algorithm in Theorem 20 is:


-e
xt
ne

b
bb

The language of this CFG is exactly the same as the language of the CFG
two examples ago except that it does not include the word A. This language
can be defined by the regular expression (aa + bb)+.
298 PUSHDOWN AUTOMATA THEORY

We should also notice that the CFG above does not have any productions
of the form

For a CFG to accept the word A, it must have at least one production of
this form, called a A-production.
A theorem in the next chapter states that any CFL that does not include
the word A can be defined by a CFG that includes no A-productions. Notice
that a A-production need not imply that A is in the language, as with

om
S -- aX
X-- A

.c
The language here is just the word a.
The CFG's that are constructed by the algorithm in Theorem 19 always
ns
have A-productions, but they do not always generate the word A. We know
this because not all regular languages contain the word A, but the algorithm
suggested in the theorem shows that they can all be converted into CFG's
tio
with A-productions.
ca
du

PROBLEMS
-e

Find CFG's that generate these regular languages over the alphabet
xt

S= {a, b}:
ne

1. The language defined by (aaa + b)*

2. The language defined by (a + b)* (bbb + aaa) (a + b)*

3. All strings without the substring aaa.

4. All strings that end in b and have an even number of b's in total.

5. The set of all strings of odd length.

6. All strings with exactly one a or exactly one b.

7. All strings with an odd number of a's or an even number of b's.


REGULAR GRAMMARS 299

For the following CFG's find regular expressions that define the same language
and describe the language.

8. S--aX bS aIb
X-- aX a

9. S- bS aX b
X-- bX aS a

10. S- aaS abSIbaSlbbSIA

om
11. S- aB IbA I A
A -- aS
B -- bS

.c
12. S- aB bA
A ---> aB a
B --> bA b
ns
io
13. S- aS bX a
X-- aX bY a
at

Y--> aY a
c

14. S--> aS bXl a


du

X--- aXI bY bZ a
Y-> aY a
Z---> aZ bW
t-e

W --> aW a

15. S- bS aX
X---> bS IaY
x

Y-raYlbYla lb
ne

16. (i) Starting with the alphabet

S= {ab() + *}

find a CFG that generates all regular expressions.


(ii) Is this language regular?

17. Despite the fact that a CFG is not in regularform it still might generate
a regular language. If so, this means that there is another CFG that
defines the same language and is in regular form. For each of the ex-
amples below, find a regular form version of the CFG.
300 PUSHDOWN AUTOMATA THEORY

(i) S --> XYZ


X--* aX bX A
Y--- aY bY IA
Z---> aZ A
(ii) S -> XXX
X -- aX a
Y-- bY b
(iii) S -XY
X - I
aX Xa a
Y-- aY Ya a

om
18. Each of the following CFG's has a production using the symbol A and
yet A is not a word in its language. Show that there are other CFG's

.c
for these languages that do not use A.
(i) S aX I bX

(ii) S
X--•albIm
aX bS Ia Ib
ns
tio
X---> aX a A
(iii) S aS bX
X aXI A
ca

19. Show how to convert a TG into a regular grammar without first con-
du

verting it to an FA.
-e

20. Let us, for the purposes of this problem only, allow a production of the
form
N,--* r N2
xt

where N, and N2 are nonterminals and r is a regular expression. The


meaning of this formula is that in any working string we may substitute
ne

for N1 any string wN2 where w is a word in the language defined by


r. This can be considered a short-hand way of writing an infinite family
of productions, one for each word in the language of r.
Let a grammar be called bad if all of its -productions are of the two
forms
N1,-- r N 2
N3 -- A
Bad grammars generate languages the same way CFG's do.
Prove that even a bad grammar cannot generate a nonregular language,
by showing how to construct one regular expression that defines the same
language as the whole bad grammar.

You might also like