Professional Documents
Culture Documents
CS402 Quiz Solved and Reference-1
CS402 Quiz Solved and Reference-1
CS402 Quiz Solved and Reference-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
.c
ACCEPT and READ
ACCEPT AND START
ns
ACCEPT AND WRITE
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
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
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
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
20. A(n) ___________ operator adds a new letter at the top of STACK
PUSH
POP
xt
READ
APPEND
ne
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
27. Before running the input string on PDA, it is first placed on ___________
Stack
Memory
xt
Tape
Ram
ne
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
.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
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
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
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
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.
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
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
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)+.
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
98
© Copyright Virtual University of Pakistan
Theory of Automata (CS402)
Theory of Automata
Lecture N0. 35
Reading Material
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
Null Production
Definition
-e
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)
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)
om
S → a|b|bb, A → a|b|bb, B → a|b|bb.
Which generates the finite language {a,b,bb}.
.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
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.
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
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
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
a
xt
S' A B +
a b b a b b
ne
a a b
b a a,b
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
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
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
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.
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
a,b
ne
a
b b
READ READ READ
∆ ∆ ∆
106
© Copyright Virtual University of Pakistan
Theory of Automata (CS402)
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
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
READ1 ∆… aaabbb∆ …
PUSH a a∆ … aaabbb∆ …
xt
READ1 a∆ … aaabbb∆ …
PUSH a aa∆ … aaabbb∆ …
ne
109
© Copyright Virtual University of Pakistan
Theory of Automata (CS402)
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
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
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)
om
9. A READ state that reads the next unused letter from the TAPE, (may contain more than one out'edges
with same label).
.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
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)
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
Summary
PDA corresponding to CFG, Examples of PDA corresponding to 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
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)
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
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)
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)
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
(READ3) XY aaab ∆ ∆
117
© Copyright Virtual University of Pakistan
Theory of Automata (CS402)
Theory of Automata
Lecture N0. 40
Reading Material
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
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
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
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.
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
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
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
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
START READ1 Λ $ $ 1
READ1 READ1 a $ a$ 2
READ1 READ1 a a aa 3
om
HERE READ2 Λ a '' 5
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
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
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
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
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
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)
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
.c
Where What What What Number
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
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
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
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
a S A
A X a
a S A
b a
127
© Copyright Virtual University of Pakistan
Theory of Automata (CS402)
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.
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)
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
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
PUSH S START
xt
a A
ne
S
S
PUSH A PUSH S
PUSH A PUSH S
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
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
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
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)
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)
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
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
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
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.
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
.c
ns
tio
ca
!
!
du
" $! !
")$ "%$
" $! !
"'$ "&$ " $
" $ "*$ "+$
-e
"($ "#$
!
"%$
!
"&$ "#$ 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)
om
!
.c
!
! ! !
ns
!" $ !
⇒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
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)
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
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
Theorem
Every regular language is accepted by some TM.
Example (b,b,R)
( , ,R) 2
5 HALT 1 START
(b,b,R)
(b,b,R)
3 4
(b,b,R)
141
© Copyright Virtual University of Pakistan
Theory of Automata (CS402)
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
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 ∝
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
(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
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
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?
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
.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
sions:
du
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
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:
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
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
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:
(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
ri
r73
220 AUTOMATA THEORY
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
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
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
a b
a
du
N sablueblue blue +
t-e
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
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
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
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
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
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)*
.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
(ii) If F accepts infinitely many words, than F accepts some word w such that
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
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
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
THEOREM 18
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
We can test them all by running them on the machine. If any are accepted,
du
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
FA1 FA2
ne
a a
a a (I
'V and
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.
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
.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
A
x
ne
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.
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
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
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
I
-+9
2
ne
8 5
4 + 8- +---
21 3
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
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:
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
LOAD 3 in Register 1
LOAD 4 in Register 2
xt
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
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.
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
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:
.c
The method by which this sentence can be generated is outlined below:
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
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)
-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.
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
form
or
-e
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
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
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
EXAMPLE
PROD I S---) aS
PROD2 S--->A
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
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
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
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
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:
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
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
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
om
X-->A
.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
EXAMPLE
S-- aS
S -- bS
S-. a
S--- b
S- A
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
anything aa anything
ne
or
(a + b)*aa(a + b)*
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
Similarly, the words that can be derived from Y are exactly those that begin
x
EXAMPLE
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
type aa or type bb
xt
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
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
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
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
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
A > bAA
leading eventually to
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:
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
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
S-- XIY
-e
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
(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
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
S-- aX
du
X- aX I bX IA
S -- XaXaX
ne
X aX I bX IA
S -SS IXaXaX IA
X-- bXI A
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
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
(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
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.
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).
m
L = {a baa bbaab bbbaa}
co
then
.
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
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
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:
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
S -* AA
A--- AAA I bA I Ab I a
t-e
/S\
ne
A A
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
N -- abcde
270 PUSHDOWN AUTOMATA THEORY
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
.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
- 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
.c
version of arithmetic expressions:
ns
S -> S + S IS * S I number
3 + 4*5
c
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
S ---* (S + S) I (S * S) I number
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
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
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
the addition. When we replace this with the number 23 we see that the process
of evaluation is complete.
ne
((1 + 2) * (3 + 4) + 5) * 6.
/\
/ 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
*+ *3 + 3456 + 34
xt
* + *3756 *37
ne
*+ 21 5 6 + 215
*266 *26 6
156
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
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:
.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.:
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
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
S -- aS I a
ca
/ I s
-e
a S
/ I
xt
a S
ne
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
.c
S - aa bX I aXX
aa bX aXX
du
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
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
.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.
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'
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:
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
S-- XIb
X-- aX
-e
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
8. abab
-e
9. bbaa CFG 5. S- aB I bA
A ---> a aS I bAA
xt
11. Find an example of an infinite language that does not have any production
of the form
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
S - aSa I bSb a b I A
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
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
EXAMPLE
The CFG:
S ---> ab I aSb
{anbr}
Repeated applications of the second production results in the derivation
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:
om
generates the language TRAILING-COUNT of all words of the form:
.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
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
(Start here) S
(A b-loop back to S) bS
(An a-edge to M) baM
REGULAR GRAMMARS 289
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
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
S -- aM S>aM
M -bS # abS
ne
S -- aM abaM
M -aF abaaF
F -- bF • abaabF
F -- A = abaab
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
Y
ne
X- A
Y---> A
for it.
REGULAR GRAMMARS 291
At every stage in the derivation the working string has this form:
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
.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.
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
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
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
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
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
or
ne
(where the word may be A) then the language generated by this CFG is regular.
PROOF
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
Np - Wq
draw a directed edge from Np to + and label it with the word Wq.
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
EXAMPLE
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
(aa + bb)*
-e
xt
EXAMPLE
ne
gp--> Wq
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
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
4. All strings that end in b and have an even number of b's in total.
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
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
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
S= {ab() + *}
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
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