80% found this document useful (5 votes)
18K views198 pages

Introduction To Computer Theory by Cohen Solutions Manual

This chapter introduces formal languages and provides examples of simple languages to illustrate core concepts. It emphasizes the importance of understanding which concepts will be built upon in later chapters versus more trivial examples. The language of palindromes is highlighted as important to understand going forward. The chapter also contains problems to help students explain and prove basic properties of languages to develop their understanding of formal proof.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
80% found this document useful (5 votes)
18K views198 pages

Introduction To Computer Theory by Cohen Solutions Manual

This chapter introduces formal languages and provides examples of simple languages to illustrate core concepts. It emphasizes the importance of understanding which concepts will be built upon in later chapters versus more trivial examples. The language of palindromes is highlighted as important to understand going forward. The chapter also contains problems to help students explain and prove basic properties of languages to develop their understanding of formal proof.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 198

- SoLuTions MANUAL

ele To =

OMS
Bie
_SESOND EDITION |

Se

Ws PREPARED oY |
py CHANAH BRENENSON
Digitized by the Internet Archive
In 2022 with funding from
Kahle/Austin Foundation

https://archive.org/details/solutionsmanualt0000bren
SOLUTIONS MANUAL

TO ACCOMPANY

INTRODUCTION TO
COMPUTER THEORY
SECOND EDITION

DANIEL I. A. COHEN
Hunter College
City University of New York

Prepared by
CHANAH BRENENSON

John Wiley & Sons, Inc.


New York e Chichester e Brisbane * Toronto e Singapore e Weinheim
eash shin sorig an
Hawtie jos wl wi ut
uM vane TaqRy att

PS Ge oll ont ty porelgyer(

apifiin +n odo aval mo Sar vcd aii


noite dwourlits \soitibe
ec em ey ates] Seipllanaed
Aw aati Borafoeer

=a

SPegarins © i af? e yaa) @ a @ tame. 400

TIGA amet) Jey be ed ca bo main 98


ein ards te (ie egib
a) ee

Pvt) \o tfe) tiles Nines td Acmeies

m¢es#8- 2 4 2 .)
=

Fer)
08 8
ie
tee) ed
CONTENTS

Chapter |
Chapter 2
Chapter 3
Chapter 4
Chapter 5
Chapter 6
Chapter 7
Chapter 8
Chapter 9
Chapter 10
Chapter 11
Chapter 12
Chapter 13
Chapter 14
Chapter 15
Chapter 16
Chapter 17
Chapter 18
Chapter 19
Chapter 20
Chapter 21
Chapter 22
Chapter 23
Chapter 24
Chapter 25
“4
a ae ee :

| ——— a
7 _
-
CHAPTER 1

The main goal of this chapter is to give at least a vague idea of what Computer Theory
might be and why it might be studied. This course is often required of all majors and so
they enrol without even the usual pretense of acting volitionally. Unlike courses offering
programming skills this one is not primarily vocational -- it is, of course indirectly of benefit
to the art of programming, but it’s main thrust is intellectual enrichment. Plumbers may not
know exactly who invented plumbing but they do know why, and perhaps how, it came
about. For all their mental achievements programmers generally have no basis for
answering this same question about their profession.

Computer Science majors are very suspicious about being slipped extra theoretical math
courses under the guise of "this will be good for you somehow." To allay this fear we
emphasize that this is not a course in mathematics as such but a course in mathematical
models, a friendly technique that arises in social science and linguistics and other
unthreatening subjects. Of course, this is a course in mathematics. Haha. But the joke
is more on the mathematical community who have no idea what beautiful results lie in this
disenfranchised corner of their discipline -- this despite the fact that its grandfathers,
Hilbert, Gédel, Church, von Neuman et al., are held in reverence for their other, more
“pure” contributions.

In general the format of this text is in the classical theorem-proof-theorem-proof model, but
unfortunately most students who need to know the material in this course are (functionally)
mathematically illiterate. Even if they have been forced to take a course in Calculus or
Linear Algebra they have almost no conception of what it means to prove anything. Worse
yet, they are afraid of ever being responsible for constructing proofs themselves. This
book is realistically sensitive to this deplorable situation and it takes great pains to
assuage such anxiety. This is a gentle introduction to the art of providing convincing
explanations (i.e. proofs) for proposed propositions that may even usefully serve as a
primer for those math majors who suffer from proof-anxiety.

For the student who is more sophisticated mathematically there are other texts which
cover approximately this same material in many fewer pages. Some of these books are
excellent. We can recommend, without reservation, the following list from our own
experience: Hopcroft and Ullman, Lewis and Papadimetriou, Mana, Harrison, Kain, Minsky,
etc. No claim is made that this list is exhaustive and there may be many more texts of
recent vintage of which we are unaware.

Students should be informed about these books as possible other sources of reference,
though they should be cautioned about the vast disparity that may exist in notation and be
alerted to possible subtle differences in definitions. If any of these books are approached
they should be read from page one.

Before our current book was released there was some confusion as to whether the books
listed above were texts for graduate courses or undergraduate courses. There is a strong
2

feeling that Computer Theory should be taught to undergraduates, especially context-free


grammars a topic and notation that appears importantly in other undergraduate courses
such as compilers, Al etc. However, a graduate level text does not become an
undergraduate level text simply by reading it slowly.

Even though it is certainly true that the books listed above are more advanced this does
not mean that they contain everything of value that is in our text. There are original
contributions to be found among these pages, not original theorems but some new ideas
in the proofs, some original problems, some useful examples and many instances where
an idea that is only vaguely sketched in other sources is made explicit here. (Sometimes
making an idea explicit can seem painful or frightening, but there is always what seems
to us a good justification for giving a student more than just the impression that she knows
what is going on.)

If a student finds this book easy, let her read through it quickly. There are 600 pages here
and no one is going to read it over night, but we believe strongly that a bright student
should be encouraged to set her own pace. For the strongest foundation in this subject
one might start by finishing this book and then read one (or all) of the texts mentioned
before. A student who approaches the more advanced books already knowing most of the
material in them (our material) will not only have an easier time reading but will develop
an appreciation for the power of the advanced mathematical notation for its efficiency and
Clarity of abstraction.

We generally avoid advanced mathematical notation for two reasons: psychological and
pedagogical. The fear and alienation engendered by a page of symbols more than
neutralizes the efficacy of introducing the notation as it defeats the purpose of education.
It is also true that far too often a student (or a professional mathematician) gets a false
sense of confidence in the material by merely having followed the manipulation of the
symbols on the paper. We have too frequently heard, "I! followed everything you did in
class but | couldn't do the problems." It is our belief that the hasty adoption of abstract
notation is largely responsible for this predicament. Notation should be introduced only
when a student feels that her thoughts are flying faster than her ability to express them.
At that point students should be encouraged to invent their own personalized shorthand
bearing their own private meanings. Only as a final stage, when the need for a
standardized medium of communications becomes clear, should symbolism be introduced.

The important mistake not to make is to feel that a subject becomes more rigorous, more
precise or more mathematical because it is riddled with symbols. Greek letters are no
more exact than the English words that defined them. Many shoddy proofs go undetected
because they are disguised in opaque notation. If an argument is rigorous in symbols it
remains rigorous when expressed in English sentences. What it takes us 600 pages to
do here could have been condensed to fifty -- with complete loss of educational value.
CHAPTER 2

It is important to distinguish for the student what material from this chapter will be required
in the future and what will not. Obviously the silly languages L,, L,, L, and , are not
important for the rest of the book, though they do reappear briefly in Chapter 3. On the
other hand the language PALINDROME must become a member of our immediate family.
A big fuss should be made about the null word and the Kleene star. We have found that
Theorem 1 is a good introduction to the concept of proof. We challenge the students to
provide "an explanation of why this is true that would convince someone who didn't already
believe it." There is usually some obliging student in the class who doesn't understand the
result and when the other students try to convince him that it is not only true but painfully
obvious he is sometimes able to shake their confidence in their ability to articulate their
thoughts. This is the beginning of the understanding of what proof is all about.

| have assumed throughout the book that the readers are acquainted with standard
elementary computer programming concepts such as the notion of formal strings of
characters, concatenation, prime numbers, factorials, symbolic logic, etc. Other than that
the text is so self-contained that it defines many terms the student is already familiar with.
What could be the harm?

It is our habit to give exactly 20 questions per chapter many of which have multiple parts.
Problem # 20 is usually more theoretical in nature and often quite challenging. Sometimes
#20 is so difficult that it contains a hint that gives virtuaily the whole problem away. This
#20 is not too hard.
Chapter Two

S* has 4 2-letter words, 8 3-letter words and 2" n-letter words.

5 words of length 4; 8 words of length 13; 13 words of length 6. In general, the number of
words of length n is the sum of the number of words of length (n-1) and the number of
words of length (n-2), a Fibonacci sequence.

length 2: ab, ba; length 4: abab, abba, baab, baba;


length 6: ababab, ababba, abbaab, abbaba, baabab, baabba, babaab, bababa;
S* contains no words of odd length, and no word containing a triple letter substring. The
smallest word not in this language is a.

The string abbba is not in the language.


length 1: a; — length 2: aa, ab, ba; length 3: aaa, aab, aba, baa;
length 4: aaaa, aaab, aaba, abaa, abab, abba, baaa, baab, baba;
length 5: aaaaa, aaaab, aaaba, aabaa, aabab, aabba, abaaa, abaab, ababa, abbaa, baaaa,
baaab, baaba, babaa;
length 6: aaaaaa, aaaaab, aaaaba, aaabaa, aaabab, aaabba, aabaaa, aabaab, aababa,
aabbaa, abaaaa, abaaab, abaaba, ababaa, ababab, ababba, abbaaa, abbaab, abbaba,
baaaaa, baaaab, baaaba, baabaa, baabab, baabba, babaaa, babaab, bababa;
This language is all the strings of a's and b's where each 3 has its own a on its left or right.
No two b's may share the same a.

Factoring gives: aa|baa, baa\aba\aa, baa\aa\aba\baa\aa


No word can be factored in more than one way (look for the first odd length substring of a's
and/or the position of the first 5). No word can have an odd number of a's because each
factor contains two.

1 (xxx) and 8 (xx)- 9 arrangements (nine choose one)


3 (xxx) and 5 (xx)- 56 arrangements (eight choose three)
5 (xxx) and 2 (xx)- 21 arrangements (seven choose five)
total 86 arrangements

(i) To determine if a string is palindrome:


1) Ifthe length (string) < 2 then the string is palindrome, otherwise continue.
2) Compare the first letter(s) with the reverse of the last letter(s). If they match then
delete them both and repeat step 1.
Since x is palindrome, x = reverse(x). Following the algorithm to test x", two copies
of x are repeatedly deleted (one from each end, because they match) until the string
is reduced either to A (when n is even) or to x (when n is odd). Both of which are
palindrome, therefore x" is palindrome.
(ii) If a string is palindrome, then deleting an equal number of letters from the both ends
leaves a palindrome word. Hence, removing the front and rear copy of y from y°
which is palindrome, leaves the palindrome string y.
Chapter Two

(mi) Continuing the proofs above applied now to the palindrome string z", repeatedly remove
two copies of z at a time, one from either end, until if n is odd only a palindrome
string z remains. Ifn is even, then stop shrinking the string when zz remains. Note
that any palindrome can be viewed as a string concatenated with its own reverse. zz
is palindrome and zz = z reverse(z), implies that z = reverse(z) and z is palindrome.
(iv) The are four palindromes of length 3; aaa, aba, bab, bbb and of length 4; aaaa, abba,
baab, bbbb. For each odd length palindrome insert another copy of the middle
letter adjacent to it to make an even length palindrome.
(v) By using the algorithm in part one, we can reduce any palindrome to a central core of one
or two letters. On {a, b}, there are as many palindromes of length 2 (aa, bb) as there
are of length 1 (a, b). To make palindromes of length 2n, choose a core of length 2,
and then make n-1 choices for the letters to the left which determine the letters to the
right. To make palindromes of length 2n-1, choose a core of length 1 and then make
n-1 choices for the other letters. In each case n choices determine the word. Since
there are two choices for letters, there are 2" palindrome words of length 2n or 2n-1.

Here is an algorithm for finding z.


1) If length(x) = length(y). Then we have a palindrome of even length, so it must be of the
form z reverse(z), and x=y=z.
2) Ifthe strings are of different lengths then the longer one either begins or ends with the
shorter one. That is if length(x) < length(y) then xy = xsx, where y = sx, and symmetrically
if length(x) > length(y) then xy = ysy where x = ys. In both cases the substrings s are
palindrome (because they are each the center of a palindrome). In addition, the longer string
L is palindrome that is the concatenation of two palindromes, so repeat algorithm on L.

(i) Sc T, so S*c T*. 5bbd is the only word in T but not in S. However, bb € S so bbbb
e€ S* and T* c S*, therefore S* = T*.
(ii) Sc T, so S*c T*. However there is no way to generate bbb with the elements of S, so
Si71 Tt.
(iii) S* = T* even though S # T only when all the words in the symmetric difference can be
generated by words in the intersection.

10. No changes in the equalities and inclusions.

ee (i) (S*)* includes A even is S does not, so (S*)*= S*. S* = S** by Theorem 1.
(ii) There can be no factor in (S*) that is not in S*, (S*)" cS’. In general, any set
is contained in its positive closure, S* < (S*)’. Therefore (S")’ = S”.
(iii) Yes. IfA € S, then S* = S*. (S*)* = (S*)* = (S*)*. IfA € S then A € S* anyway, and
(S*)' =(S*)* = S*=S*UA = (S*)*.
12, No words in S* contain an odd number of b's (each factor contributes two if any), so none
of the examples is in the set.
Chapter Two

Is. This is the same as saying that the language L would allow all concatenations that did not
produce squares. First observe that A= AA, so A cannot be in the language. Consider
Ww, #W, andw,w,€L. Letw=ww, sinceA€Ll, wW#w, sow =Www, €L where
W, # W,, finally let w, = w,w, € L. However, w; = w,w,w,W, = W3W; Which is a square so
ws € L.

14. (S**)* = (S*)* = S* by Theorem 1. It is often bigger than S.

15. (i) no.


(ii) yes.T=S+
{w} > we T > we T%* and T* = S* > we S*

16. Let S = {aaa}, S* has one six-letter word and no seven-letter words nor eight-letter words.
However it is impossible for S* for any S to contain more six-letter words than twelve-letter
words because for every six-letter word w there is a twelve-letter word ww in S*.

We (i) All words over & = {a, 5} of even length.


(ii) S = { aaa, aab, aba, abb, baa, bab, bba, bbb }
(iii) All strings of a’s and b’s except A

18. (iv) S* and T* both represent the set of all strings of a's and b's. Therefore T must include
at least the words a and b, which is the set S.
(v) S={a, bb}, T= {a, aa, bb }.

19: The word abaaba disproves the algorithm.

20. Since T is closed and S c T, any factors in S concatenated together two at a time will be a
word in T. Likewise concatenating factors in S any number of times produces a word in T.
That is any word in S* is also in T. However we are given that T + S* so T contains some
words that are not in S*. We can conclude that S* is a proper subset of T, in other words S*
is smaller than T, and in symbols S* c T.
CHAPTER 3

Computer Science majors are familiar with the term "recursive programming" and this will
be very helpful with the concept of recursive definitions. The same idea of self-reference
is not carried over directly to "recursively enumerable" since that is a back formation from
"recursive functions." Students should be told not to worry that the mathematical examples
of polynomial and derivative set a precedent of a strong connection between this course
and mathematical analysis. These examples are just too perfect not to have been
included. if Calculus were properly taught the concept of recursive definition would have
been introduced there.

Theorems 2, 3 and 4 are simply illustrations of the theorem-proving power of recursive


definitions. They also serve as a pre-introduction to parsing. The discussion of well-
formed formulas is also tangential to the material in the text but computer students should
have mastered this useful tool by this time in their education.

Why have we defined recursion but avoided all reference to proof by mathematical
induction. The answer is simply that we hate proofs by mathematical induction. They
verify the truth of a result without shedding any understanding of why the claim is true or
where it came from. It is a fine tool for research but a poor one for education. It is
employed by lazy authors who wish to compress as many results into as short a space as
possible. The benefit of this in a textbook is a mystery to me.
Chapter Three

Rule 1: xismL,
Rule 2: [fw is in L, then so is wx.

There are eleven ways to prove that 14 is even.

2+2=4

4+2=6 4+4=8

6+2=8 6+4=10 6+6=12 8+2=10 8+4=12

8+2=10 8+4=12 8+6=14


V, .
=10+ 2=12 10+4=14 12+2=14
| eS
10+2=1210+4=14 12+2=14

10+2=12 10+4=1412+2=14 12+2=14 1242-14

12+2=14

2>4=>8 = 16 = 32 = 64 = 96 = 100: 8 steps.


To show that 2n is in EVEN: Keep adding to itself the largest number in the set until the first
result that is greater than or equalto n. Ifthe result equals n then adding it to itself gives 2n.
Ifthe result is greater then n, add to it the largest value in the set that will not bring the total
above 2n. Continue this procedure until adding that value gives the result 2n.

We must show 1) that all positive even numbers can be produced from this definition and 2)
that the definition produces nothing else.
1) We have 2 and 4 and 2+4=6, 4+4=8, 6+4=10, 8+4=12, ..., so ifthere is an even number
that cannot be produced from this definition it is large. Let us suppose that there are some
such numbers and let us call the smallest of them n, (that is n is the smallest even number that
cannot be produced from this definition). But that means that all smaller even numbers can
be produced by this definition, and in particular, that n-4 can be produced. So to produce n,
we apply rule Rule 2 to n-4. Since there is no smallest even number that cannot be produced
from this definition, all even numbers can.
2) How can we produce an odd number from this definition? If we add an even number to
some arbitrary integer n the resulting sum has the same parity as n. The increment in the
definition, 4, is even. Both of the elements known to be in the set (2 and 4) are even.
Chapter Three

Therefore application of Rule 2 will never alter the parity, and all numbers in the set will be
even.

We can make up any rules as long as they do not change the parity and providing that they
cover all cases. IfRule 1 lists the smallest n even numbers and the increment in Rule 2 is 2n
(the next highest in the set), both conditions are met. There are infinitely many such lists of
rules.

Use the following recursive definition of EVEN:


Rule 1: 2, 4, 6, 8 and 10 are in EVEN.
Rule 2: Ifx is in EVEN then so is x + 10.
Since adding 10 never changes the last digit of the number, all numbers in the set end in 0, 2,
4, 6, or 8. This defintion satisfies the conditions in the answer to Problem 5, so it will not
allow change of parity and covers all cases.

Rule 1: Any number is in POLYNOMIAL.


Rule 2: The variables x and y are in POLYNOMIAL.
Rule 3: If a and b are in POLYNOMIAL, then so are a+b, a-b, (a) and ab.

(a) 3 is nm ALEX (Rule 1). x is in ALEX (Rule 2). 3x is n ALEX (Rule 3). 2 is in
ALEX (Rule 1). x+2 is in ALEX (Rule 3). Therefore by ALEX Rule 1, x+2 and 3x
are both m ALEX. Since x+2 is n ALEX, by Rule 2a, so is (x+2) and by Rule 2g, so
is (x+2)™.
(b) Elementary calculus contains rules for differentiating sums, differences, products,
quotients, and exponetiations of differntiable functions. By the rules given here, if
these functions are polynomials, they are composed only of elements that are
differentiable functions and are therefore differentiable.
(c) No, when it is a matter of polynomials, the permissible functions are all defined in the
other rules.

(((((3)x)+7)x)-9) contains only two products: the product of 3 and x and the product of
(((3)x)+7) and x.

10. x? = xx (one step) x)? = xtex8 (four steps)


= xx’ (two steps) x8 = xx"? (five steps)
x! —= x2+x? x4 — x2 xx

x = xx! (three steps) x5 = xex?xtex8 (six steps)


x° = x*>x* X= xhex® (four steps)
x’ = x-x?-x" (four steps) x7 = xx! (seven steps)
=x Xx (three steps) x8 = x?-x76
= x-x* (four steps) xl? = xex?-x (six steps)
x10 = x2.x8 x70 = x4.x/8 (five steps)
x) = x-x?-x (five steps) x7) = xxx (six steps)
10

Chapter Three

x2? = x2-ytx/6 x77 = x+x7x-x/6 (seven steps)


x3 = xxx4.x6 (seven steps) xe= x*ex8-x16 (six steps)
m== x®.x/6 (five steps) = x-x*+x®-x!° (seven steps)
x? = x-x8-x!° (six steps) x00= xPex4exFx?
x26 = x?’ 8, xlé

fa Forbidden substrings of length 2:


++ -+ “4 /+ (+ (
i - ; c
+/ -/ L i (/
es, ’) /) ()
2. Forbidden substrings of length 3 that do not contain shorter frobidden substrings:
re Sie = fee (ee toto

LS: Not without very careful stipulation of many contingencies. It is much more practical to
allow them to accumulate and eliminate redundancy in (or reduce) the configuration later.

14. (i) Rule 1: Any letter is in Prep-Calculus


Rule 2: Ifx and y are in Prep-Calculus so are (x), ~x, x/A y, xV y, x> y.
CG SK Q 18
Cala (mea Gs
~) 5 BN es
V) YVR A N=
\) WN SENN KH
+) SN SA Ss

| (i) Rule 1: a, b and A are in PALINDROME.


Rule 2: Ifx is in PALINDROME, then so are xx, axa, and bxb.
(i1) Rule 1: aa and bb are in EVENPALINDROME.
Rule 2: Ifx is in EVENPALINDROME, then so are xx, axa, and bxb.

16. (i) Rule 1: 1 is ODD.


Rule 2: Ifx is in ODD, so is x+2.
(ii) Rule 1: 1, 2, 3, 4, 5, 6, 7, 8 and 9 are in DIGITS.
Rule 2: Ifx and y are in DIGITS then so are x0 and xy.

17. This is the set of positive rational numbers. Starting with Rule 1 and applying addition, we
get the positive natural numbers. Then applying x/y, we get all the fractions.

18. Def 1. Rule 1: 1 is im POWERS-OF-TWO.


Rule 2: If x is in POWERS-OF-TWO, so is 2 * x.
Def 2. Rule 1: 1 and 2 are in POWERS-OF-TWO.
11

Chapter Three

Rule 2: Ifx and y are in POWERS-OF-TWO, so is x * y.


The proof is rule 2 of definition 2.

12 (i) Rule 1: A is in EVENSTRING.


Rule 2: If wis in EVENSTRING, so are waa, wab, wba and wbb.
(ii) Rule 1: a and b are in ODDSTRING.
Rule 2: If wis in ODDSTRING, so are waa, wab, wba and wbb.
Gii) Rule 1: aaisin AA.
Rule 2: If wis in AA, so are aw, wa, bw and wb.
(iv) Rule 1: A,aandb are in NOTAA.
Rule 2: If wis in NOTAA, so are wb, and wba.

20. (i) xyz can be any word in the set. Rule | gives us an initial element. So far 3-
PERMUTATION = {123}. Let xyz = 123 and applying rule 2 gives zyx = 321 and
yzx = 231. So far our set is {123, 321, 231}. Now let xyz be a different element in
the set; xyz = 321. Applying rule 2 on xyz = 321 gives zyx = 123 and yzx = 213.
Only the second string is a new element, when added makes {123, 321, 231, 213}.
Apply rule 2 on xyz = 231 gives zyx = 132 and yzx = 312. Both ofthese can be
added to our set, hence {123, 321, 231, 213, 132, 312 }. Applying the rule to the
remaining three elements , 213, 132 and 312 does not generate any new elments so
we have verified the six 3-PERMUTATION’s.
(ii) Of the 24 possible arrangements of the digits 1, 2, 3, 4, only these 8 can be obtained from
this definition by the same method detailed in part (i):
1234 4321 2341 3214 1432 3412 4123 2143
12

CHAPTER 4

Throughout the text we stick to the alphabet {a,b} instead of the more logical alphabet
{0,1}. We are not the only text to do this. We find that O and 1 are hopelessly overworked
in Computer Science already: boolean, binary, bit string etc. We already use the plus sign
for disjunction and for union; if it sat between bit strings the temptation to mistake it for
addition would be too great.

In the text we emphasize the distinction between a language defining expression and the
language it defines -- perhaps we over emphasize this. In class we are certainly much
more careless. The fact that r* really means (r)* is another example of the confusion of
word and object that should be made clear. In this chapter we begin the theme of finite
versus infinite. The only infinite that we can really deal with is that which can be made
finite. In future chapters this motif will echo furiously.

Although it is not so mentioned Theorem 5 is proved by constructive algorithm and this


could be pointed out. The end-of-proof mark does not appear on Theorem 5 until after the
algorithm is illustrated once. Students might note that the inclusion of this illustration
"inside" the proof is for the purpose of being sure that the description given of the
algorithm is clearly understood. In writing a proof it is so important that the explanation be
understood that any clarifying discussion may be included.

The distinction we belabor between having a well-defined language (by regular expression
say) and actually Knowing what the language is, is crucial to understanding that the
concept of meaning is not (yet) properly part of mathematics. Hence there are no
algorithms for it and no theorems about it. This is what it is that makes Artificial
Intelligence artificial.

The complicated regular expression for the language EVEN-EVEN is worth covering since
this language will appear many times throughout the text. It will be revisited every time we
introduce a new characteristic of languages.
13

Chapter Four

The language associated with (r, + r,)r, is the language of all words made up of an initial
factor from the language associated with either r, or r, and a second factor from the language
associated with r,. The language associated with r,r, + rr, is the language of all words of
the following two types: a factor from the language associated with r, followed by a factor
from the language associated with r, or else a factor from the language associated with yr,
followed by a factor from the language associated with r,. But these are merely two ways of
saying the same thing: all words in either language must end with a factor from the language
associated with r,, and the first factor must be from languages associated with either r, or
r.

(aaa + b)* is the most obvious, or ((aaa)* b*)*

(a + b)* (s, + s, + 8, + s,) (a + b)*

a* b a* b a* (b + A) a*

(i) (a + b)* (aa + bb)


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

(b + A)(ab)* aa (ba)*(at+ A) + (a+A)(ba)* bb (ab)*(a+ A)

a* ((b + bb) aa*)* (b+ bb + A)

Building on previous results, we have the language aaa but no triple 5, to which we add its
a-b complement: the language with bbb but no triple a.
a*((b + bb)aa*)*(b + bb + A) aaa a*((b + bb)aa*)*(b + bb + A)
+ b*((a + aa) bb*)*(a + aa + A) bbb b*((a + aa)bb*)*(a + aa + A)

(i) b*a*
(ii) (at+ba)*b* + b* a(at+ba)* (A+ b)

10. b* (ab*ab*a)* b*

11. (i) a* (b(bb)*aa*)* b(bb)* a* + a*


(11) EVEN-EVEN (b + ab(bb)*a) EVEN-EVEN, where EVEN-EVEN stands for the
regular expression (aa + bb + (ab + ba)(aa + bb)*(ab + ba))*
(iii) ©EVEN-EVEN (ab + ba) EVEN-EVEN

12. (i) (atb)* ab (at+b)* is a regular expression for the language of all words containing the
substring ab: arbitrary ab arbitrary. (at+b)* a (atb)* b (atb)* is a similar regular
expression of the form arbitrary a arbitrary b arbitrary. The difference is the
additional requirement of some string between the mandatory a and b. Soto show
equivalence, we need to show that for all choices for the middle arbitrary string, a
14
Chapter Four

arbitrary b contains an ab. In all cases, once an ab is located then the substrings to
the left and right each become the new choices for the front and back arbitrary’s. If
the middle arbitrary = A, then the expressions are clearly identical. Ifthe middle
arbitrary contains the mandatory ab then just redefine the origins of the substrings
on either side. Otherwise the middle arbitrary does not contains the mandatory ab.
In this case the substring must either start with a 5 or end with an a, either way an
ab is formed by using the mandatory a or b that surround the middle arbitrary string.
Therefore the two expressions are equivalent.
(i1) (at+b)* ab (at+b)* represents all the strings containing the substring ab. Compared
with all possible strings of a's and b's, there are three types of strings missing: strings
of all a's, strings of all b's, and strings in which all b's precedes all a's. Since b*a*
contributes all these missing strings, the union is (at+b)*.
(iti) | Substituting (a+b)* for the expression in the brackets gives an equivalent expression
as proved in part (ii). Now the resulting expression is exactly the same as above.
(iv) | Wecan keep making substitutions of this kind, so there are infinitely more beasts in
the cave.

13: (i) Every word in S* is the concatenate of some finite number of words from S. Let us say
w is the concatenation of k words from S, then w is also an element of SX. Therefore if we
take the union of S* for all k, we get S*.
(ii) Obvious.

14. No. Let L=(atb)*. L and L* differ by only A, but L” and L* differ by A, a, and 3.

15; (i) (ab)*a alternates the a's and b's and tacks on an a at the end. a(ba)* starts with an
a and then alternates the b's and a's. Both define odd length strings the no double
letters, where every word begins and ends in a.
(ii) (a*+b)* can not generate more words than (at+b)* which already generates
everything. Moreover a* can just as easily be generated by (a+b)*.
(ii) Again (a*+b*)* can not generate more words than (at+b)* and either factor can just
as easily be generated by (atb)*.

16. (1) A* is all the words produced by concatenating factors of length zero. There is only
one such word, A.
(11) a*bis the language of all words that have exactly one b which is the last letter. Its
Kleene closure, (a*b)*, is the language of all words that do not end in a.
Concatenating the a* permits words to end ina. So (a*b)*a* gives all words over
{a, b}. Symmetrically, ba* is all the words that have one 3S, the first letter. Its
closure is all words that begin with 56. Concatenating the initial a* gives all words
over {a, b}, so the regular expressions define the same language.
(iii) Similar to the previous problem only this time both expressions define the language
of all words over {a, b} where the 5's occur in clumps divisible by three.
Chapter Four

17. (i) (atbb)*aa describes the language of string where 5's occur in even clumps and every
word ends in double a. Apply closure only adds the null word, because we can
already use the factor inside the parentheses to make any strings of aa.
(ii) Both expressions define all strings of only a's. (aa)* gives the even length strings and
then (A+a) gives the option of having no a’s at all or an odd number.
(ii) a(aa)* gives all strings with an odd number of a's. (A+a) gives the option of even length
strings of a's. Every word must have one final 6. +b allows for the string with no a's.
This is the same as any number of a's if any followed by a single b; a*b.
(iv) a(bata)*b describes words that begin with a and end with 5. In the body of any word
b's are always followed by an a (which prevents clumps of 5's), however the clumps
of a's are unrestricted. The associated language contains all strings where each b is
surrounded by at least one a on either side and that ends in b. aa*b(aa*b)*
describes the same language where each d is preceded by at least one a.
(v) The three cases are clear in the first regular expression: A or all words that begin with a
or all words that contain the substring aa. Since the entire second expression is
starred, it produces A. The second factor of the second expression (ab*)* generates
all words that begins with a. If the first factor, (b*a)* is taken at least once then the
last one juxtaposed with ab* produces the desired double a; ...b*aab*....

18. (1) all words that end in either a or bbdd.


(ii) all words that do not begin with 5 and in which 5's appear in clumps of even lengths.
(iii) A and all words in which both a's and b's occur in odd clumps and that start with a
and end with d.
(iv) A and all words in which both a's and 5's occur in odd clumps and that end with 5.
(v) A and all words in which both a's and 5's occur in odd clumps.
(vi) all words of even length such that (except for A) a's occupy all even positions.

ibs). (i) R=SR+T defines R as containing all the words in T and also all words composed of an
initial part from S concatenated with another words from R. This permits us to
continue concatenating factors from S as many times as we like, but we are still
required to finish the word with a factor from R. The only type of factor from R that
does not entail this kind of recursion is a factor from the language T. So we have any
number of factors from S followed by a factor from T, or we have just a factor from
T, equivalently S*T.
(ii) R=S*T defines R as words with any number (if any) factors from S followed by a single
factor from T. If the star operator is taken for zero, we have for R any word in T.
Suppose the star operator is taken once, then we have exactly one word from S —
followed by one word from T. But the word from T is itself a factor from R, so we
can see that this word is in the set SR. Consider what happens if we take the star
more than once; the star operates on S, so we have SR, SSR, SSSR.... all in the set
S*T. In fact, we established S*T in this way. The difference between S*T and S°T
is that only S*T contains T. But that means that S*T must equal exactly SR+T.
16

Chapter Four

20. (i) It is easiest to understand this in terms of the recursive definition of regular expressions.
Any letter of the alphabet is itself. a regular expression. The rule for forming regular
expressions apply to these and to all more complicated regular expressions. Each
regular expression describes a set. Equivalent expressions describe equivalent sets.
The equivalences are not disturbed by taking unions, products, or Kleene closures.
This means that any set must be considered a building block in the construction of
other sets.
(ii) With R = (ba*)* and S = (b+A), we have
(R*S)* R* = R* (SR*)*

(((ba*)*)*(A +b))* ((ba*)*)* = ((ba*)*)* ((A +b)((ba*)*)*)*


Both sides reduce to (ba*)*, (which is A + all words that begin with b).
17

CHAPTER 5

It is the policy of this text to define all machines in terms of their pictorial representation
and base all proofs about the machines on the ability to argue from the pictures. It is
therefore important that this ability be developed as early as possible. This chapter
contains no theorems, only a definition and illustrations, yet it is very important to begin
the ability to reason from pictures at this stage. Students should be reassured that
although there is also a non-pictorial tabular representation of these machines the pictorial
representations are every bit as sound a mathematical object.

All the pictures in this chapter of FA's are planar graphs whose edges are drawn without
crossing. Students sometimes need to be informed that if the pictorial representation
requires crossed edges this is perfectly fine so long as they are drawn so that no confusion
arises.

Problem #20 is actually an interesting theorem and if it is not assigned it should be


covered in class. It is another instance of the struggle of finite versus infinite.
18

Chapter Five
E

~oo} po)—m2 waSs +Q


“+
*
_~
19

Chapter Five

(1) (ii)
If all the states in an FA are final then automatically all
strings are accepted because no matter where the string
ends its accepted.

(iti) | Not necessarily, for the non-plus states may be unreachable as in the following FA:
ab a a,b
>)

(i)

(ii)

(iti)
20

Chapter Five

10.

a,
21

Chapter Five

(il)

C0 © ¢ }
oe b : b a,b b a bh b

a a,b co © me ; 7@) iO

b a,b b b b b A

a,b b a

(11) Transition tables for two-state machine has four cells, (two rows and two columns).
Each may be filled with 1 or 2. So there are 2* possibilities. There are still the same
four ways to designate final states. Hence the total number of different transition
tables is suprise 64, (2°4).

ive An FA with a designated start state and two other states can have up to three final states if
any at all. There is only one way it can have no final states, there are three ways it can have
one final state, there are three ways that it can have two final states, and only one way that
the FA can have three final states. For each of these 8 possibilities, there are 6 transitions
each of which can have one of three destinations. In total, there are 3°8 = 729*8 = 5832
different finite automata of three states.

2.

(iii) The language accepted by NIF is the reverse of every word in the language accepted by
FIN. If there was a path from one state to another on FIN by a sequence of letters,
then there must be a path on NIF using the sequence backwards.

(iv) The FIN pictured above accepts the non-palindrome words ab and FIN = NIF.
22

Chapter Five

(i) (ii) | An FA only retains its properties if the state bemg


removed could not actually be reached, that is a state
is removable only if there are no edges coming into it.
Clearly if the state is never used it has no bearing on
the language that the FA accepts.

(ii)

L5,

16.

a.b a

17 (i) The language of odd length words over {a,b} that end with a.
(ii) The language of all words over {a,b} of even length that end with a.
(ii) The language of all words over {a,b} of even length that have a's in all even numbered
positions.
(iv) (aatab+bat+bb)*a
(at+b)(aatab+bat+bb)*a
(aat+ba)*(aatba)

18. We start in state 1 and remain there until we encounter an a. State 2 = we have just read an
a. Scan any a's and return to state 1 on reading c. State 3 = we have read a 5 following an
23

Chapter Five

a. Reading an a puts us back to state 2 and reading a D sets us back to state 1. However
state 4 = we have just found a substring abc, and if the whole sequence was read the string
is accepted.
Now states 4, 5 and 6 exactly mirror states 1, 2 and 3. Returning to state 1 indicates that we
just found another occurrence of the substring abc. Being in one of the first three states
means that we have read an even number of abc substrings (if any) and are in the midst of
finding another one. Ending in an accepting state, 4, 5 or 6, means that we have read an odd
number of abc's.

ee (i) This FA starts out like a binary tree. For the first three letters of the input, each time we
read a letter we reach some node on the next level. However reading any more letters
traps us in the dead end state at the bottom. So no word of more than three letters
can be accepted.
(11) There are exactly three final states in the machine. Concatenation of the labels on the
transitions to these states shows that the machine accepts the words a, aab and bab.
(iii) Eliminate the three existing pluses. Place new pluses in each of the following states, the
right-most node on level two, the third node from the left on level three and the
second node from the right also on level three.
(iv) Each of the 15 possible strings with less than four letters is represented by one of the
nodes (excluding the dead end state). (Note the lexicographic order.) Simply mark
those nodes that represent the words in the language with plus signs.
(v) Since L is finite, we can determine the longest word and then build a binary tree large
enough to accommodate it. Next we add a dead end state with transitions from all
the leaves and itself: For every word in L, we place a plus in the appropriate, unique
state.

20. Even though the binary tree is infinite, the machine retains its uniqueness property (of the
machines described in problem 19) that the path for every string ends in a different state.
Surely just by placing the plus signs in the nodes that represent words in L would constitute
an Infinite Automata. However, the machine obviously fails to define the language because
no one can write an down the infinitely large picture.
24

CHAPTER 6

Transition Graphs are nondeterministic, and non-determinism is a horse of a different


color. Students who are forced to jump into nondeterminism at the level of complexity
theory (e.g. Cook’s Theorem relating hard problems to nondeterministic TM's) but who
have not seen nondeterminism at the FA level are confronted with a sea of gobble-de-gook
such as oracles and other demons.

It is stated in the text that TG's were invented in 1957 to simplify the proof of Theorem 6
which (as we see later) was proved in 1956. Clearly the 1956 version of Kleene's
Theorem did not explicitly mention TG's. We are committed to develop this subject in
logical order, not chronological order. Paradoxically, the oldest mathematics in the book
is the material at the end.

The transpose operator of problem #17 will be brought up again later and it may be useful
to cover it now.
2)

Chapter Six

2n1 6 (1)7 ah ba bb
a

5-18 a,b,c ake a,b,c 5-19


( yee (+)
abc

(i) 2,5 (vi) 1,5,6


aha sa 1,5,6
i) 1,2,4 vili) 5
(iv) 1,3;5;6 (ix) 5
(v) 1,2,4,6 (x) 3,4,6

If the TG has an odd number of states, simply add another state. If your definition insists that
the graph be connected, then add an edge from that state to any other state with an arbitrary
label. With no transitions to enter the new state, it can not possibly interfere with the
language accepted.

There are infinitely many TG's with two states over any non-empty alphabet.

Proof by constructive algorithm: Ifthe TG has no plus state, then we must add a plus state
that cannot be reached from the start state. Ifthe TG has only one plus state, no conversion
is necessary. Otherwise, add a new plus state. Then for every original final state, delete the
plus and add a A-transition from that state to the new plus state. Any string that was
accepted by the old machine will be accepted by the new one by taking the A-move, and no
string will be able to reach the new + state that could not reach one of the final states on the
old machine.
26

Chapter Six

(i) There are many machines that accept this (and other any finite) language. One way to
build this FA is to designate a start state. For every word in L, add a plus state and
a transition from the start state with the word as the label.
(ii) Two states are required; a start state and a final state. The one edge should be labeled
with each word in the language, ie. w, W, W, ...W, The reason why one state is not
enough is that the word x which is a concatenation of two words in the language
would then be accepted even though x is not in the language and there is no label x.

Make a new designated start state. Add two A-transition edges, one to the start state of TG1
and one to the start state of TG2. The final states remain the same as on the individual
machines. The resulting machine TG3 now accepts all the words that used to be accepted by
TGI by taking the A-edge to the old start state on TG1 and continuing to a final state.
Similarly TG3 accepts all the words that were accepted by TG2 and finally accepts no words
that were not accepted by either TG1 or TG2.

10. This time no new states will be necessary. Simply add A-transitions from every plus state in
TGI to every start state of TG2. Next erase the minus from the TG2's start state. Finally
delete all the pluses from any state that came from TG1, leaving the pluses in TG2's states
unchanged. The resulting machine TG4 is a connected graph that accepts words that are the
concatenation of a word from L1 followed by a word from L2.

ae L*. L* must contain A, and unless A was a word in L originally, it will not be accepted by
the modified machine.

12; (i) Ifthere are no transitions that lead into the start state then we can simply make this a final
state as well. Otherwise we can add a new state marked + with a A-edge to the
original start state and then remove the old minus sign.
(ii) Add A-transitions from all final states back to the start state. IfL1 includes A then stop
otherwise now apply one of the methods described in part one.

ee For every sub-expression that consists entirely of letters of the alphabet we can make a
separate TG that accepts it by the method described in problem 8 for finite languages. For
any sub-expressions that are joined with a + in the regular expression, their respective (mini)
Zi

Chapter Six

TG’s can be combined as described in problem 9. For each sub-expression that is starred in
the regular expression, the appropriate TG can be transformed as outlined in problem 12(ii).

14. Let us number the states clockwise from - (1) to + (3) and around to 6.
From start, there are two choices for the first a: 1-2 or 1-6.
1-6 If we take 1-6 edge then we have no more choices. We take the only outgoing b-
edge and arrive at 4, then bb-edge to 5, a-loop at 5, bbb-edge to 3, ab-edge back
to 4. The remaining string is ba and there is no edge we can take so the input crashes.
1-2 In this case there are five choices for bbb: 2-2-2-2, 2-2-2-3, 2-2-3-3, 2-3-3-3, 2-6.
2-6 gives us two choices for a:
6-1 in which the word crashes, or
6-3 in which case we loop at 3 for bbb, take the ab-edge to 4 and crash.
2-2-2-2 means that we use a to get to 3, then loop at 3 for bbb, take ab-edge to 4 and
crash with ba left unread.
(2-2-2-3, 2-2-3-3, 2-3-3-3) For the other three choices have read abbb when they
reach state 3. Follow the ab-edge to 4, bb-edge to 5, a-loop at 5, bb-edge to 6, and
finally a-edge to 3, where the word is accepted.
To summarize the paths are: 1-2-2-2-3-4-5-5-6-3, 1-2-2-3-3-4-5-5-6-3, 1-2-3-3-3-4-5-5-6-3.
And there are no other possible paths.

ies

(i) If there are edges out of the final state by which a string can reach the same or another
plus state, the new machine will accept not only the old language L and ba but also
the product of ba and all strings that can be formed by concatenating the labels on the
path from the final state to itself or another final state.
(ii) If there is a circuit that goes back to the start state, we face a problem similar to the one
above: the machine will accept L and ba and also the product of any strings that can
finish their processing in the start state concatenated with the string ba.
(iii) Add a new start state and a new final state to the machine and connect them with a
transition labeled ba. Connect the minus state to another minus state by a A-edge.

1th (i) Change all final states of the FA into start states of the TG for the transpose. Change the
start state of the FA into a final state in the TG. Reverse the direction of every
transition in the FA. This machine accepts exactly the reverse of every word in the
original language.
(ii) First apply all the steps outlined in part one. Then if the original machine was a TG not
an FA, then we must also change the labels. For each edge, replace each string that
28

Chapter Six

appears in the label with the reverse of that string.


(iii) LL, = {ww, | w, € L,,w, € L,} When w,, is transposed, the last letter of w,
becomes the first letter of the new word followed by the rest of w,
spelled backwards, then the letters of w, spelled backwards. This is exactly the
word transpose (w,) - transpose (w,). Therefore the transpose of the set L,L,
is transpose (L,) - transpose (L,).

18. We can think of a word in the language of a TG as the concatenate of edge labels on a path
from-to +. Suppose no edge of T has a label of odd length. Then no concatenate of edge
labels from T can possibly produce a string of odd length. So if L is the language of T and
if L contains an odd length word the T must have a edge with a label of odd length.

Jed For example: 6

20. (i) In a machine without A-edges, every transition consumes at least one letter of input.
Therefore, we know that Step 2 is true. The list in Step 3 is exhaustive; it includes
all possible paths through the machine (where a path is represented by the sequence
of integers that number its consecutive edges) and also sequences of integers that
cannot represent paths because the edges are discontinuous in the graph. There is an
upper bound on the magnitude of the integers used as edge numbers and on the
length of the sequence. Since the list is finite , we can actually check it for the two
required characteristics: 1) Does the sequence represent a path from a start state to
a final state, and 2) when we concatenate the edge labels, does the result match the
word we are testing? Since all possible paths are listed, if our test word has a path we
must find it. Since we test paths for validity at Step 4, we are not deceived by
matching edge labels on an invalid path. The list and the procedures we apply to it
are finite and so this algorithm must terminate.
(ii) In a machine with A-edges, it is not true that every transition consumes at least one letter
ofinput. We can use infinitely many A-transitions without consuming any letters at
all, and therefore we cannot set an upper bound on the length of the path that gives
us the test word.
29

CHAPTER 7

It is possible to teach Computer Science without covering Computer Theory but it is not
possible to teach Computer Theory without covering Kleene's Theorem. We believe that
the excruciating detail that we go through in this proof is completely justified and several
grounds:
1. the theorem is so important it merits a complete proof
2. the proof is a beautiful example of the strength of recursive definitions
3. it is an important illustration of the technique of proof by constructive
algorithm

There is one pitfall that we must be careful to avoid here. We do not want to over
emphasize the importance of developing facility with the method of conversion. It is only
important to see that conversion is possible; it is not important to "get good" at doing it.
In some courses it is necessary to drill on a new technique until the students become
expert in applying it. That is not the case here. The only purpose the examples and
problems serve in this chapter are to insure that the student really understands what is
going on. It would be misleading to give the impression that the ability to convert regular
expressions into FA's and vice versa has practical or commercial value.

This chapter usually takes a week to cover satisfactorily.

We have heard of zealous students (or intense teachers) who have written (assigned)
computer programs to perform the algorithms on FAs in this chapter. As long as it does
not displace some of the more important material this seems harmless.

Many authors prove Kleene’s Theorem by first proving that nondeterminism can be
removed from FA’s and then the result follows easily. We include this approach in this
edition without replacing the old method for two reasons. The old method is a perfect
illustration of the nature of how to design a constructive algorithm -- an art which can only
be taught by example. And the fact that there are two proofs for the same result is in itself
a great example of the power of mathematical techniques as opposed to mathematical
results alone. Each proof is in essence its own separate result.
30

Chapter Seven

1. (i) (atb)*b

SOHQ CHO OMmO


(ii) (a+b)ba(a+b)*

ab 2 a ab a atb

bb a bba

aba
a
ath 6 ieee eer +)
bba

(iii) (at+b)*(atb)

atb

abba

at+bt+abba

Oe G
Ona @
Chapter Seven

(iv) (atb)*

atb
a atb+baa(at+b)*abb
A
er baa A+baa
baa(atb)*abb
A
a @Q- *(A+baa) Se

(v) a*bb(at+b)*

a e? is abbatabb+bb atb

( )a* abbatabb+bb(atb)* ()

(vi)

a a
iy ab A ab —Oao)
baa bb bb
baaab
a
(-)*%> ab ©) ab(a+baaab+abbbb)*ab O
baaab abbbb

(atb)* (atb)* (atb)* (atb)*


(at+bat+bb)* a* (at+ba*b)* (at+bb*a)*
(b+aatab)* b* (bt+aa*b)* (b+ab*a)*
[(at+b)(at+b)]* A [(at+b)a*b]* [(atb)b*a]*
© @ © ©
32

Chapter Seven

a*b[(at+b)a*b]* a*b(atb)* = a*b(at+ba*b)* a*b(bt+aa*b)*


b*a[(at+b)b*a]* b*a(at+b)* — b*a(at+bb*a)* b*a(bt+ab*a)*
(at+b)[(at+b)(at+b)]* (atb)(atb)* (at+b)(a+ba+bb)* (a+b)(b+aat+ab)*

(i)

(ii)

(iti)

(i)
33

Chapter Seven

(it)
3

(i)

(ii)
34

Chapter Seven

(iit)

(iv) a b a
b a

(v) b a b a
a b 2

(i) a

(ii)

(i)
35

Chapter Seven
36

Chapter Seven

9. (i) The point is to ensure the option of getting from any final state to the start state. If there
is only one final state which is also the start state no changes are necessary. In
general, for every transition from the start state to some state x (possibly the start
state) with label /, add an edge from each final state to x with able /. (If the original
start had an /-edge to a final state then the resultant machine will have an /-loop at
that final state.) If there are no incoming edges (or loops) at the start state then
simply insert a + sign in order to accept A. However if there is even one incoming
edge at start then make a clone start state. The new start state is marked + and has
edges going to the same places as the original start state. (If the original start had a
loop on c, then there is a c-edge from the new start to the old start in addition to the
c-loop which is still at the old start.) After the edges are drawn then erase the - sign
from the old start state.

(ii)

(itt)

(iv)

10. 9(11)
9(iii)
37

Chapter Seven

Le (i) (ili)
a,b( 7 |; 7)a,b 6-"6r a,b
a b a a

12. (i) Both the machines and the languages are different. The product language does not
include A, while the closure does.
(ii) The product and the closure define the same language but the machines are different.

ee (i) The direct method usually produces less states.


(ii) Union ajen2” Un) eile
Product a) m" be
Closure a) 2" Db)

14. (i) (ii)

a b b

(iit) (iv)
a e
b a,b

b a,b b
38

Chapter Seven

(viii)

15; An NFA-A can be converted into an FA by treating states on the FA as representing a subset
of states on the NFA-A. Begin with making a new start state whose set includes the old start
state and any other state that can be reached from any state in the set by a A-edge. The a-
edge goes to a state whose set includes all destination states that could be reached by an a-
edge from any state on the NFA-A that is in the set of the source state on the FA. Likewise,
draw the b-edge to a state which represents the subset of states reachable from any state in
the source-state-set. For each new state generated in the FA, before drawing the appropriate
outgoing edges, add to its associated set any state that is reachable by a A-edge from any
state already in the set. When all the states in a set have no edge for a given able on the
NFA-A, then draw that edge to the dead-end state on the FA. However, if at least one
member of the set associated with a source state has an edge with that able then (even though
some other member state would have gone to the dead-end) the edge is draw to a destination
state whose set includes the optimistic choices. Continue drawing edges and generating new
states until every state on the FA has both an a and b edge. Any state on the FA that
represents a state which was final on the NFA-A must be marked with a +.
39

Chapter Seven

16.

17; (i) Union can be accomplished by adding a new start state with two A edges, each one
leading to an original start state. Then remove the extra - signs.
(ii) Product is made by adding A edges from each final state in FA, to the start state of FA,.
(iii) Similarly the closure machine is produced by adding A edges from each final state to the
start state and if the start state does not already have a + sign as well then add it.

18. (i) FA, A+a(atb)* FA, A+b(atb)*


S see b S yes
a


a

FA,+FA, — (atb)* FAFA, (a+b)*


a,b
a > {) a

do)
AQ

Chapter Seven

el
No paths from - to +, and this machine “oono words.
4]

CHAPTER 8

This is the first chapter with is truly independent of the rest of the book. Despite the fact
that it is mentioned later when output become a hallmark of Turing machines, the material
here is not required there.

However there are good reasons for not skipping this chapter unless absolutely necessary.
For one thing Mealy and Moore machines model sequential circuits and these are an
important topic in courses in computer logic, switching theory and architecture.

The topic of nondeterministic finite automata with output would very interesting to discuss
at this point. Students should be able to appreciate the fact that a single input may take
several different paths but not necessarily give different outputs. This question is
interesting but too much of a digression for the main text.

The AND and OR gates in the feedback circuits are not drawn using the usual symbols.
The purpose for this was to not disadvantage students who had not seen those symbols
before.

Some of the problems emphasize the nature of the transducer as a function from strings
to strings. The ultimate importance of this is beyond the scope of undergraduate
education.
42

Chapter Eight

2: (i) For each state there are two choices of output 0/1, hence there are 2* ways of labeling the
states. Then any of the eight edges can lead to any of the four states, that’s 4° ways.
So there are 2*4° four state Moore machines.
(ii) There are 2" Moore machines for n states where the input and the output alphabet each
have exactly two characters.

(i)

(iv)
43

Chapter Eight

1. (i) 100000 (ii) 000111 (iii) 111010 (iv) 000110 (v) 001001
3. (i) 000111 (ii) 010101 (iii) 011111 (iv) 101111 (v) 110101
We can convert a Less machine into a Mealy machine by the same algorithm that we used to
convert a Moore machine into a Mealy machine. (The start state of the Less machine will
have no print instruction unless there is at least one edge into it, but this is not a problem.)
To convert a Mealy machine into the equivalent Less machine, we follow the algorithm for
converting Mealy to Moore. We do not have to worry about a print instruction for a start
state with no in-edges since the start state of a Less machine does not print unless it is re-
entered. It is possible that there are other states in the machine that have no in-edges. Output
imstructions for such states can be chosen arbitrarily without affecting the results obtained by
running the machines, because such states can never be entered. (This point applies equally
to Moore machines.)

(i) (il) (iit) (iv)

a, | a/0 b/1 do b/1 a/0 do a/0 = qy | a/0_ b/I


b/0
aq, | a/1 b/0O- q, {| a/0 b/0 qi a/1 b/0
qi a/0
q, | a/l b/1 b/] q, | b/1 a/0

"Oe 0/1 i"

(li)

"COAG" ©L2O a/0,b/0


44
Chapter Eight

10.
New B= old A
Output = Input AND old B

A=0 and B=0 is qy


A=0 and B=1 is q,
A=1 and B=0 is q,
A=1 and B=1 is q,;
45
Chapter Eight

BE >)" 1/1

Lz.

(ii) The Moore machine is a natural language acceptor.

|
he (i)
0/0,1/1
0/0,1/1,$/$

(i1) There is no way of knowing when the last letter is read and its time to print the letter
that was read first.

14. Me, does not change the string at all so reprocessing a string through it any number of times
does not change it. Me, swaps I's for 0's and 0' for 1's but reprocessing will return them to
their original values.

hey This machine flips the first bit and preserves the rest. So to undo the effect on the first bit,
simply run the resulting string through the machine again.

16.
(yess Ou 1/0, Coe

a If Me, took two different input strings, x and y, and outputted the same string z then when
z is fed into Me,, it could at best produce one of the original strings x or y and Me,Me, would
not be an identity for all strings. So Me, must produce a unique string for each input so that
Me, can distinguish it.

18. It is easy to have a machine that does not change the outputs from Me,. Let us say that the
output of Me, is zero for all puts. Then Me, could be any machine at all and Me,Me, would
acts like an identity because (Me,(Me,Me,) = Me,. But this is not a true identity because an
identity must act on all inputs not just the one string that constitutes the only possible output
46

Chapter Eight

for a particular machine. So we note the fact that we confirmed in problem 17 that is that all
Me;'s that can possibly participate in an Me Mg identity must have the property that its
function is bijective. Hence for each input x Me, produces a unique y which when fed into
Me, reproduces x, likewise for each x fed into Me, the resultant y reproduces x on Me .
This proves that if Me,Me, is an identity then Me,Me, is also an identity.

19. M, flips just the first bit leaving the rest of the string unchanged if the first bit was 0,
otherwise it takes the complement of the entire string. M, recovers the changes that M
makes on a string. It flips the first bit alone if the first bit was 1, otherwise it flips all the bits
in the string. M,M, = M.M, = Identity. The two machines are inverses of each other.

20. After you read the first bit Mealy machines must produce the first output bit but there is no
way of knowing what the last bit will be at the onset of processing. So transpose cannot be
implemented by Mealy machines.
47

CHAPTER 9

This is a workhorse chapter. It is straightforward and exhausting. Students are often


tempted to try to take the shortcut of saying, "But | understand what the languages of these
two regular expressions are, and | can understand what the intersection is, and | can write
a regular expression for it." It should be explained to them why this is irrelevant to the
topic of the chapter.

Any student who has never seen a Venn diagram should still be able to work out De
Morgan's law for themselves.

This is another chapter in which it is possible to over emphasize the facility with the
techniques of conversion, while missing the basic point that it is only the possibility of
conversion that we are truly interested in.

This chapter is another good place to discuss the problems of finite and infinite. A student
should be able to notice that despite the fact that the union of two regular languages is
regular the union of infinitely many regular languages is not necessarily regular (assuming
for the moment that there are non-regular languages). This is because any language can
be considered the union of its separate words each as a regular language. On the other
hand students may want to test their skill at the conjecture that the intersection of infinitely
many regular languages is still regular, even if this intersection is still an infinite language.
48

Chapter Nine

1. bla+b)*a _ (begins with 4, ends with a)


Pet
init:
ie
ae
Py eee4
eon +i Ve
WS aos, il
vy
=A

2. (a+b)"aa(atb)*a (contains #2, ends with a)


fon ee 3 2 oe
ae hn ip
ao eis | a, ati ics
= ss =a A gh

3. ¢ (ends with 2, ends with 4)


4. bb(a+b)* (first letter is 4, second letter is 5)

ia! ( } Gast
2 yk? 7

5. (a+b)b(a+b)*aa(a+b)? (second letter is 4, contains 2a)


3,0 Q @ @
pone Shes =e Sa Fee
i-J i=) a i il ) y+e, 6
i Se =
3,2 a
6. (a+b) b(a+b)*b (second letter is 4, ends with 4)
2,4 a 2 : ae
ee Ste ae aa
ae Me } ee 4 Ia
ook 5 5
= pee

Vs $ (a never doubled, contains #2)


8. b*a(bb*abb‘a)*b’ (#never doubled, odd a's)
3

és Sea
(ao ta)
in
al a xs
Pig ee\
49

Chapter Nine

9. (b+A)(ab)*a (#not doubled, Snot doubled, ends in

es Seated

le liee lislialeas
a} ral a ie

al Sar |
Ne af ‘ ke
5

10. ¢ (starts with w starts with 5)


11. ala+b)* (starts with a)
ea

aes.
_ 2

~K
a Soas

on
12. a(a+b)*aa(a+b)* (starts with 2 contains #2)
7 Z g,0
ome ae ieee
i @) (+3
eas
3 |
49.9 A_, 2

PE ol)

(even length starting with 4)


23,0
a
Jee for
{

<a
eee
Nice

a5s
bs Fe ae Tbe
ale i+}

14. (aatab+ba+bb)*aa(aa+ab+ba+bb)* (even tength, contains a)


2 2
' ae i ; oo Fars ae 2 aoa y

he Ie el eae a

| | “af
Pe) ‘ I 3, Be 5
“j a = = io} Oo <

ak | ae oo
ar, eet ee aN!
33
50
Chapter Nine
15. (ab+ bb) (A + blab+bh+a(ba)*b?lb(ab+bb)*+a(ba)*]) (even length. no double a)
2
oe eae
§ yet
C It ! A ee

2

a ah)
(es

Cae
ie eee ee an

16. a(aatab+ba+bb)” (odd length, begins with a)


Se ;

rei i
ee a eee
a
a ) yo
(ed
17. (aa+bb+(ab+ba)(aa+bb)*(ab+ba))* (EVEN-EVEN)
2

oie ale
ps EA 2
al A |

oe
ae
ee

18. (i) same as 17 (EVEN-EVEN)


(ii) (ams bb+(ab+bax)(am+bb)*(ab+ba))*(ab+ba)(an+bb+
(b+ ba)(aa+bb)*(nb+ba))*
Po ep

ee
f)L EEE.
af)
i ee % ea,

19. (i) same as 18(it) (ODD-ODD)


(ii) ¢ (even length, odd length)

20. There are many such: the set of finite languages, the set of
languages of words of even length, the set of languages that
begin with an 2 etc.
51

CHAPTER 10

Both the pumping lemma and the pumping lemma with length are worth covering in detail.
If the example PRIME is skipped as being "too mathematical" then one of the examples
from the problem section should be substituted.

Students should also be encouraged to worry about whether all languages that are
nonregular can be proven so using the pumping lemma. This raises both the question of
whether the pumping lemma is a necessary and sufficient condition and the possibility of
true statements (e.g. that some L is non-regular) having no known method of proof.
Perhaps some more non-regular languages should be shown to be “pumpable” to drive
home the point.

An appreciation of what kind of conditions define a regular language should be developing.


Problem #20 should be very helpful in this respect. It is important to notice that there is
an asymmetry between the beginning of the string and the end of a string as far as
language-defining conditions goes despite the fact that the transpose of any reglar
language is also regular. Paradox? not so.

Myhill-Nerode is new in this edition. | didn’t include it in the first edition because its
customary application is to find the “minimal” FA that accepts a given language. This is
not a question that interests us enough to suffer through the distinction between the two
types of equivalence classes that the theorem requires. However, as an alternate method
of proving that certain languages are non-regular, it is simple, direct and useful. | like
having multiple approaches to mathematical chores. It adds depth of understanding.
Especially since the Pumping Lemma is difficult to conceptualize as a mere foo/ without
this parallel approach.

The quotient languages section is skippable. The only future reference made to this result
is in Chapter 22 in the proof that a Read-Only TM is just an FA. Both of these results can
be sacrificed if the whole book is attempted in one term.
52

Chapter Ten

(i) Let xyz be a word in {a"b"'}. Consider y, it could be one of three possible forms: 4 ,
a‘b*, or b*. Ify=a" then xyyz would have too many a’s to be a word in the language.
Ify= b* then xyyz would have too many b’s. If y = a*b* then xyyz would have some
a’s among the b’s instead of separated.
(ii) Let xyz be a word in {a"b"a"}. Consider y, it could be one of five possible forms: a’, a°b’,
a’b‘a’, b'a’ orb’. Ify =a’ then xyz would have too many a’s on one side of the b’s.
If y = b* then xyyz would have too many b’s. Ify=db6 ory=4bd ory=ba
then xyyz would have some b’s among the a’s instead of all clumped in the center.
(iii) Let xyz be a word in {a"b™}. Consider y, it could be one of three possible forms: d ,
a’b’, or b’. Just like part (i) doubling y would spoil the proportion or the order of the
word.
(iv) Let xyz be a word in {a"ba"}. Consider y, it could be one of five possible forms similar
to part (ii): a, a°b, a"ba’, ba” or b. If y= a" then xyyz would have too many a’s on
one side of the b. If y=b or y=a'b or y=a'ba’ or y= ba’ then xyyz would have too
many b’s.
(v) Using the stronger Pumping Lemma, |xy|<N (number of states), consider xyz = a“b‘a.
Hence y must be contained in the first set of a’s and when pumped the a’s would not
balance the b’s. So xyz is not in the language.

Consider the infinite set of strings aa*b. For each string x in this set, a different string z is
needed to make xz a word in any of the five languages. So in each case there are
infinitely many classes.

For this we need the Pumping Lemma with length condition. Let the FA have N states.
Consider xyz = a“ba“b a word in DOUBLEWORD. |x|<N so xy = av and when pumped,
it is no longer in DOUBLEWORD.

(i) Using the length condition in the Pumping Lemma, consider xyz = aXba“*!. y is a string
of a’s preceding the b. When pumped the number of trailing a’s will be too few to
indicate the length.

(ii) Consider the infinite set b*. For each string a different z is necessary to constitute a word
in TRAILING-COUNT. Therefore there are infinitely many classes.

(i) Consider xyz = a™bba™, where m is an even number greater than N, a word in
EVENPALINDROME. _y is a string of a’s preceding the bb. When pumped the
number of succeeding a’s will be too few. Likewise for ODDPALINDROME simply
consider xyz = a™ba™.

(ii) Again consider a*bb or a*b, and note that any such string will be in a different class.

(i) The squares have an interesting property. Consecutive squares differ by consecutive odd
numbers. 17= 1 27=14+3 3? =14345 947 = 143+54+7
53

Chapter Ten

This is because (nt+1)-(n)? = 2nt+1 = (an odd number.)


So the gaps between the squares grows larger and larger. For any number M
eventually no two squares will differ by M. Certainly ifx > M and y>M then x?-y’
> M (unless x=y) since the closest they would be is (M+1)’-M? = 2M+1 > M.
So when we pump, let s= 7°, a’ =xyz = a’a'a’ , the Pumping Lemma says that xyz,
xyyz, xyyyz, ... are all in a‘, which are a’*""4, "4 , @*"**4, _.... However, in this
sequence consecutive terms differ by the constant g, while squares get further and
further apart. Therefore these terms can not all be squares.

(ii) For each p, xa’ is a square for infinitely many x’s. (x=a"”, s =n’) However xa’ and
xa are both squares only when n’-p = m’-q for some n and m. But then n’-m’ = p-q
which cannot happen for large 7 and m. This is because, as we have seen above) the
gap between the squares keep increasing and get above p-q and stop there. Therefore
xa’ and xa! are in SQUARE only for finitely many x’s. The class of x’s which become
square by concatenation with @’ is different from the class of x’s which become square
by concatenating with a*. So a’ and a’ would require different states in an FA for any
different p and g. Hence the machine would need infinitely many states and
SQUARE is not regular.

(i) Consider w = xyz = ab" € DOUBLESQUARE. If, contains the substring ab then xyyz
contains two ab substrings and is not in DOUBLESQUARE. Therefore y is all a’s
or all b’s. In either case xyyz increases only one letter not both and so breaks the form
a'b”. Therefore w cannot be pumped and the language is not regular.

(ii) For each n, a’bx is only a word in the language when x = b”’. So there are infinitely many
different classes.

8,9. (i) The argument in 7(i) never used the fact that 7 was restricted to squares, so the proof
works equally well for DOUBLEPRIME and DOUBLEFACTORIAL.

(ii) The same as 7(ii). Notice that all three problems are special cases of the following
general theorem: Let n,, n,, ns, ... be an infinite sequence of integers. The L = a"'b™,
a@p™ a™b™.... is nonregular by either the Pumping Lemma or Myhil-Nerode.

10. (iii) Let xyz be a word in {a"b"c"}. Consider y, it could be one of five possible forms: 4 ,
a‘b’, b’, a’b’c’, b’c’ orc’. Ify isa string of all the same letter than xyyz would have
too many occurrences of that letter. If y= ab’ ory=ab¢ ory=6B¢é then the
order of the letters in xyyz would be mixed up.

(iv) Consider again a*b, for each string a different z is needed to make a word. So there are
infinitely many classes.

11. For each string in the infinite set a*, consider z to be of the form ba*b. No two strings from
54

Chapter Ten

the former set will both be accepted by the same string from the latter set. So there infinitely
many classes.

12. For each string in the infinite set (*x, a different number of close parentheses are needed to
bring the word to acceptance, so there are an infinite number of classes.

a: (i) IfMOREA were regular then a) MOREB would be regular by symmetry b) MOREA’ and
MOREB’ would both be regular by theorem 11 c) EQUAL would be regular by
theorem 12. But we have shown in the chapter (albeit also by indirect proof) that
EQUAL is nonregular. So we conclude that MOREA is not regular.

(ii) Because there is a way to divide words so that they can be pumped. Namely let y be
some substring of only a’s. If there were more a’s than b’s originally then adding
more a’s preserves (and enhances) this property.

(iii) Consider b*; 5° and b* where p<q are in different classes since a”''b” is in MOREA but
a’*'b* in not in MoreA.

14. (i) Infinite union is not regular. Let L be any nonregular language. Each single word of L
is a regular language itself, but the union of these infinitely many languages is not
regular.
(ii) Infinite intersection is not regular. Let L be any nonregular language. The complement
of each single word in L is regular. The intersection of all these languages is L’,
which is also nonregular.

jhe (i) R=(atb)*, N = any nonregular language.


(ii) R= any finite language, N = any nonregular language.

16. (i) Suppose PRIME’ was regular, then its complement PRIME would be regular but PRIME
was already shown to be nonregular. So PRIME’ is also nonregular.

(ii) The length of every word in PRIME’ has some divisor, k, that is PRIME’ = {a‘"}. To
satisfy the Pumping Lemma, let x = A, y = a‘, and z= a‘). Then xy"z = akaker)
which clearly still has a divisor k and is in PRIME’.

(iii) The Pumping Lemma sets the condition on regular languages. We use the contra
positive of the conditional as our proof method. Ifa language does satisfy the lemma
then no conclusion about regularity can be drawn.

17. (i) By Theorem 5, any finite set of words is a regular language. By Theorem 10, the set of
regular languages is closed under union, so the addition of a finite ser of words to a
regular language yields a regular language.
55

Chapter Ten

(ii) The difference between two sets is the intersection of the first with the complement of
the second. Since any finite set of words is a regular language its complement is
regular. By Theorem 12, the intersection of two regular languages is regular.

(ii), (iv) if nonregular + finite = regular then regular - finite = nonregular (in short hand)
and we know the latter to be false, hence the antecedent is false. So if a language is
nonregular, adding or eliminating a few words does not effect its “regularity”.

18. Given an FA that accepts P, take a word in Q, say e.g. bba. Then some (maybe none) of the
states of the FA have the property that from there bba will take them to a + state. Label these
states with a ty “bba’’. Do the same for all words in Q (in theory only since this is infinitely
many steps.) Any states in the FA that have a complete set of labels Dba, ..., for all words
in Q, are the final states of the FA for P/Q. Since these states all define Myhil-Nerode classes
and there are at most finitely many off them P/Q is regular.

19: (i) The strings (* already define an infinite number of classes.

(ii) Every string contains the substring () at least once. Let y = (), then pumping the word
preserves the necessary property.

(iii) Since in PARENTHESES every ) must be preceded somewhere by an ( so to every 5


will be preceded by an a.

20. (i) It should be clear from the discussion that S, is the finite set of words accepted by the FA
before entering the circuit and S, the finite set of words accepted by the FA on the
first trip around the circuit. Concatenating (a")* after S, simply permits any number
of trips around the circuit. [For a more complete discussion of ultimate periodicity see
Harrison, pp. 47-50.]

(ii) This problem clarifies the relationship pf the Pumping Lemma to the concept of ultimate
periodicity. No matter how many words we take to be in the finite set S,, and no
matter how large we make the circuit (i.e. the size of the finite set S,), the circuit will
bring the number of a’s in an accepted word to a magnitude expressed in base 10 by
an odd number of digits.
56

CHAPTER 11

Students have often complained that this chapter belabors some obvious points.
Decidability seems trivial as long as the question in question is decidable. As trivial as
decidability seems to them now, that is just how impossible un-decidability seems to them
later. When a class takes this position they can be given harder questions to puzzle over,
such as: given two regular expressions, without converting them into FA's, determine
through algebraic means whether they are equivalent. Once we have shown that the
question is algorithmicly decidable are there necessarily also algebraic algorithms? If that
question seems too easy (of course it isn’t, but who Knows whether someday a student will
answer it), ask them to decide whether the language of one regular expression is a subset
of the language of the other. Or find an algebraic way of writing down the regular
expression that defines the intersection of the languages of two other regular expressions.

If a class has had some graph theory they may want to investigate methods of deciding
whether the language accepted by an FA is infinite simply by looking at the graph itself
(disregarding labels). They will have to decide whether there is a path from a start state
to a final state that contains an circuit. There are such algorithms that involve pruning the
graph of redundant edges and moving the start and final states.
Chapter Eleven

Setse Ry Zp

| |JSy
Sh[|
hd
— Fy |
I} eH
oe I~.
SIs

+ m1 mn Tyed

am Ly]
Sy}
-
Pea
cE
I
2

ne
i)
ima cn
if Be
bel
pa

FA\NFA accepts at 9143, HR. Bh. 4


FA; MFA, accepts at or, 951

FA,;AFA2 accepts at @, ALG


FA; NFA, accepts at 91,

FA NFA» ° accepts at Ql, NH, BY, % £3, Wl}, 94 13, Bl, BR

FA, MFA, accepts at 9g)


A: Neither machine has any nonfinal state. Therefore neither
machine's complement can have any final states at all.
Therefore neither intersection machine can accept any words.
58

Chapter Eleven

These machines do not accept the same language. FA, accepts


A and FA, does not. The machine for FA,AFA,° will have a
final state at the start state.

Aas
"t)
7h;
|ON
4

nal
»
Pod
YL
pba
t_ e
Lair
|)
Sey
Sint)
S|]
ee er, vy.
sy j—
lal
"Sy

eT
RI
Se ee |
Ord
Cod
"ay
es

FA|NFA, has final state 13 > = s¢

59 and A3)omme 58:


FA,NFA, has final states 1 yy = 5; and 2 3 = 54.
44a, aab, aba abb, baa bab, bba bbb, aaaa aaab, aaba
44D6, abaa, abab, abba, abbb, baaa, baab, baba, babb,
bbhaa, bbab, bbba, bbOb, aaaaa, aaaah, aaabha aaabb,
88D 82, 88D&D, 28bba aabhbb, 262028, a2bhaakh, ababa, ababb.
2H6a4, abbab, abhbhbha, @abbbb, baaaa baaabh, baaba baabb
Dbabhaa, babab, babba babbhb, bbhaaz bbhaabh, bbaba
bbhabhb, bhbhaa, bhbhabh, bhbba bbbhbb
59

Chapter Eleven

VE Sow 10. no bplter 110 i700


(i) finite (ii) finite (iii) infinite (iv) infinite
The blue paint algorithm for FA‘s works equally well for TG's.
The process of testing all strings in the range VS length of
string <2 works equally well for NFA’s.
The process of problem 15 works as well for NFA-A’s.
By Theorem 15, an FA with /¥ states that accepts a non-empty
language accepts a word of lengths VV Since the original
machine was an FA, we know that the final state must have
had out-edges. By changing all of these into loops, we change
any string that would have passed through a final state into a
String that dead-ends into a final state. No matter how many
fewer than / letters it takes to get to the final state, the rest
of the word will stay at that state. Therefore, since a string of
length N is long enough to reach any final state in the machine,
it must be accepted if any of its prefixes is accepted.
This is an effective procedure. Because the original machine
was an FA, we know that there are exactly as many edges
leaving each state as there are letters in the original alphabet.
(When change the alphabet to& = {7} we may get some
duplication of edges, but this does not change the upper
bound.) Suppose there were a letters in the original alphabet.
Then, at each of the /V letters in the string to be tested we
have a choice of exactly a edges to follow, so the upper bound
is
18. Follow the zedge out of the original start state and paint its
destination state blue. From this point (instead of from the
original start state) follow the blue paint procedure of the
chapter.
ee (i) Test all strings of length ¢ VW that contain a 4. If the
machine accepts none of them, it accepts no words that contain
a 5. Alternatively, copy the machine. Follow the 4edge out of
start and (as in Problem 18) proceed from there to see whether
60

Chapter Eleven

the machine accepts any words beginning with 4. If so, stop;


the answer is yes. Otherwise, proceed. Follow the 4edges out
of each of the states that can be reached on one letter. Apply
the blue-paint algorithm from there, etc.
(ii) Test all strings of even length:
20. Construct FA, for the language of r,; and FA, for the language
Of f>. Intersect FAz with FA,’. If the resulting machine
accepts no words, then the language of r, is contained in the
language of ry. (If it is properly contained in the language of
f>, the intersection of FA, with FA,” accepts a nonemply set.)
61

Chapter 12

On the surface we present one primary reason for introducing CFG’s: regular expressions
were inadequate to define all interesting languages and therefore we turned to the metnod
of defining human languages to find a more powerful tool. If this were really the case, and
our whole goal was to find the most powerful language defining structure, why did we
waste time on Part | at all since Part Il is better (dominating), and Part III will be better still.
There must be a reason why Parts | and II exist. Students should be asked to figure this
out for themselves.

Loosely speaking, Chomsky’s goal in inventing generative grammars was that he had a
fundamental theory of why humans can learn to speak and why all human languages are
So similar (assuming that they are). He reasoned that all human brains come hard-wired
(like nest-building in birds) with a language grammar of everything but the terminals. Each
human language invented need only supply the terminals “chair’, “table”, “cat”, etc. Infants
know the grammar already and are eager to learn the terminals so that they can form
sentences. | like to have the students speculate on this possibility and to contrast it to
computers, programs, and Al. If this were really literally the case why would so few
human-uttered sentences be grammatical?

| think the term Polish Notation for Lukasiewicz Notation is politically incorrect and
demeaning. The man deserves personal credit, not synecdoche. This is also a topic
which, if not already presented in some other course, all CS students should learn.

To make the distinction between ambiguous grammars and ambiguous languages


meaningful takes some classroom discussion. Just to say that there is a distinction will not
impress the students as much as forcing them to explain it themselves.
62

Chapter Twelve

There is only one nonterminal S. There are only two productions, S > aS and S > bb.
We must apply the second production exactly once to eliminate the nonterminal S from
the working string and form a word. (Therefore all words in the language of the grammar
end in bb.) As soon as we apply it we are finished, since there can never be more than one
occurrence of S in the working string. Ifwe apply the first no times at all we make the
word bb. If we apply it exactly n times, we make a"bb. Thus, we can make all of a*bb. We
can make nothing else because we have only the two productions offered, and the
substring bb must terminate the word.

The grammar has three nonterminals. S > XYX gives us a word of three factors. The
outside factors are each of type X which we recognize as having the form as grammars for
the language of the expression (a+b)*. The center factor is type Y, which allows only the
string bbb. Since all strings in the language start from S all must have the Y factor in the
middle, with an X factor concatenated before it and one after it, just as in the regular
expression (at+b)*bbb(at+b)*.

(i) a(atb)* The X factor in this grammar is exactly like the X factor in the grammar of
Problem 2, and generates the same language, namely (atb)*. Since S gives the
single production S > aX, the grammar produces all words that begin with a.

(ii) (at+tb)*a(at+b)*a(at+b)* Again, what the X factor produces is entirely arbitrary. The S
production requires exactly two a's (in addition to any that might be introduced
from X). So this grammar gives the language of all strings that have at least two
a's.

(i) By taking X > A, we get the null string from X. We must apply this production once
to eliminate the nonterminal X. To make the substring b" for any 7, we apply the
other X production exactly n times before eliminating X.

(ii) We have shown that X gives us all of b*. We can derive any of the strings in the
language from wherever the nonterminal X occurs in a workins string, by applylng
productions as shown above. The requisite a’s are included in this S production.

(iii) Consider the two productions S + SS and S > A. By applying the former production
n+] times and then applying the latter to any one of the resulting S’s, we derive a
working string of exactly n S's. Since we have shown that S can produce any
number of S's (including 0), we see that we can use it to derive something we
might call S*. Each S can generate XaXaX, which is a word from b*ab*ab*.
Therefore the language generated is (b*ab*ab*)* = [all strings with an even
number of a’s]-[b*]+[A].
63

Chapter Twelve

(iv) This language includes no words with an odd number of a’s, since each repetition of
the factor(b*ab*ab*) introduces exactly two of them. The number of b’s
introduced with any factor is entirely arbitrary. However, there is no way to
generate any b's at all without also introducing two a’s. Therefore, of all the
words with no b's, only A can be generated.

(v) We have already shown that we can make all words over (at+b)* that have some
positive even number of a’s, and A. We must show that adding the new production
S > XS to the grammar gives us the possibility of producing the words in b*
without adding undesirable strings to the language. By using the sequence S = XS
= XA = X we can easily see that we can now derive the strings in b* from S.
Since we have not introduced an unpaired a into the grammar, we may safely
assert that we can now produce all strings with an even number of a's.

This grammar generates the language of all words that either begin with a or contain the
substring baa (or both). The word abaa can be derived in two ways:
S = aX = abX = abaX = abaaX = abaaA = abaa
S = XbaaX = abaaX = abaaA = abaa

(i) Sentence = subject predicate


= noun-phrase predicate
= adjective noun-phrase predicate
= adjective article noun-phrase predicate
= adjective article noun predicate
= adjective article noun verb noun-phrase
= adjective article noun verb adjective noun-phrase
= adjective article noun verb adjective article noun-phrase
= adjective article noun verb adjective article noun
= itchy the bear hugs jumpy the dog
(ii) Change rules 3, 4 and 5:
3: A noun-phrase can be an article followed by an a-phrase.
4. An a-phrase can be an adjective followed by an a-phrase.
5: An a-phrase can be a noun.
(iii) Sentence = subject predicate
= noun-phrase predicate
= article noun-phrase predicate
= article article noun-phrase predicate
= article article article noun-phrase predicate
= article article article noun verb noun-phrase
= article article article noun verb noun
= the the the cat follows cat
(iv) The alternate rules listed in part (ii) prevent this.
64

Chapter Twelve

(i) S- Sbla

(ii) S + XY
X-+aX|A
Y-bY|A

(iii) S-+SS|baa|abb|A

(i) S-+aS|bxX|A
X-aS|bY|A
Y-aS|A

(i) S-aS|bY
Xoax
|bY
Y->aY|bZ|A
Z->aZ\|A

(ii) S-bS|aX|A
X-+aX|A

(iv) S-aS|bX|A
X-bxX|aY|A
Y->bxX/A

(v) S > aX|bY


X > aX | bW
Y= bY | aZ
W > bW|aX|A
Z->aZ|bY|A

Consider the choices of productions from nonterminal A. Eventually, if the grammar


produces a word, each A in the working string must be replaced by exactly one a.
Furthermore, the choices A > bA |Ab mean that when we are adding the terminal 5 to the
working string we are not changing the number of A's in that working string. The
production A > AAA means that we can at any time add exactly two A's to a working
string. This will not change the parity of the number of A’s in the working string. The first
production in the grammar gives us two A's, so the parity must be even. Eventually, each
of those A's must terminate as a’s, so there cannot be a word without any a’s in this
language. This language is the langauge defined in Problem 4 without A.

This grammar generates the language of all words containing a (positive) number of d's
divisible by 3.
65

Chapter Twelve

Le S - SS| EXE
X-aX|a
E + EQUAL

12. | We can convert the CFG for any CFL into its transpose by replacing the right sides of
each production with its reverse: S > XY becomes S > YX.

13. Inthe productions, the nonterminals on the left side of S become the letters of the word
and the a’s on the right side of S keep count of the number of letters in the word.

14. (i) At least two;


An observation: I conclude that the watch is broken.
A command: It must be done - break my watch.

(1) I had a book and someone stole it.


I had} book
a stolen

I arranged the theft of a book.


I had stolen

I was at the point of stealing the book when...


I | had j_stolen
a
book

15. (i) CFG 1 S CFG 5 S


/\\ /\\
ab ak
b

(it) CFG 2 S
/\\
CFG 3 S
/\
CFG 4

3 aS

" ay
A
Xs

a a
66

Chapter Twelve

CFG 5

CFG 4

CFG 5

>=

O oyOo N CO iy,oO LF
ay

TS
2s
na ae
De

x, Nx
=Naa
67

Chapter Twelve

(ix) CFG 5 S
/\
bA
/\
aS
/\

ab
16. (i) babababab
(ii) aab
(iii) aaaaaaaa
(iv) aaa
(v) aaaa

hy. (i) S = XaX = aXaX = aAaX = aAa= aa


S = XaX = aXA = aaX = aaA= aa

(ii) S = aSX = aaSXX = aaAXX = aaXa = aaaa = aaaa


S = aSX = aAX = aaX = aaaX = aaaa = aaaa

(ii) S = aS = aaS = aaA=aa


S = aaS = aaA=aa

(iv) (i) S + bS | aX (ii) S + aX (iii)S>aS|bS|A


X > aX|bxX|A X->aXla

(v) (i) S>+bS|aX|la (ii) same as above Gi) S > aS|bS|a]b


X > aX|bX|alb
68

Chapter Twelve

18. (a) not ambiguous


S—a
aS — aa
aaS — aaa
= aaaS - aaaa
aabS - aaba
abS— aba
ae abaS - abaa
abbS - abba
bS — ba
baS — baa
baaS - baaa
babS - baba
bbS — bba
bbaS - bbaa
bbbS - bbba
(11) ambiguous
S— b
sas abaS abab
abaaSaS
aSab abab
aaSaSab
aaSaSaS —— aabaSaS
a. gaaSaSaSas
aaSaSaS —— aaSabaS
ya aaSaaSaSaS

aaSaSaaSaS

(iii) not ambiguous


S— a
aSa — aaa
ae aaSaa aaaaa
abSba ababa
bSb — bab
baSab baaab
bbSbb bbabb

(iv) not ambiguous


S— aSb— aaSbb aaaSbbb
|_______ gabXbb
abXb abbb
ees ADD
69

Chapter Twelve

ees Se ery
Leen DHX DAP
L__ bbbX- bddd

(v) ambiguous
S— bA— ba
baS — babA baba
L___ baaB baab
bbAA- bbaA abaa
bbAa abaa
bbbAAA
bbAbAA
bbaSA
bbAaS
aB — ab
abS — abaB abab
= abod abba
aaBB- aaBb aabb
aabB aabb
aaaBBB
aaBaBB
aabSB
aaBbS

19. (1) 7 PS)


(ii) 4*123
(iii) 1-23
(iv) **1+234
(v) +*+1234
(vi), -1*2734
(vii) ++1*234

20. The binary operators are V, A and >. The unary operator is ~. Using prefix notation, we
can distinguish between infix ~pVq = (~p)Vq and ~pVq = ~(pVq) by writing V~pq and
~Vpq respectively. Construct a grammar analogous to the one used to generate the prefix
trees and proceed in the same fashion.
710

Chapter 13

We make an enormous big deal out of Chomsky Normal Form, in part for the parallel to
regular grammars, in part for proving later that there is a PDA for every CFG, in part for
parsing by CYK, and in part to prepare for Kuroda Normal Form for CSLs. However the
true reason for all the fanfare is that this conversion in its naive form provides two
wonderful examples of when what seems like a completely well defined algorithm is
actually not.

If the teacher would rather cover the whole mater in one class, fine.

Notice also that there is mathematical induction disguised in the proof of the grammar for
EQUAL.
q3
Chapter Thirteen

1. (1) Sa SS| 2aa| D


Hie
X79 aX| DAA
¥3 aaa| b66
(Gh) Sa aX] OS|A
X> aF| 6S5\4
ko OS\A
tv) $3"a5 [or
X> aXk| OY
Yo aS | bLX\A

(Vv) S+ aaS| 2b5| baS| 663\ a| 6


(Vi) Sa HF
X> 6X| Xl a2
¥> aF| Fa\ b
Vil) 5 + YF
X > bX aw
Vo bW\| aXla
ERE BAIA
LZ > AZieby
ee (1) S + aaaS | bS |aaa] b
(ii) Sas | bSi) X
X — aaaY |bbbY
Yeay Loy | A
(vi) S+bX|aY|alb
X > bX |aB B->bB|A
Y-aY|bA A-aA|/A
The other four grammars are already in regular format.

3 Gi) b*(a+b)a* nonnull words in which all @s follow all 4's


(i) b*(ab*a)b*(aa+b) nonnull words with an even number of 2s
4 ()) (aa+ab+ba+bb)* words of even length
(ii) (ab+ba)* words of even length where no letter is tripled and
initial and final letters are not doubled
t2
Chapter Thirteen

(i) a(ba)"(b+ba) + b(ab)*(a+ab) words of length 22 where as


and 4's alternate
(i) a*(b+A)a*(b+A)a*a words ending in a having at most 2 4's
(i) a*(a +bla*(a+ba*a+ba'ba'‘a)|]) words ending in # having at
most 3 Js
(=e=e
~~ (a+b )*aa(a+b)*(a+b) words including the substring 2a
followed by at least one more letter
Le SSS ECS SES Sab
(ii) The language is nonregular. (Parentheses may be
arbitrarily deeply nested.)
(i) S4 a2S| DSIA
(ii) S>+ zaa¥
PS Dae
(iii) S>+ aa¥
X> axlaA
Every edge of the TG ts labeled with a word. Give the states
names (as in the FA algorithm) and then, for each transition,
create a production of the form
STATE OF ORIGIN? (edge /abel!) DESTINATION STATE

We can turn the bad grammar.into a machine of the type


formed in the proof of Kleene’'s theorem that is like aTG but
with edges that are labeled with regular expressions. Consider
each nonter minal to be a state, from which there will be one
transition to correspond to each production. The A-productions
all go to a final state. This machine can be reduced to a regular
expression by the algorithm of Kleene's theorem.
w2

Chapter Thirteen

Il. G) Sa aX] bX al b
X> al b
(ii) S> aX] bS| al b
Xa akla
(ii) Sa aS| bY) 6
Xa axa
(VY) Sa Yar] O41 al Yel 2741 5
X> Xank) Xal a¥1 a] X67 X61 OF] 6
12. (i) Choose a symbol for a new nonterminal, * , and add the production * > A. This will
not change the grammar because *¥ is not derivable from S.
(ii) Add a new unit production * > N, where N is some nonterminal already in the
grammar. Again this will not adversly effect the grammar for the same reason as
above.

(3, (1) Sa aS| YO


Ya bY\b
(ii) Sa AA| BB
Aw BB
Ba abB\ b\ 66
Ci) Sa AB| 2B\ 2| Bb| 5
Aw #2B8\a| BOS
Ba a8\a\| Bb| Db
14. Ui) S+ SS\ 2 (already in CNF)
(ii) S> aR| SRI 2
R> SA
Ata
(ii) Sa AR
Ra XX
X> AS| BS\ 2
Aza
Ba B

(iv) E> EP £-./


E> ET ii

£4 LR |
Ae
74
Chapter Thirteen

Pa» XE L (
T> ¥& Cas
Ra &C
(Vv) \1- and 2-letters] S> a] 5] AA| AZ| BA| BB
is-tewers)| = S-+ AR | AR ARL AR |BR |BR | BR | BR
(4-letters} S23 R, £y | Ry 3 By Bs | Ry ky | ky ky | By; | By |

Ry Ry | RyRy | RyRy | RR
[5-letters| S+ Bks | A fs, | Ay | eA | fy B| Ks,A
[6-letters] S+ Ry Ks,
hy > AA Kh, BB 7 AB
h,7- AB Bs > Ry ky Ata
> BA > hy A Ba b

(vi) S+ GR,\SA\AS\ 4
5+ RRs| RyRy | As) 2B | Bl RBI AR | AB
S94 Ky es,| hy Ks | ky; | Rs Ay | Ay A | A A | BR, BA

hy> AS Ky SA Avta
hy SB Bs + SH; Bo b

k,> BS

Vii) No change. This grammar generates no words, but it is in CNF.


iS G) S+ AA 27 AA
X4 AA A+ a
Y> AA
(ti). S72 SS| AS\ a
Ar SS| AS\a
Not necessarily. For example if the first rule n a CFG was S > XS |XY then adding the
rule S > A would change the language. Instead, we need a new start symbol S’ and the
productions (not in Chomsky normal form) S’ > S| A.

(i) S= AA = aBA = abBA = abbBA = abbAA = abbaB = abbaA

Yes. For example the word aababb can be derived in at least two ways from the
srammat>S
AY | 2OC VY, XY| enya ey,
TS

_ Chapter Thirteen
19. Draw the derivation tree for the word in question. Start with
the original production from the Start symbol & Thereafter,
find on the tree the node corresponding to the rightmost
nonter minal in the working string and apply to the
nonter minal whatever production shows in the derivation tree.
Since, at any point in the derivation of a word, if there is at
least one nonter minal in the working string, there is a
rightmost nonter minal, this works for all grammars. Since
every word derivable from a CFG has at least one derivation
tree and since that tree (by definition) terminates when the
word is formed, the process is finite.
20. Since the conditions specify that A is not a word in Z we know
(by Theorem 21) that there is a CFG in CNF for Z. We derive
our grammar from this one. We Know that in the process of
deriving a word in Z we start with a string of all nonterminals
(the single nonterminal 5) and end up with a string of all
terminals (the word). We observe that whatever nonterminals
were used in the derivation process have all been turned into
terminals eventually, since none can have been eliminated
directly by applying a A-production. We do not have to worry
about both nonterminals on the right af a production arrow; if
we know that they will both eventually turn into terminals
then, in particular, the first one will. Start by separating the
productions.
Step |. For all “dead” productions, that is, of the form WV a,
act, paint x blue and paint the nonterminal on the left of the
production arrow red (at that place only).
Step 2. Wherever a nonterminal that is red somewhere appears
to the right of a production arrow, paint it red.
Step 3. For all productions having a right side that begins with a
red nonter minal, replace each of them by the set of all blue
terminals that can be generated by that red nonterminal. Now
paint the nonterminal to the left of the production arrow in
76

Chapter Thirteen
each of these productions red (again, only at that place).
Repeat Steps 2 and 3 until nothing new is colored. All useful
productions (see Chapter /9) now begin with a terminal. All
useful nonterminals will be red. Any productions that do not
now begin with a termina! cannot ever be used in the
derivation of a word.
ars

Chapter 14

We define PDAs as pictures, not i/lustrated by pictures. Manna does this too, but slightly
differently. We correspond runs of inputs to trace tables, but we never replace the pictures
by tables (that is until the proof that every PDA corresponds to a CFG). To define PDAs
(or FAs or TMs for that mater) by transition functions is a simple exercise in the elementary
theory of using function notation but it adds nothing of pedagogical merit to the discussion.
We could eliminate the pictures from all of graph theory and be left with graph-less theory
with no intuitive combinatorial value. Let us not do this.

PDA languages can also be defined by acceptance by empty STACK, completely read
TAPE, etc. If we had world enough and time we would prove the equivalence of these
other definitions to the one given here, but this task seems less exciting than progressing,
as quickly as possible, to TMs where the true surprises lie.

it would be nice to prove by an elementary argument at the early stages of this chapter,
that PALINDROME cannot be accepted by a deterministic PDA. When | figure out one
of these slick proofs | will print a new edition. Till then the necessity of the non-
deterministic nature of PDAs will have to rest on arguments like the one given here. That
generation by CFG allows (and requires) choice and that that is wny PDAs, in order to
correspond to them, must also have choice in the nature of non-determinism, is bogus.
Remember that generation of words from regular expressions also allows choice but FAs
can be defined as deterministic (this is because for them NFA=FA). So this feeling is
imprecise to the point of being misleading. Try to do something about this in class.
Personally, it is still a heuristic mystery why CFLs are not closed under complementation.
78

Chapter Fourteen

ACCEPT

3. (i) STATE
START

(ii) STATE
go
Chapter Fourteen

(ii) STATE STACK


START A
READ, A
PUSH «#

REJECT
(iv) STATE
START
READ,
PUSH @

ACCEPT
Ageia
2p22)
(ii) S > 4555| 2656
(iii) No. (It can be proven nonregular with the weak form of the
pumping lemma.)
80

Chapter Fourteen
5. (i)STATE STACK
START A
READ, A
PUSH #
81

Chapter Fourteen

(iii) STATE STACK TAPE


START A aaabhaa
READ, aabaa
PUSH « aabaa
ahaa
ahaa
baa
baa

By

>o>Oo
Oo
&m&
mem

TAPE
aaaabb
aaabD
aaabb
28D
aabb
abb
abb
bb
bb

PLY
rer
&
82

Chapter Fourteen

6.1) Consider the machine in sections.


1. The machine accepts A - 29s where length(s) -0.
2. The first letter of any other string must be 2 That sis
stored. As long as the machine continues to read #s they
continue to be stored. No word consisting only of #s can be
accepted.
3. At the first 4 control passes to the second loop. For each
letter read (including that 45) the STACK is popped once. If
there are fewer #s in the STACK than letters from the first Sto
the end of the word the string crashes. When there are no
more letters on the TAPE (read A) the string crashes if there
are #3 left on the STACK. Only those words are accepted that
have the same number of letters from the first 4 to the end of
the word as initial #s.
li) Sa a5b| aSa| ab
Gi) Use the strong version of the Pumping Lemma. Assume a
machine that has # states, and take the counterexample of
(#226222-1} There must be a circuit within the first clump
of #s, and this can displace the 4 that initiates the second half.
i
oe Tape Stack State Tape Stack
Start aababb A Start abbbaaab A
Read (a) ababb Read (a) bbbaaab
Push a a Push a a
Read (a) babb Read (b) bbaaab
Push a aa Push b ba
Read (b) abb Read (b) baaab
Push b baa Push b bba
Read (a) bb Read (b) aaab
Pop (b) aa Push b bbba
Read (b) b Read (a) aab
Pop (a) a Pop (b) bba
Read (b) A Read (a) ab
Pop (a) A Pop (b) ba
Read (A) Read (a) b
Pop (A) Pop (b) :
Accept Read (b) A
Pop (a) A
Read (A)
Pop (A)
Accept
83

Chapter Fourteen

(ii) Focus on the four loops dividing the machine into four steps. The first loop reads n a’s
pushing then on the stack, the next pushes m b’s on the stack. The third loop
matches the second clump of a’s with the b’s on the stack to ensure that there are
ma’s. Finally the last loop matched the last clump of b’s with the a’s on the stack
hoping to find exactly n of them.

8. (i) S > aSb |aXb


X > bXa| ba

(i) The strings aba are all in different Myhil-Nerode classes since b” tums only one of
them into a word.
9. (i) STATE STACK TAPE
START A ab
READ, A b
PUSH 7 > b
READ, I A
POP, A A
READ» A A
ACCEPT
(ii) STATE ST ACK TAPE
START A bbba
READ, A bba
PUSH x I bba
READ, x ba
PUSH 2x IX ba
READ, XI a

POP, d€ a
READ, X A
POP, é A
READ, A A
POP, A A
ACCEPT
10. The algorithm for accepting any string of length 22(2 0) is to
read a letters while looping at READ,and then cross to POP,
on the 2+1t) letter and continue the rest of the word from
84

Chapter Fourteen

there. The loop at READ, puts some number of y's into the
STACK. After the transition is made to POP, nothing more can
be pushed. Starting with the transition letter, for every letter
we read we must be able to pop a counter. When the TAPE
reads A, the STACK must also read A. In other words, the
number of letters read in the first loop (counters in the
STACK) must equal the number of letters read in the second
loop, so the total number of letters in the string must be a
double, that is, an even number.
No word of odd length can be accepted because it is impossible
to break it into two equal parts. If |2/2] is the transition
letter, there will be one fewer counter in the STACK than
remaining letters on the TAPE and the string will crash
popping A at POP). If [n/2] is the transition letter, the TAPE
will empty first, and the string will crash popping y at POP».
S+ SS | aa| 2h| ba| 66

or
@,b
12. (i) STATE ST ACK TAPE
START A aa
READ, A a

PUSH x I a
READ, Ps A
POP, A A
READ, A A
POP, A A
ACCEPT
(ii) STATE ST ACK TAPE
START A babaaa
READ, A abaaa
PUSH x § a0aaa
READ, Ti baaa
PUSH x XX baaa
85

Chapter Fourteen
READ, xX aaa
PRS YS ¢ OG ¢ aaa
READ, III aa
POP, Tr aa
READ? jee a
POP, DG a
READ, § A
POP, A A
READ, A A

ACCEPT
“eee¥*We have to start the second part of the process with the first of
the trailing #‘s; that is, when the TAPE is reduced to eractly as
many #'s as there are y ‘s in the STACK: reading the first of
those # 's should initiate phase 2.
(iii) For a word to be accepted by this machine it must contain at
least one a that is not followed by any 4's. The string
babaaab must crash either by reading 4 in READ,, by popping
4 in POP, of by reading 4 in RBAD,.
(iv) For a word to be accepted by this machine it must have even
length. (The structure of the machine is exactly like that of the
machine for Problem 9.) If the last letter counted is the second
6 in the string, the string will crash reading A in POP,. If the
last letter counted is the a following that 4, the string will
crash popping A in POP».
1S If length(s) = 2, then every string in TRAILINGCOUNT has
length 2.7, in other words, all strings are of even length.
Further, while the first n letters are arbitrary, the last letters
must all be #s. We have established (by the pairing of _r's
popped to letters read) that this machine accepts onty words of
even length. The transition from the READ - PUSH loop is 2
Thereafter, reading any other letter will cause the string to
crash. Therefore, this machine accepts exactly those strings of
even length of which the second half is all as.
86

Chapter Fourteen

14.

1S. UG) a ab, CC COC COCE COCO ECECCORD CO ACD: Cad aad
abhcc ache cabc acch cach, ccab, aabbhe aabcb, aachb,
acaDb, caabh, abhece achcc cabcco accch, cacch, ccacb,
cccabh, aaabbb, aabbhcco aabche aachhc acabbc caahbhc
aabcch, aacaca, acabcbh, caabcbh, aacchb, acachb, caacbhb,
accabb, cacabb, ccaabb, abccce acbhccc cabcce acchcc
cachcc ccabcc accche cacche ccachc cccabc accccb,
cacccb, ccach, cccach, ccccab
(ii) Use a counterexample of the form (#54), which is a subset of
this language,
(iii)

(iv) Sa FathY | ¥
X> aXbh| X¥ | YX
auch |Z
87

Chapter Fourteen

17. Using the transition function &( g, x) to represent the destination


reached from gon input a, where q is the start state, the PDA
starts by pushing @ onto the STACK. Then control passes to a
central READ state which has one branch out for every letter in
the alphabet and one for A. On each of the paths the next state is
a POP. On the alphabetic paths, for whatever state gis popped,
the machine pushes the state 5{ g, x) onto the STACK and loops
back into the central READ. On the A path, if the state popped is
a final state there is a path to ACCEPT, otherwise not.
18. Use a machine similar to the one of Problem 17, except that the
letters must be read in pairs. (An alphabet of ar letters has a 2
possible pairs to account for.) The state &( g, x) pushed onto the
STACK is a state two FA transitions away from the state that was
popped.
19. (i) The PDA for |Z |reads an odd-numbered letter and ignores it,
then reads an even-numbered letter and processes it just as the
machine for Z would have done.
(ii) Yes. The FA for Z can be converted into a TG for |Z |be
relabeling the transitions with pairs of letters. A transition on #
becomes a transition on a#or52 etc.
20. (i) sexaabbbbh, 2anbabbb, aabaabbb, abaaabbhb, asabbabb,
aabababb, sbaababb, ababaabh, aaabbbab, ashababb,
AbAAbDAbh, Rab DARD, AHabhaahdh, aaabhbhbhab, aababbab,
ahaabhbab, atbbhabab, abababab
88

Chapter Fourteen

(ii)

(iii) Use a counterexample of the form (4#*5#) and the strong form
of the Pumping Lemma.
(iv) Sa SS| 4Sb| 2b
(v) They are matched pairs (equal numbers of #s and 4's), where
the nesting requires that each close parenthesis (4) be prepared
by an open parenthesis ( #) that has preceded it.
89

Chapter 15

This theorem has two halves. | always cover the first and skip the second. This is jpso
facto inexcusable but | plead the necessity of time constraints. | do think the proof in the
text is understandable and self-contained and can be assigned to students to read as long
as they are promised it will not appear on the final exam. | have not made the usual big
deal that the STACK must always return to a previous length condition and that that is why
the Net nonterminals will always be sufficient. This is partially because it is as obvious as
many other points that are slurred over expressly and partly because our approach seems
to include this as a necessary consequence -- also the STACK-content graph-pictures
appearing in other texts always seem to imply that the STACK contains a continuous rather
than discrete number of elements, which negates the mathematical precision of the whole
exercise.
90

Chapter Fifteen

leet seal

(ii)

PUSH&
oT

Chapter Fifteen

START PUSH S

REAL)
2 as

\ f ase 6 x7 :
Sy a i PUSH 4
7 AC gerry ao [PUSHA} >
eee VAG :
“ SS > we :

AT :
Sy
.
ape
( ACCEPT
6X
Ue
|es Pages
{PUSHS| [PUSHB

START
FUSHS

(RE HA

SCCERT = READ

START }] PUSH S

eS
SR pAp ea

:
ihe, x i
ne aera Ec
eee a
w/a:
READ >——(
4
ACCEPT
fe
=
ee
2d
92

Chapter Fifteen

: {PUSHY} [PUSH 5)
READ A A c —
» PUSH AJA
Nor eS
ao

4 ~~ y 5°ae Hey ia

ACCEPT RE AD wv
~—1 PUSH 4L-f PUSH 4

(ii) None
To (h)

Cid) Sz ((a)(a))((a)(a)), (a)((a)(a))(a)), (a) (Cada) (a))). (Ca)(Ca)(ad))


(a). (((a)(a))(a))
(a)
8. (i)

[BOSH] {PUSH
1a PUSH = PUSH
ea
§4 x

\
aa)
PUSH 3” PUSH 4
98

Chapter Fifteen

(ii)
.

Vai YON
oe Wolo Pane bans >
deemed
ie Siew
eee IVoYV boi rOvy pai S66
5535 IH IS BID 5565 L636 B66
bhb695 2bI5h bIbSd Sbb55 bbh95 bd55h
5058395 365398 SSOBHO 56368
bb 968 IDHISH bbbbdH6 666356
5bhID5
bh56356

9. The same process can be used to create PDA's for any CFG's.
Everything is stacked; nonterminals, when popped, follow
procedures as given, terminals are read.

10. (i) Use the strong form of the Pumping Lemma. Consider a
machine with 7 states and the counterexample of ((27 a )27).
(ii) Sa SSI(S)IS+ SIS* lal bla
(iii) CNF: Sa ZA| SP| SAlalblA
Ra SC
L- ( C+) P-3+ ABs
C START)
FLUSH ©

READ 3
bee apy. t PUGH 2
ahyGnd ‘POP,
7‘ae

RE AD -#—————— Tas p es A PUSH ¢ LF PUSHS|


7 ; oe ‘ . / Pe ye ,
p = . 4

ReaD
%. ‘ ae 7 Pa oe
ie { P -
: Sb ex

7 ~ ve a . a ie hy =e :- Se A a

CREAD
», vy
4——" CREAD>
.. a
RE~ AD; rd> ACCEPT
——$$__—~
Lf ‘. /
%.a x ns /
94

Chapter Fifteen

(iv)

11. (i) In conversion form every READ must be followed


immediately by a POP, and since no branching can occur at POP
States the same POP cannot be used to follow two READ’s.
Therefore it is impossible to have a PDA in conversion form
with twice as many READ’s as POP’s.
(ii)

\/
12. (i), (ii) Yes to both. Note that the question refers to rows in the
table, not to paths through the machine. Rows just show
alternative paths available between consecutive joint states,
without restrictions of consistency.
(iii) No. Now we are talking about paths to ACCEPT. To get
there we must POP everything, including the $, from the STACK.
We cannot have pushed more letters than we POP.
13 Gi) -a(arb)* the language of all words beginning with 2
(ii-iii) FROM 10 READ POP PUSH ROW
START READ $ $
READ, HERE F $ ease 42
95

Chapter Fifteen

HERE HERE a 3
HERE READ, b 4
READ, READ, a Saeers 5
READ, READ, b $ $ 6
READ} ACCEPT A $ 7
14. (i) WX F and Z represent the sets of possible joints. Not all
combinations are useful in the production of words.
PROD; S-> NET(START, ACCEPT, $)
PROD? NET(START,Y, $) > Row,NET(READ;,4 $)
PROD; Net(READ,,.4°$ Rowgner(READ,, Y/a)ner( MZ abeer(ZW, Dhol W, X $)
PROD, NET(HERE, HERE, 4) + Row;
PRODs NET (HERE, READ2,) + Row,
PROD, NET(READ2,¥,$)+ RowsNET(READ2,¥ $)
PROD? NET(READ>,4$)+ RowgNET(READ2, X$)
PROD NET(READ2,ACCEPT,$) + Row?
All derivations begin
PROD, .5-» NET(START, ACCEPT, $) + PROD2 NET(START,ACCEPT, $)
+ Row ;NET(READ; ACCEPT, $)
+> PRODs, 4. 5 Row ; Row 2Row3Row3zRow, NET(READ2,ACCEPT, $)
There is no choice so far. This gives us
+ PROD, Row,Row,Row3Row3Row, J Row7
where
X Rows 1 |Row, 41 A.
The CFG for the row-language is
5S Row, Row 2RowzKow3;Row, JY Row;
X= Rows 1 | Row, 41 A.
(ii) To covert it to the language over = = (a 45) add the productions
Row; > A RoW5 > @
Row, > @ Row, > 5b
Row; > A Row, > A
Row, 7 A
96

Chapter Fifteen

5. (i) S 7 ARIAB
Rk 7 SB
A 24
B34)
(ii)
LUSHS|

ie, FROM TQ READ POP PUSH ROW


START HERE $ S$
HERE HERE 5 AR 2
HERE HERE S AB 3
HERE HERE R SB 4
HERE READ, A 5
HERE READ» B 6
HERE READ; $ $ 7
READ, HERE a B B 8
READ, HERE a R R 9
READ, HERE b B B 10
READ, HERE b $ $ 11
READ, ACCEPT A $ 12
17. (i) The easy productions are
PROD, S— NET(START, ACCEPT, $)
PROD, =‘NET(HERE,READ;,4) > ROWs
PROD, = NET(HERE,READ,0)+ Row,
PROD, NET(READ3,ACCEPT,$) > Row,
To simplify the other productions, we note that in ner(Y ¥ Z)
97

Chapter Fifteen

1. If ¥ is READ; then Ymust be accept and Z must be $, since


READ; goes nowhere else.
2. ¥ cannot be start.
3. 4 cannot be Accept.
PROD NET(START, 4 $) - Row,NET(HERE, ¥ S)net(
)NET( 4 X$)
PRODg NET(HERE, 1S) - Row NeET(HERE, ¥ A)net( XX #)
NET(
PROD? NET(HERE, 4S) - Row3NET(HERE, F A)Net( ¥%X 2)
NET(
PRODg NET(HERE, 4 &)
NET( - ROW4NET(HERE, K S)NET(
N KX B)
PRODg NET(HERE, 4 $) - Row NET(READ3, 4 $)
PROD; 9 NET(
NET(READ,, 4 &) + RowgNET(HERE, 4 2 )
PROD, NET(
NET(READ), 4 #) + ROWoNET(HERE, Y #)
PROD;2 NET(READ2, 4 2) + RowygNET(HERE, ¥ 2)
PROD3 NET(READ2, 4 $) — Row, ;NET(HERE, Y $).
This gives us the following:
PROD, 5 ver(stant,accert,$
}+PROD 5ROW yret(Here READ), S ret(READ, accert, $)
+ PROD,3 Row NET(HERE,READ»,.5 JRoW, ;NET(HERE, ACCEPT, $)
+ PRODg RoW,NET(HERE,READ2,.5 JROW, s ROW NET(READ3,ACCEPT, $)
> PROD, Row ;NET(HERE,READ3,.5 JRow, {Row Row 2
So all the words in the row-language are of this form.
Examining the key portion NET(HERE,READ2,.5 ) we find that it
must use either PRODg or PROD7. If it uses PROD, Ymust be
READ, and Y must be HERE. since the stack will have A and
then Z and only the edge to READ, pops the 4 and from there
only Rows pops the & This sequence consumes the stack
down to whatever is under the 5
HERE, J)
NET(HERE,READ>,.5 )> PROD7 ROW NET(HERE,READy,A)NET(READ)
> PROD? Row zROW5NET(READy
HERE, 2)
> PROD, Row3RowsRowsNET(HERE,HERE, 27).
If we use PROD, from here instead we have this sequence:
NET(HERE,READ>,.5 )+ PROD« Row NET(HERE,READ,A)NET(READ
;HERE, 4)
98

Chapter Fifteen

PROD, Row ,RowWsNET(READ HERE,#’)


PROD), Row ,RoWsROWgNET(HERE,HERE, # ).
PROD, ROW ,ROWsROWgROW 4NET(HERE,READ2, 5 )NET(READ 2,HERE, 2 )
4 PROD,
db Row RowsRowgROw 4NET(HERE,READ , 5 Row Row jg
We replace NET(HERE,READ2,.5) by the simpler symbol Af The
CFG becomes
S + Row, M Row,Row, ,Row7Row,2
M + Row,RowsRowgRow, Af Row¢Row,9 |RowzRowsRows.
(ii) The row-language to input-language conversion is
Row, > A Row, 7 A RoWg > @
Row, 27 A Row, 7 A Row;97 3
Row3 7 A Row7 > A Row;; 2 2
Row, 2 A RoWs > @ RoW,;27 A
The resulting CFG is S 2 Mb
M->aMbh\a
which is clearly a grammar for the language (227).
_ Consider the “Grand Central POP” machine of Theorem 28. This
machine uses exactly one READ state for each “dead” production
(Nonter minal > terminal). Each of these READ states has only one
transition, on the terminal required at that point in the deriva-
tion, and returning to the central POP state. Because the
transitions out are the same for each RBAD that looks for a given
terminal, we can combine them, so that every production of the
form N> a (a«Z ) is represented by a transition from POP to a
single READ state, and that state has a transition back to POP on
a. For= ={ 2 6), that is two READ’s: the third is for A.
19: The algorithm presented to construct a PDA from a grammar in CNF requires only on
POP state. So any CFL can be implemented by a machine with one POP.

20. In these PDA’s each path from the POP goes to either a sequence of two PUSH. ’s or a
READ state before loopmg back to POP. A deterministic PDA has one such loop per
stack letter, m this case, nontermmal So starting with the single S edge emanating form
the POP, at most one path will lead to accept, recognizmg only one word. The S edge
99

Chapter Fifteen

either goes directly to a READ in which case the word is only one letter, or replaces S
with two nonterminals (which may be the same). For each of these nonterminals there is
only one path to follow from POP which have the same possibilities as S just described.
100

Chapter 16

This chapter is more important for its convincing discussion of why certain languages are
inherently not context free than it is for the Pumping Lemma itself. The parallel between
self-embeddedness and FA-loops / Kleene closure, is a profound insight and the
beginning of mathematical sophistication about this whole subject. Perhaps we give too
many pictures illustrating the same point, but the point itself is reiteration and so it is better
to iterate too many time than too few. It is actually not all that important in practice to
develop facility with demonstrating that languages are not CFLs but it is an easy advanced
topic to formulate test questions about and so students should attend carefully to it to
become prepared for Master’s and PhD qualifying exams.

There are two sloppinesses in this chapter. One is whether it is necessary for a self-
embedded nonterminal to reproduce using the very same production X-NN or whether it
is sufficient that the second occurrence use a different production X-MM. The other
carelessness is counting the rows of the derivation tree -- is the S symbol the zero-th row,
is the bottom row of all terminals counted in the tree? Some times p+7 should be just p or
p+2. The point is clear enough and the issue is that there is some predictable finiteness
about how far one must go to use the Pumping Lemma -- O(p), as complexity analysis
would say. Long enough and just so long. If you wish to be more precise about this go
to it.

Remember to emphasize that, like the Pumping Lemma for regular languages, this one is
a necessary but not sufficient condition for CFLs; and not all non-CFLs can be shown to
be so using the Pumping Lemma, even with length.
Chapter Sixteen

1 Only SPA
2.(i)To make az Apply 5+ AA and then A a twice.
To make 54 Apply S> 88 and then 2 & twice.
To make any longer word in NONNULLEVENPALINDROME:
If the length of the word is 2, then for m1 times,
If the nert letter is 2 apply the productions S54 AY and
A SA; if the next letter is 6 apply the productions 5S» AY
and /> S& Then, if the 7" letter is 2 use S> AA; if 5 use
S + BB Then apply the dead productions A> 4 and B> 6.
This shows we can make every word in the language. We can
make nothing else.
The only choices from § are S+ AA and S+ 8&2 which lead
only to dead productions, and $a AY and S+ AY Since the
only choice from Y is 43 SA, the choice of S~+ AY gives the
structure 5% ASA. Identically, S» AY must give the
symmetrical derivation 5% #5 So the grammar produces
only palindromes of even length, and does not produce A.
(ii)
Sy Ss
‘\ ‘N
AA a8
te Lal
gad BaD

(iii) Putting the grammar into the binary form of CNF did not
change the result. The real difference comes from introducing
the productions 53 AA and S57 #84 which do not reintroduce
the nonterminal S It is still true that only the shortest words
in the language can be derived without using a self-embedded
nonter minal.
3. The theorem does not give the “wrong” number, it just gives an
upper bound that is well above the actual result in this case, 4.
If a word of length 4 guarantees self-embedding, a word of 64
must also.
102

Chapter Sixteen

4.(i1) Sx ABA|AD| BAILS


Aw Aala
(ii) Sa AR| AB| BAILS
Aw AAla
Ra BA

te So) Wy
Pe
ty.

Spal
oo
sO
Atlin
ih

6. Ifagrammar in CNF has generated a word of length 2 @ +1


there must be a +1 levels of live productions in its derivation
tree. If the grammar has only @ live nonterminals some of
these will have to have been repeated by the time the a +1
level is reached. However, repetition does not necessarily
guarantee self-embedding. To establish that there is
necessarily self-embedding: If there are a different live non-
terminals, than at the first level of the tree there are (a+1)2
possible productions that avoid self embedding. S
Similarly, at the next level there are ( 2+2)2 possible Jule
non-self-embedding productions. Clearly, at the nt S ots
m‘h jevel there are ( a> a7)? possible non-self-embedding
productions; in other words, we necessarily incur a self-
103

Chapter Sixteen

embedded nonter minal if we extend the tree to this level, as


Wwe must to generate a word of length 22?!.
The live nonterminals are 5 Y and %so m= 3 and2@*! = 16.
Therefore any word of length 16 or longer must have self-
embedding. This is obviously true (see 2(ii)) and provides a
closer bound than Theorem 34 provides.
The live nonterminals are 5 Y and Kso m= 3 and 2#?*! = 16.
Note that any tree with an Yor a F in it will have a self-
embedded S.
Prove both by the weak form of the Pumping Lemma. If v
and ycontain some #s and some 4's, vi x;2 zwill contain too
many occurrences of key substrings. If they consist of solid
blocks of #s or 4's then one or two clumps will be expanded
Without changing the others, giving a result with nonmatching
exponents.
1|. (i) 8 words of length 105:
gi05 353535 gl 62l gl gl G2l [915 f15]3 gid
[aoa [DH A0D 17/2 A] 8 [26522
(ii) Use the strong form of the Pumping Lemma.
ie {#2622 24) is not context free. Use the weak form of the
Pumping Lemma and show unequal clumps and/or forbidden
substrings.
ee {#759c® )is content free:
S2> XY
X7 aXbh\| ab
YsacKle
Pe {#262@ } is not context free, by the weak form of the
Pumping Lemma.
Since PALINDROME is context-free the Pumping Lemma must
apply to it. Take vy and y symmetrically around the central
letter (or two letters).
104

Chapter Sixteen

Use the strong form of the Pumping Lemma and a long word of
the form #°6"c° where > 22.
Use the weak form of the Pumping Lemma. If v and yp
contain parts of s only, yyvvryyz changes the s part but not
the reverse(s) part, so they no longer correspond, and
similarly if they contain substrings from the middle (reversed)
section only. If they contain substrings from both parts, since
UPDOWNUP does not have the bilateral symmetry of
PALINDROME either the inflated left s will not match the
inlated right s or the inflated. s will not correspond to the
central reversed section.
is. 29 20. The Pumping Lemma for CFL over a one letter alphabet is the same as the
Pumping Lemma for regular languages over one letter.
W = uvxyz = a?ata‘ava'
uv'xy"z = aPa™a‘a™a’ = aPirt gnats)
While w=xyz= a‘ata"
xy"z = afa™éa" = al an®
Both are of the form a@™™ anconstan)_
If a language fails the Pumping Lemma for regular languages then it fails the
Pumping Lemma for CFL
105

Chapter 17

One of the high points of this chapter is definitely that the machine proof for the closure
of CFLs under product is bogus. This is yet another mathematical caveat. More is learned
from wrong answers than from right answers, especially if they are someone else’s. The
cautionary tales of these “almost” proofs are as important in learning wnat PROOF is and
what PROOF isn’t as are numerous examples of correct proofs alone.

In the proof involving the intersection of a PDA and an FA we state that the PUSH states
need no FA labels because no branching occurs at such states. Profs. A. Barnard, L.
Leenen and T.A. Meyer have pointed out to me (and their students) that a better
explanation is that FAs only change state when reading input. | feel that this is what |
meant, but they state it better.
106

Chapter Seventeen
(i) S > aX
X > aX|bx|A
(it) S > aB|bA
A-aS|bAA|a
B > bS |aBB|b
(iit) S + SS|BS|USU|A
B = aa| bb
U = ab|ba
(iv) Srey
X > aXb | ab
Y > aYb | ab
(i) S + XY
X > aXb | ab
Y > bYa|ba
(ii) Do NY
X > aXbb | abb
Y > bbYa| bba
(iii) S> ky
X > aXbb | abb
Y > bYa|ba
(iv) S > eZ
X > aXb|ab
Y <2abY2.|/ba
Z > aZb |ab
We get all words of the form a"b"a™b"

3. (i) S3> ay | S>

5 nd 25, 5 | ab

S,+ bS,a\ ba
(ii) No. Every word in the closure of this language must be
factored into substrings that are words in EQUAL. The string
4ababhb is in EQUAL but not in the closure of this language.
(iti)
5} > a5; b| ab

57 bS,a\ ba
107

Chapter Seventeen

(iv) See (ii)


(v)L 25 ba aabb ababh abba baah baba bbhaa
888Dhb aabbab aabbba abashb ababab ababba
4\haab abbaba abbbaa baaabhb baabhah baabba
babaah bababa babbaa bbaasab bbhasabha bbbhaaa
aaaazbDDbhb 4aabbbhab Saab bhbhDa fabbaabbhb 48bhbabab
aab0babba aabbbaab aabbbaba a2bbbbaa abhaaab hb
shaabbab 4bhaabbhbha shabaabhb Shababab 4habhabhba
ahabbhaab ababbaba &babbhbhaa ahbhaaabb 4bbhaabab
abbhaabhbha ahbhabhaab abbhabhabha ahhabbaa Bhbhbhaaab
ahbhbbhaaha 2hhhbhaasa baasaabhbhh bhaszabbab baaabbba
basbhaashh bashabhabh bashabhbha bashbhaah baashbhabha
basbbhbhaa babaaabh babaabab babaabba bababhaabd
babababa bLababbaa babbhaaad babbaaba babbbhaaa
bbhaaaalhb bbaazbhab bbaaabhba bbaabaab bbhaababa
bbhashbhaa bbhbaasab bbbaaabha bbbhbasaa
All finite languages are regular. (This of course includes the
one-word languages {A ) and, over = - (2 4}, (4) and (4), out
of which we can construct all regular expressions.) There is a
CFG for any one-letter language: Just write each letter in 2 that
is a word as the right side of a production from 5
Now we can build up grammars as we do regular expressions:
The union, product and Kleene closure of any two regular
languages are regular, and, since we have grammars to start
with and theorems to account for union, product and closure,
by grammars, we can construct a grammar for any language
that can be represented by a regular expression.
5. (i) Sa+ ak
Xa bBbXYA
(ii) S52 SS| axXlA
Xa BbbXIA
(iti) Sa Xa
Ys bOXA
108

Chapter Seventeen

(iv) S+ SSIA
5; > Xa
X~> bOXLA
(VY) $+ 5.5515,
517 56
Q> ala
6. (See also Chapter 4, Problem 20.)
(i) Any language is regular that can be defined by a regular
expression. Replace each 2 in the regular expression for Z by
s,and each 5 by s, Since the strings can be represented by
regular expressions, this can be done. The resulting regular
expression defines the substitution language, which is
therefore regular.
(ii) In the CFG for the language, change each terminal 2 tothe
string s,and each 4 to the string s, Eventually, in the
derivation of any word in the original language, the terminal
must have come from some production where it occurred on
the right side. This occurrence has been changed to the
substitute string.
7c) Ley i me
X> AXb| 2ab
Av» aAla
Ys a¥B | abb
Ba 6B \b
(ii) Saud
X 7 aXb| ab
Yo b¥2 | ba
(iii) Sa XY
AX LZaaz
Z> al | bZ\A
Yo arl\6¥\a
109

Chapter Seventeen

8. (i) In general, there are languages for which the decision


procedure can be made before the entire input string has been
eXamined, as in a(a+b)*, and languages about which no decision
can be made until the whole input string has been examined,
as is the case with (atb)*a. We do not want to permit
nondeter ministic branching to the next machine unless the
language in question is one of the first type. (Note also that a
language may be the union of a language of each of these |
types.)
(ii) We know that every CFL has a PDA. We can make the product
language grammar by the algorithm of Theorem 31 and
convert this into a machine by the algorithm of Theorem 28.
(iii) We can use the method of part (ii), making the CFG for Z* and
constructing the corresponding PDA a la Theorem 28.
However, we can also permit nondeter ministic branching as in
18(iv), because any string that can reach ACCEPT for the
machine of Z is necessarily accepted by the machine for Z*.

4. (i) context-free: ODDPALINDROME's beginning with 2


(ii) context-free: either A or ¢g
(iii) context-free: (2767}
(iv) context-free
(v) context-free: (NONNULL)PALINDROME
(vi) non-context-free: (42672 #7 )
(vii) non-contert-free
pO

Shaws b ys Bare

SERDAR 2 Zee aps prip S2+READ >< POP is


RIDA
esas PoP

CRG for (22 b* 27)


Sa aSa\ aXa
Xa bX| 6
110

Chapter Seventeen

1. (i) §2 4#5b| 246


DALYGL
Yo bY \5b
Z7 al \a

(ii) Sa VY (iii) S> YY


X2 aXa\| ava X> aXbh\| ab
Vaubh¥ |b Ysa 2¥ | Yb\a\6
(iv) Z;N 2,0 Lz = (29624964 }, which is not context-free.
)2. Suppose VERYEQUAL ts context free. Then, by Theorem 40, its
intersection With a regular language must be context-free. The
intersection of VERYEQUAL with the regular language a*b*c* is
{2252 ) which we have shown is not context-free. Therefore
VERYEQUAL cannot be context-free.
Ltt

Chapter Seventeen

13. (i) Z° = (a*b*) + (2762). This is the union of two context-free


languages and is therefore context-free. If either Z or Z’ is
regular the other must be also. In that case, if we intersect
either with any regular language the result will be a regular
language. Intersecting Z° with the regular language a*b*
gives (2252 ) which is not regular.
(ii) Use the grammars S+ ASO S+ a5B
Aw» Aala Bo Bb\ b
to show that they are context-free. Since each is the transpose
of the other, if either were regular the other would have to be
(see p. 98). Their intersection is (2762 }, so they are not regular.
(iii) Their intersecton is (4262).
(iv) Their union is aa*bb*
14. (i) Sa 2$a| aXa
X> bXa\ ba
(ii) Sa XV
X> aXb| ab
Yr a¥ |\a
(iii) Their intersection is (2262#22}, which can be shown not to be
context-free by the weak form of the Pumping Lemma.
Pees > Valw lal Wa ARA
Voawlihi KIN X> RBA
Vyi2 Kal Old Yo ABT
V2 aVy|A L+ ATA
V32 A| B| AB R-> &Rb| ab
V2 ABAV, T> bla
| ba
Aw &aAla Boa bB\b
}6. Assume Las defined in (i). Again, 2- #= ZN #’. Since # is
regular, so is A’, and the intersection of a context-free
language with a regular language is context-free.
fiz

Chapter Seventeen
17. (i) The algorithm works fundamentally by de Morgan’s law (see
Chapter 10, p. 183), that is, we complement the union of the
complements. If a PDA is nondeterministic then some word that
is accepted by one path will be rejected on another path. When
the ACCEPT/REJECT status of the states is changed, such a word
Will still be accepted, by one of the paths that did not work on the
original machine. This shows that the procedure is bad, since a
word cannot be in a language and its complement both.
(ii) Yes. It may result in a nondeterministic PDA, however.
13. (i) The PDA is on page 350. The FA and the intersection machine
are below. Note that the alphabet is= = (2 45, ¥}). The FA
accounts for all transitions. The PDA accounts for strings that

a
en
SS S

ard % KP ary oa ACCEPT


é

ae
(ii) First note that any J causes the string to be rejected by taking
it to a state that includes FA state 3. These states have no
A- edges. The #s read initially are stacked. The Y causes a
transition to the next state of both machines. Thereafter, any Y
read (or popped) causes rejection. For every #read, the stack
is popped. If TAPE and STACK are both empty at the same
time, the string is accepted. As many #’s must be read after
the XY as were stacked before.
19. (4267) a(a+b)*
abe,
3 son gl

READ é>= pap, x ACCEPT


113

a
Chapter Seventeen
piaeD) 7 (a+b)*aa (as byeer ER -EVEN
@

7) b
e(2)5
BOae
-) 2
G) Tejeme2G
y oh
? é 3)
Oo
(i) (2959

\
aN
RAO Ba EA,
Be ve

PUSH
| @ Foden
a READ,
>A Por, SE
SCACCERT
4 SS per: bee

(foQD

(ii) ¢

: 7: es va \ xX
ESLER : css) ep eaten)
a
b men: 5
NC @

Ae 2 pecan “aN

PRP ONE
(iii) (2252 922)

READ)29[FUEH a| READ, Has


3
>< PoP,»
Soy
ay, €.
: b 3
114

Chapter Seventeen
(iv) (422622 n=0, 1, 2,...)
CSTART )

mo S ue 2

(ACCEPT #}=< REAL,» READ?


a oe Soy
vA sf

RUE. WAS OP x6
BA oo = 4 ve
es Ne

Q *, ‘ a NY *. | A oO

2avs os we ny 5 a cx oe 2 ra ‘ BS a s By a ) we a

CREADAC PUP eee R EADS AX PPL


are eee UCP
ss
NE
“a
a eae nete
rs 7
ee
ey ae
<C PIR, eS REALOC POPS READSS‘
i

Be ON) AYE
- AN
Sects
,
OR
No oF) ye
ee
; - a
ne | a etl oe Ses 2x SS Gl? ¥ Vag? See
Me ee Pas , Se <a Bs oat

aN Xx
rs Ss Pa .

< pop, >sCACCEPT “pop, Y


a > ve
Se \
N37
Ve Vv

90. Here is the intersection machine.

In the READ and | states reading A causes rejection. In the


READ and 2 states, where we have read an odd number of
letters, reading A takes us to ACCEPT.
115

Chapter 18

The numerous references to what is undecidable can only be verified by something like
Rice’s Theorem which cannot be given until after TMs are studied, but by PART Ill we
have about lost interest in CFLs and their petty questions. What is undecidable about TMs
becomes so much more interesting. Perhaps in another edition Rice’s Theorem and the
proofs of the undecidability of the questions in this section will be formally presented. At
the moment students must be satisfied with a feeble “we'll see soon” type of excuse.

The CYK algorithm as presented in the first edition of this text turns out to be not the CYK
algorithm at all, but my own invention. Oh my. It is a perfectly effective algorithm for
deciding whether w can be generated from G but it is not the creation of John Cocke. |
made his acquaintance in the summer of 1979 at IBM Research at Yorktown Heights, NY
and over one lunch |jumped too quickly to an interpretation of what he said. The algorithm
made perfect sense to me and | published it as his. This is a disservice to C and Y and
K since their version is better. They have my apologies, as do the students who have read
the first edition. But since the theorem was decidability the proof was not wrong, merely
the attribution. And as is stated repeatedly throughout the text, it is not how best to do it
that we are concerned with but whether it can be done at all. Still it benooved me to
correct this mistake, and we do here present the CYK in all its glory.

The PDA-transducer for parsing simple arithmetic is a valuable link to courses on compiler
construction, and for those students not taking such a course a satisfactory convincing
demonstration of how compiling can be done. It is quick reading and needn't take up much
class time. On the other hand, it is interesting to discuss what it means for a
nondeterministic machine to be a transducer. What if the same input string can produce
many different outputs, is this a problem or an opportunity?
116

Chapter Eighteen

(i) no words
(li) = yes; ab
(iii) yes; baba
(iv) no words
(v) yes; abbbb

The algorithm actually does not really use the fact that the grammar is in CNF at all.
Allowing t to be a terminal or string of terminals covers grammars in any form.

(i) X and Z are useless; finite


(ii) | Y and Z are useless; infinite
(iii) |X and Y are useless; finite
(iv) infinite
(v) A,B and C are useless; finite
(vi) A, B and C are useless; infinite
(vii) X is useless; infinite
(viii) infinite

Nothing in the algorithm requires the grammar to be in CNF.

If the only nonterminal is S then a live production means that S is self-embedded and can
generate strings with any number of occurances of S. Moreover a dead production means
that all those S can be turned into substrings of terminals. Hence the infinitely many
strings generated of any length are all words.
117

Chapter Eighteen

The algorithm processes substring ranging in length between | and n. There is no


difference when comparing the substrings of length 1 to the right side of the productions.
However for length 2, not only must all possible NN combinations be tried but also the
possibilities of the forms tt, tN and Nt. Likewise for substrings of greater length, all
possible combinations, considering terminals and appropriate nonterminals must be tested.
The advantage of CNF is great in this case because regardless of the length of the
substring, only the concatenation of two nonterminals must be considered.

12. (i) No
118

Chapter Eighteen

;
i eay es

aaaa abaa rk
aaab
baba bbba fore bbbb

aaaaa aabaa ababa


abbba baaab babab
bbabb bbbbb

(iii) No
ay;

ISH) 5% £9 T+ 9 Fs BO (+ h9 1+ 7+ Fit Fs ED (+ f+£F


=> (+ f+ Td f+ f+ FD f+ (+ 7
ii) S* £9 f+ FX 7+ FX Js F+ FD Fst Ft FR fs Fs F
> ie (+h fe (+L fs (+ Lak O fs f+ Fak fu (+ fs F
=» fx f+ fx f
GpS
> £9 79 Tx FO Js Fe FO Fe Fe FO f(s Fs F
»(*(£)* Fa jt ( F+ F)* Fo ss ( 7+ F)e F
> («( F+£)* FO fx ( f+ FE) FO fe (4+ TI LF
say i (se Fy fa fs | gor ipye Fae 7 alee ois 7p

Gu Sd £9 £+Te T+ TO Fs TRUE) +F(1)57s (Fe Ff) el


aie fe (2) Fer eA) Ae a7 ee
> (FF)79 ((/)8F +7 ((/)8(
2) 7 9 (1 )0( 7+ 8) + 7
Chapter Eighteen

DE) T= ((/)* (4+ F))+ TOs) = (4+ 7))+


—=_=(/+ F))+ 79 ((/)* (4+ F))+ 7 9 (Cs)* (4+ /))+
asiran)
PR, pn,
4
Si, Te

(W) > FeT*F9X(F)9(T+ £)>


( T)+ £)9 ((F)+ £)% (((£))
( (F))+ F) (((7)) + ££) (((7))
( (7)) + (2) 9% ((s))* (7) > (C4)
> (((7)) + (F))) > (4)) + (4)
14 RAS
BS
Ht
Obes | Niro fA Mevea tf* (al iron AUB hand * Ftron? 2/itean 7 trom / irehleon5
ii 1)+((i)) hrom((F’)+((/)) hrom(( 77)+(( 7) trom(2)+((7))
is
alta trom( F'+((7)))
trom( J +(( 1) bron 7°+((F
))btremn( 7°+(( 77) hroea( 7°+((£)) trom 77+)

T trom F trom S
(il (/* 442 hon ( F* + S)iron( F* F + Strom (F* 7+ 1Mtron( 7+ tron( 7+ &)
tron( J+ J trom( 7+ & )tron( £ trom Ftrom Ttron & trom S
UY £*( 4+ )trom FY S42 jtrom F*(F +f bron F*( 7 +7 trom F*( T+ F)
from FY T+T hon F*( 7 +8 hrm F*(F rom F* Firen F* STtron trom § from S
Pry =7) sacle 7)” Kiron (A metal Finca a eaeence a) Be
tron (J) * Strom (AF) * Stran F * StrenF *FironF *trom
J Jiro FirmS
is(i) 5% £9 &-73 F739 T/ F- 13 FIF-T3(8)/F- T
3 (F-T)/F- TA (T- 7)/ F- Ta(F- 7)/ F- T
> ((£)- TP) F-T3 ((#+T) -T)/F-T3 ((7+7)- TF -T
>((F+ 7)- 7)/ F- Ta((f+ 7)- T)/ F- T
>((/+ /)- T)/ F- T(t /)- 72 F)/ F-T
> ((7+ S)- 12 F)/IF-T (r+ 1)- Fe FF) F-T
> ((4+ /)-f2
F)/ F- 73 (re f)- ff) F-T
> ((/+ /)- ft /)/ 4-79 (4+ s)- ft f)/7-F
> ((/+ /)- 424) / f-4
(ji) 59 89 #+TAT+ Tan k+TaA f+ Tad i+ Tadi+Fads¢+ sf
gi) S9 F9 F-7T9 7-79 TF -197* AFT FFE -T
> PA F-T3 G1 F-TaA L
ss (-T? Pe t- FR ttli-s
(vy) Se £9 F3 AF HAFS HFS dis ds
Left to right: divide the quotient of the two left parts by the
120

Chapter Eighteen

one on the right.


(V) S-t-Stron f-L-Firon S-f- I trom f-F- I tron 1- I - I tron F- I - Tt T
-T-T tron B-T-T tron F- J tron FitronS
16. (i) (ii)
STATE STACK TAPE OUTPUT STATE STACK TAPE OUTPUT
START a 2"(7+2) START A 3%4+7
READ a *(7+2) READ a 4447
PRINT A *(7+2) 2 PRINT A *4+7 3
READ a (7+2) READ A 4+7
Pop a (7+2) Pop A 4+7
PusH * * (7+2) PUSH # * 4+7
READ * 7+2) READ . +7
PUSH ( ( 7+2) PRINT x +7 4
READ (? +2) READ * 7
PRINT (* +2) 7 Pop a 7
READ + 2) PRINT 4 7 s
PoP * 2) Pap a 7
PUSH ( be 2) PUSH + ’ 7
PUSH + +(% 2) READ + a
READ +(* ) PRINT + a 7
PRINT +(® ) 2 READ + a
READ +(8 r" Pop a a
Pop (* a PRINT a .
PRINT (* a + Pop ‘ a
PoP * a ACCEPT 3487.
READ . 4 START rN 34*7+
Pap A a READ a 4*7+
PRINT a 4 4 PUSH 3 3 4*7+
ACCEPT 272s" READ 3 7s
START a alee PUSH 4 43 a
READ a dase READ 43 7+
PUSH 2 2 72+" MPY 12 7+
READ 2 2+8 READ 12 +
PUSH 7 nee 2+* PUSH 7 Pages +
READ Toa +8 READ y's '?: a
PUSH 2 Dalia +* ADD 19 a
READ ava - READ 19 a
ADD 92 x PRINT 19
READ 92 a
MPY 18 a
READ 18 A
PRINT a a 18
121

Chapter Eighteen

(iv)
STACK TAPE OUTPUT STATE STACK TAPE OUTPUT
A (3+5)+7*3 START is (3*4+5}*(2+ 34)
A 3+5)+ 783 READ A 3*4+5)*(2+3*4)
( (3+5)+783 PUSH ( ( 3*4+5)*(2+3"4)
( +5)+ 783 READ ( ™4+5)(2+3*4)
( +5)+793 3 PRINT ( *4+5)*(2+3%4) 3
( 5)+733 READ ( 4+5)}*(2+3*4)
A 5)+7*3 POP b 4+5)9(2+3"4)
( 5)+743 PUSH( — ( 4+5}*(2+3*4)
+( b)e7s3 PUSH® = * 4+5}*(2+3*4)
+( )+7*3 READ * +5)}7(2+3*4)
+( )+7*3 5 PRINT *( +5)*(2+3*4)
+( +783 READ <i 5}#(2+3*4)
( +783 POP ( 5)?(2+3*4)
( +783 + PRINT ( 5 }*(2+3*4)
A +783 POP A 5}*(2+3"4)
a 783 - PUSH( ( 5}#(2+3*4)
a 73 PUSH + +( 5}*(2+3*4)
+ 73 READ +( (2+3*4)
A 23 PRINT +( }*(2+3*4) A
+ 33 7 “READ +( *(2+3%4)
+ 3 POP ( *(2+3*4)
4 3 PRINT ( *(2+3%4)
’ 3 POP a *(2+3%4)
ty 3 READ i (2+3*4)
By A POP A (2+3*4)
a, ; 3 PUSH® (2+3*4)
oa A READ * 2+3*4)
+ a PUSH( ss (® 2+3%4)
+ a s READ (8 +3*4)
A s PRINT (# +3%4)
A A + READ (* 3*4)
: " POP z 384)
35+73%+ PUSH( ( 3*4)
; 3547384 PUSHi+? 2 (© 9_33*4)
fe 5+73%+ READ +(® —-*4)
3 5+73%+ PRINT +(f 84)
3 +73%4 READ +(® = 4)
53 +7384 POP (* 4)
53 733% PUSH+ +(* 4)
8 7384 PUSH * *4(* 4)
8 38, READ #(® )
122

Chapter Eighteen

3*+ PRINT a, (® ) 4

By READ a,(3 rn

By POP +(® a
+ PRINT +(® ri “
+ POP (= a
A PRINT ( 4 +
4 POP . a
A READ z a

A 29 POP A A
PRINT 4 A .
ACCEPT 3485523480
START 34*5+234%+*
4*5+234%+* PUSH 3 cn deWf bot
4*5+234%+* READ Sa
*5+ 2348+" PUSH 4 4:42) D7 oe
¥5 42348" READ 432 17 +
5+234*+* MPY P2032. Eee
5+234%+* READ {2-2 17 =
+234%+* ADD 1417 *
m2 3ate* READ IC ie dae
234*+* MPY 238 a
234*+* READ 238 a
Z4%>* PRINT 238
aay 34*+%

Zel} 4*+*

17. The machine on p. §22 can evaluate prefix with one slight
alteration and one major intuitive leap: The alteration is the
exchange of open and close parentheses in the processing: the
intuition is that the infix string must be fed in backwards. The
output of this transducer is the transpose of the prefix equivalent
of the reverse of the original string.
. Again, the trick is to feed in the reverse of the prefix string. If
this is done, we process just as we did postfix. It should be noted
that the reverse of a prefix expression is not its postfix
equivalent.
19. While an algorithm is not necessarily restricted to a single sTACK,
it is worth demonstrating that only one is needed to do this.
123

Chapter Eighteen

> PRINT”
(START ) ae
AX POP ACCEPT )
Loe es
7 N Af ve
SH sigri CREAD =, hein ee ows ah
eee Sy PRINT ACRE AD PRINT,
er :
mee DNS.
SOF eA a aN

ele
Ey

20. (i) The evaluator has no ACCEPT state. It evaluates 345+ as 9


leaving 3 on the STACK.
The translator will accept the string +*+ and give output *++.
(ii)

FLISH

— ty FRINT A
Hs AHN a

“POP
mee Nis

~ “

ees Sone Oe
7 PRINT“ PRINT POPSC
At
(ACCEPT)
124

Chapter 19

Turing machine definitions and their associated pictorial representations are fairly
standard with the exception that we have chosen the unauthentic version of the one-way
infinite TAPE. In general | would have said that this is a bad choice and the two-way TAPE
has obvious advantages: (1) we do not have the artificial threat of inadvertently crashing
by a left TAPE HEAD move, (2) the association to Post machines, 2PDAs and multi-track
TAPE TMs is easier to make, and (3) it is Turing’s own choice. The reasons for not using
this scheme are: (1) in order to hook onto the students previous intuition developed on the
PDA, whose TAPE is one-way infinite because it is a manifestation of the FA invisible
TAPE, (2) it does tend to anchor the input as opposed to letting it float in indeterminate
space where we are constantly in fear of losing its location, (3) it helps us begin to draw
the inference that computers are examples of limited TMs, and (4) it justifies numbering
the cells from (i). 4>3 QED.

Yes, there is some unjustified asymmetry between illustrating INSERT over a two letter
alphabet while we illustrate DELETE over a three letter alphabet. The purpose is clearly
that the earlier example is drawn in a simpler fashion and INSERT is more natural to be
the first discussed, especially since we have the perennial “rewind the TAPE HEAD to cell
’ problem. The instructor should probably do some blackboard version of the alphabet
of n letters if he feels the need for pure mathematical full generality. But | have a feling
that any student who has the mental abstraction capability to understand the design
for the alphabet ) = {x,, X, ...x, }can imagine it for herself from what is presented in
the text.

There is no such thing as giving too many examples of TMs. Being able to work within the
constraints of the TM instruction set is the essence of understanding how a computer
works. Unlike FAs or PDAs, programming TMs is real programming.
125

Chapter Nineteen

1. (i) START 42493 #229 4 #2994 4#am5 5 taz36 #7 Bet


+14 gf 43 88% 4 HALT i. an 7
(ii) START 262393 * a> 4 *ha54 46a 35 thas 6 % ft
+7 #h* +1 #642 888 ~ CRASH 2
(iii) START 5425233 * 495234 *%2aha5 44% aaba 34 *aaba
+4 #gshba> 5 * a8bha 6 * aab® 37 * sab* 37 4 gabe
3978 gals | F gah® 2 3 8 gh 3 4 FF a bF a 4 88 gpa
+ 5 ## ht 3 6 HH AH, TRH git, | wt git, 3 weet 5 HALT

(iv) START #486673 #*£B26674*%b26674%*babb +744#babb


24 *hahbbA35 *6abh76 FFhal? 97 *hahbta7 # hab
»>7 # habia 1 * habt a 2 88 ght 3 4 FF a bh a 4 FR gph
35 #8 ght4 6 RH RH 7 RH RE | ORR phe 3 RHBRE HALT
The machine changes the two outer letters to * until there are
no two outer letters left to change. Then, if the original string
had even length (and therefore no middle letter) the string
crashes by reading # in 1. If the string had odd length but the
middle letter was b it crashes reading # in 2 (or A in 2 if the
string was just 4). If the string had an a in the middle that a ts
turned to # in 1, and the # that starts the second half of the
string is read in 3 and goes to HALT.
ne (i) (ii)
126

Chapter Nineteen

(4,4,2) (2,9,8’)
(6,6,£) (*,4,2)

(6,50)
(i) af ne
CHAT
———_.,
eT ; aRT : #224)
(4, 4A). ————. (AA,4) ».2 6 2

irae TT - OSS)
IQA") iy x
27,

Chapter Nineteen

7.(i) In states 4 and 7, the last letter has been compared to the first
and changed toa A if it matches. The transitions at both states
are a loop on gor Sand a transition to 1 on A. Since the
transitions match perfectly the states can be coalesced.
(ii) By using the algorithm of deleting an initial and then deleting
a terminal 4, the machine can be constructed with 5 states
including START and HALT. (See 6(ii), above.)
8(i) leat>2e#Aar3 2Amr4edAar5S #AX7 #AX48 2 AY
910 ##4> 12 #e* > 8 tee> 13 #e*Q > HALT
(ii) l sa2232 #Aga3 2Aaagn3# Asahi 42#AsarS #Aara
6#Aat71 AgX¥> 2 2 AAX CRASH
(iii) 1 #gaaa> 2 pAgaa> 3 #8Aaaa7> 3 #Aaaa>3 4 Aaam
+4 sAaag>5 #AagX> 6 # Agak 6 #Aaako | #2Agarxy
32 #AAaxn 3 #2AAafr 4 #AAayxyr 5S #AAXNR7 #2AAXY
97 #AAXY> 8 sAAXNX@> 10 #2 ANY 10 #8 AXXO 12 #2 AY
212 #2 At > 8 AeXs 10 satX> 10 eetXs 12 eee
912 seers 8 saeee > 13 wees 13 eeeeA > HALT
(iv) 1 Saehbaabh 292% Aabaabh 293% Aabhaah +3 * Aasbaab
93% Aahsab 93% Ashbaah 33% Ashaah > 4% Aabaab
295 *%AsbazgY 26% AabaayY 26% Aahgay 26% Aabaay
a6 %*AghaaY 26% Aabaak 391 *AabaaY 92* AAbaay
33 *AAbaalY 293 * AAbaal 393 *®AADaal 9 4"% AAbaayY
45% AADAXY 26 *AADAXY 2 6 * AADAXY 2 1 B®AADAXY
+1 %AABaXYY 322% AABAXY 93 * AABAX 248% AABANXY
a5 *AABYYY 27 8 AABXXY 397 B®AABXY 397 8AABXXY
38 *4ABYXY 28 FR ABYYY 910 #8 ABLLY310 88 ABLXY
+1288 4AB*NVY
a1288 AB SHY 3912 FR AB* YY 8 BR AB*NY
>10 #8 BY 310 F884 B* VY 310 FRR AVY 312 BHR Bt PY
+12 #88 BY s12 #88 PY 4B eee BY 9 wHHHMEY
49 ##ee P,Q atte YQ aeeetee | jy aes
+] ] RADA” => 8 sea he 313 on eee 313 non eett

[3 #eHRTA 41 ZRRAH TAN 4 HALT


Chapter Nineteen

(v) 1 #26363 2 #Abab> 3 # Abab> 3 #Abab>3 2 Ababa


294 #Abab>a5 #Abar>b #Abay 26 #Abarr> | eAbarYr
3+28#ABayra3 2 AAaYra 44 ABalY>S #ABXY>7 # ABXY
+7 #ABXY— 8 x ABXB- 10 ## BXY¥> 10 #e# BYV 12 #e# Be ¥
212 ## Be ¥> 8 we Be Fo 9 weeFo 9 gene Fo ll saeee
> 11 tates 8 paeee > 13 tages 13 ###**4 ~ HALT
9. (i) The circuit 1-2-3-4-5-6-1 divides the string in half. First-half
as become A; 4's &; second-half uses ¥ and Y respectively.
State 1: Treat the next untreated letter and account for it by
pulling it into upper case. State 2: Start moving down the
TAPE. Crash here if the string is of odd length. State 3: Find the
end of the processed string. State 4: This is the last
unprocessed letter (found by bouncing off the next cell).
Process it. State 5: If there are unprocessed letters, continue
the loop. If not, check that the string repeats. State 6: Move
leftward until a processed letter is found and bounce off it to
the first unprocessed letter remaining. The second half of the
machine (states 8-HALT) compares the two halves of the
string. State 7 is a rewind state that returns the TAPE HEAD to
the beginning of the input. State 8 is the state from which the
comparison is made: an # sends us to 10 and 12,a 4 to 9 and
11. State 9: The letter in the first half was 4 Look for the
next letter in the second half. Crash if it does not match. State
10: The letter in the first half was 2 Look for the next letter
in the second half. Crash if it does not match. State 11: Get
here if the was matched. State 12: Get here if the zwas
matched. Return to process another letter. In State 8, if the
string has not crashed, the first half will be #'’s and the second
half will be all *’s. State 13: Verify that there is nothing left to
process before the first blank.
Chapter Nineteen
(ii) Words of odd length crash in State 2. Words of even length
such that the first half and the second half do not match crash
in State 9 if the first-half letter was 5 and in State 10 if it was
a. These are all the kinds of strings not in DOUBLEWORD.
10.(i) Since all out-edges are identical, these states can be combined.
(ii) If State 8 reads * the two halves of the string have been
matched. Therefore State 8 can go directly to HALT on (*,",#.
Le
14%)
BARC) Omak MEEbe eat.
(2,A,R | RO in oe x
MEPeSh ( TONG (558)

tee
ren yees) EN EL) QUbdd
{INSERT#}-9C ; Caner ae
Z nat
START
(24,8) (44.00) (6,8,L)

ba

(43 230 }-—_—_ | Ca,a,£) Con)


Habe” en (AAS)
Cae) (AA L) ee

14. The loop at 5 can be (222); (6,6,Z), (4, 4,2). We know that
there is a * to prevent a crash by moving left from cell i, so
there is no reason not to loop on A as well as on azand 2
130

Chapter Nineteen

15.(i) 1 eebbaes 2* abbaar 2*abbaar 3* abhbhea> 3° abbaa


+4'aghhbaar 5*abaaa > 5 * abhada +5 * ahaa
+6's#haaa 27 * abams 28 *ehbmA +8 *2h4>8* she
4+8'ehaol *#h15 2h 93 bas 4 b45 5 88
95 "4mM76 "go 7" Mo BAAD 8 AA>HALT
(ii) | g#bbaaa 22* abbaaa » 2*82bhbaaa » 3* abbaaa
>~3*ahbaaa 274* abbaaa > S*abaaaa +~\*abaaaa
>S‘*ahaaaa 95 * abhaaam 296 * 2haaaa 27 *abaaa
4+8*2b2a@A 398" 426828 98" abaa 98" ahaa 28" abaa
91*gbaa72** $6a8 93°*6ga74** baa75**38aa 75S 824
+5 ** 2776 "**4a4ag77 ** agi 8 ** gaa 8 ** MAD 1 **gZ
+2***A CRASH
(iii) | zabaa 2 2 *abaa> 2 *absaa > 3 *8baa + 4 *abaa
+5 *229887 5 *a#a887 5 *8a8@8aa 2 6 * 828828 2 7 * 88a
+ 8* 2a@A 38 * 227 8 * 2a71 *2732** a4 +2** ay CRASH
(iv) | g#bbaahbh>2* ahbaabbh+~2* abbaabbh>+3* abbaabb
93 * ahhaabbh 4* abbaabb- § * abaagabbh 5 * abaaghbh
+5 *abaaalhb CRASH
(v) Strings that do not start with gor that have fewer leading 2s
than 4's crash in | reading 4. Strings with fewer 4's than
trailing 4's crash in 2 reading A. Strings with too few trailing
4s crash in 3 reading A. Strings that have a 4 after the trailing
4s crash in § on 4
16.
(3,2,£ )
NEAL
C2, 2 wceed 5k J
(START
ART } INSERT#| |DELETE | 5
pens
#,£) (2,a,L)
(3,a,L)C oR) (S8£)
(5,0,£) |
DELETE JH ¢eLoeete
HC)
Ne)
(65,8)
pers /

——. (*, HRI (8, AL)


( HALT 6
131

Chapter Nineteen

17.(i)1 262652 Aeab>2 aeabh>2 Bshy>3 Behr 5 Bad r4


BlhA27 2abA> ? gab
(ii) 1 #2b2a5 2 2h aA > 3 2has—> CRASH This routine will not
work on final letters. By putting a (AA,/’) transition from state
3 to state 7, and including (A,A,Z) in the possible moves out of
7, we allow deletion of a final letter. However, the routine then
leaves the TAPE HEAD pointing to the first blank after the string,
which might be inadvisable.
(iii) 1 #5244 +» CRASH The routine does not permit deleting A.
18. (For # and 4 only)
(Coe a SS Ly
“4
~

(a,A,L vy NGL)
4

(2,062)(AAR) 16a
(A681

19. (i)
(b,b,R) (a,a,R)
aie ©) (A,A.R)
START es) AAR)
(b,b,R)

(ii)
(b,A,R) AR b AR A.aR
START
(A,b,R)
20.
182

Chapter 20

Many instructors skip this chapter but then find themselves embarrassed by the references
to it in the later sections of the text (in particular the next chapter and the discussion of r.e.
and recursive languages). | personally believe that the work of Emile Post is as brilliant
as Turing’s and that but for the Allied decoding project Colossus, Post would get an equal
share in the credit for founding computer theory. The fact that changing a STACK into a
QUEUE escalates the ability of the device orders of magnitude is an insight of the greatest
importance in this subject. Most people mistakenly believe that the Post model is limited
as a practical model of a computer since it does not provide random access to the data
field, only to the two ends of the STORE. However they then fail to realize that Turing’s
model also provides us with only two data cells of access at any instruction, the cell to the
right of the TAPE HEAD and the cell to the left. The true advantage of Turing’s description
is that in its two-dimensional form, it allows us to imagine the whole blackboard as a TAPE
to be operated on and so heuristically explains why the TM can perform all known and
knowable “algorithms.”

The fact that given READ-FRONT and ADD-BACK we can write subprograms for ADD-
FRONT and READ-BACK whereas given ADD-FRONT and READ-FRONT we are stuck,
is the purest example of the importance of data structures as a deep field of investigation.

Another selling point for the importance of this chapter is that it is the first and best
example for simulating one entire machine on another (except for Mealy = Moore, which
is essentially trivial). This is a skill that crops up in the actual practice of computer science
all the time, and the student should have the theoretical basis of how to do it: equate data
bases and instruction sets.

The whole chapter can be treated in one class and it richly deserves at least that.
Chapter Twenty

li) START- #525 > READ2-426 + READ, -25 > READ> -4 READ, - A
+ READ» - A - ACCEPT
(ii) START-ba255a READ)- 24664 > READ3- a654 4 READ)- 54a READ- ba
+ READ? - 4 READ3 - A ~ READ)- A - ACCEPT
(iii) START - 242555 READ) - 24665 + READ, - 2605 ADD a- abbba
+ READ, - £644 > READ2 - baa >READ3 - 22 > ADD 6- 226 9READ3 - 2b
+ READ, - 6>READ, - A > READ» - A > ACCEPT
(iv) START - 225555 4 READ) - 2654 9 READ, - 5566 ADD 2- bbbba
+ READ, - 554 > READ2 - 662 READ - 52 ADD 5- 525 > READ, - 25
+ ADD 5 - 266 + READ3 - 56-4 READ? - £7 READ3-A
+ [ADD 6- 5 READ3- A + ADD 6...) INFINITE LOOP
a START - 6646428 ~% READ - babaa8 > READ3 - 26248 ADD 6- s6a82b
> READ3 - 54420 + READ - saab > READ3 - 440 > READ) - 24 > READ, -
6 ADD 2- 62 READ, - 2 > READ) - A> READ, - A 7 CRASH
2 and 3. The machine works as follows: READ, is the state of having
consumed equal numbers of zs and Os. If the string becomes
null in this state we accept it. READ, is the state of just having
read the first excess z READ;of just having read the first extra
b. In these states, reading the minority letter just cancels out
the previous excess letter. However, if we read the majority
letter in one of these states we add it to the STORE. A String
that contains a single surplus # consumes all the d's and the
non-excess #s, perhaps on several passes through the input.
Finally, the extra 4 is read in READ», control transfers to READ,
where the string crashes reading A. A symmetric argument
applies for 4's. More than one letter in excess results in the
cancellation of equal numbers of both, as before, but once the
minority letter has been exhausted there is no way to pair the
excess letters. Every excess letter after the first is added to the
STORE, which is never shortened to A.
Chapter Twenty

4.

ao:
ae AG

vy
&

5.

(ACCEPT ea

6.

7A)

READ >

A
i335

Chapter Twenty

(ii)
START

8.

EA : 4
| SHE pea [Sue }<E
<FEAD) :
ae, Sy
{2 a Ya

ACCEFT ) (REJECT)- (ACCEFT}


9. (i) There might not be a way to accept the words in the LOOP set
of the PM.
(ii) The PM for EQUAL on p. 613 cannot be complementing by
- reversing ACCEPT and REJECT states, even if ACCEPT states are
drawn in for the condition of READ A in READ, and READ; of
the complement machine.
(iii)

D> REJECT }

ACCEPT)
10. By Theorem 45, every regular language can be accepted by
some Turing machine. By Theorem 47 every Turing machine
can be converted into a Post machine. Therefore all regular
languages can be accepted by some PM.
136

Chapter Twenty

11 (i)
start state

EADS. <REAU
i:
7 .

eccccccesesese® Ao
acc cesses
ecco escocsccconcosrces®

halt state

(ii) (Turing) START 27 1 a7 2.2 CRASH


(Post) START- 23 ADD *- ## > READ,-* + READ27-A + ADD A- A> ADD ®*
- A* + SUB - *A > SUB- A* > READ3 - * + ADD A- *A > SUB - A* > SUB
*A + READs-A CRASH
(iii)(Turing) START 253 1 201 #44 > 2 26> ACCEPT za
(Post) START 40 ADD * - 4° +-READ, - 0° + ADD a- 0°2 READ)- 2
+ ADD b- "a5 + SUB 4°2 READ, -"2? ADD D- "4 9READ, -2 ADD
A- ath + ADD © - a2\* > SUB- "2a > SUB- A* 2d READ3 - "40> ADD
A- "20h >SUB
- A* 25> SUB
- 4% a> READS
- Af 2 ADD 6- A* ab SUB-
BAa READ, -A’a2 > ADD 2- A*%aa READZ- * a2 ADD A- * aa
SUB - A* #2 ACCEPT
(iv) (Turing) START 445 1 #659 1 26671 abf\ > 2 2563 ACCEPT ahah
(Post) START- 254+ADD *-255* + READ,- 55% + ADD # 5542 READ>- 2
+ ADD b- ab SUB- 662 > READ3. 5°23 ADD 5- 5*26 READG - "25
+ ADD b- 4255-> SUB- 6°25 READ; -£454 ADD b- "255 READ, - 255
+ ADD A- #544 ADD *- 2544 SUB - #26449 SUB - A 255 READ3- 4 255
is]

Chapter Twenty

+ ADD A- 92544 SUB- A* 255>SUB- bA* ab>+READS -A"2b7 ADD & A*%abb
~ SUB- AA* s6-+READ ¢- A*’a67 ADD a- * aba READ - 4624-4 ADD A- shah
+ ADD * - bah" + SUB- *a5ah 4 SUB- A* aba ACCEPT ,
Note in (iii) and (iv) that the initial A* at ACCEPT shows that the
(simulated) TAPE HEAD is reading the first blank after the input.
(v) a(a+b)*b
(vi)
-———_-.. we aN ; WeoN, > ieee
USTART JX READ >=] SUB LC READ > ACCEPT)
:
See
NGA
4Neal
eitesers
123) (ii)
(START
ies] 5

13.(i)
START}
a, | we

Ceca ae LG
anLan READ) CACCEPT)
ave NY
(ii) Take a counterexample of the form (4”24”) and use thestrong form of the
Pumping Lemma for regular languages. :
(iii) It can be generated by the CFG S #Sa| a5b| 55a| bSb| 2
14. The algorithm in the chapter does not account for SUB. 1. Read and delete the
rightmost letter; 2. Move all the way left; 3. Insert A before the string: 4. Writs
the deleted letter onto the A; 5. Return the head to the reading position:
138

Chapter Twenty

(A,4,2)
START )(4,6,2) (28,84
Cy re wy

@,¢,£)
r, n=) 5
-(4,4,R) (A644) Let Xoo

“ Cany,=,1)

1):
? *« ACCEFT
en)? [AG 3
d b
READ
START SoA 4 ie
BAY ADD b
ADD é

(i) START- 42565 ADD *- #255" > READ ;- 254" ss READ>- bL°> ADD «-bh*2

4 READ)- 442 READ4- “#7 ADD 6- "26> READ4- 24 ADD £- 22"

> READ,-4° > READ2- * > READ4- A> ADD #-*" 3 READ,-A CRASH

(ii) START- 222554ADD *- 2aa55* READ - 22h5READ2-


2b4°ADD @ abl a
READ 2-45%
2 ADD & 64% 22 READ2-
64 48> READ, - “a2 ADD 6- * 886
+ READs- 225 ADD *- 225" + READ, - 24" 9 READ)- 4° 9 ADD a- M2
+ READ» - “4 READ4- 2 ADD *- 2” + READ- * > READ2- A ACCEPT
139

Chapter Twenty

(ili) START- 25252 4ADD *- 2ha5a* READ


|- babe READ2- 25a" + READG - ba?
+ ADD 2- ba*a2-READ,- 2% 2 ADD 5- a*ab-» READ, - 445 ADD a- * abe
+ READ, - #64 ADD * - aa” + READ- 62” % READ2- 2” % READG- ¢
+ ADD # £4 READ4- 2 ADD* - 2 > READ, - * > READ? - A > ACCEPT

(iv) START- 26426254 ADD *- 2baba5" + READ


;- babab! 2READ2- abel?
+> READ4- 644° ADD 2 > bab’
2 > READS - 2%2 > ADD 6- ab* ab
+ READ,- 4°26 ADD a-
4° 2b2 > READ, - 4262 ADD b- *2b2b
> READ4- #545 ADD °- #425" + READ- 526% > READ)- 24% > READ4 -
> ADD # 02 READ,- *2> ADD 6- £26 READG- 25> ADD 4 - 2h?
16. 7 READ, - 4* + READ - * » READ4- A ADD * - * + READ, - A CRASH
Saar Oe

Red S READ:— ‘READ;


i Noe

We

(o0CEPT)
140

Chapter Twenty

(TART [a0§]

(accEPT }—© Crear

TET
Se a
mea
19.(i) Delete the first letter and remember it by path choice: mark
the end of the string with *: use INSERT to write the
remembered first letter right after the *. When * is read, all
letters have been processed. The INSERT routine will have to
be a variant of the one presented that permits replacing A by
the letter to be inserted.
-¢ ae
Set Sait Ina
E wy M4 os

f2, 4,R) 0Aas feeapeccams


tle rg hea
Neat) Gell
mea ae
mel
& : A f.2,2 : “2
a a olen oh J BeFO
ee )hae das sep os
ae,

| START
yt : .

)-
- a Ee ‘ ‘ c

-
ba eee te eer ees
Se a ( x pa{ = ,
We | atat
i fA (Sere
wu

CAN 2 a fa wae,
Doct Sit i”
Mess! ‘ . *
t %
,
- aes < ~.

(2,3, 105 a ata ie ke aa


(é2s
Ss A’) ‘ Io, e, c 3 ! ral
\, } i
14]

Chapter Twenty

(ii) Mark the end of the string with # and use $ to separate the
reversed string from the unreversed. Read the first letter and
remember it by path choice. Cycle through the string reading
and adding each letter as it is encountered. When # is read it
is added back and the remembered letter is added
immediately after it. The rest of the string is ignored (i.e., read
a letter, add it), until the $ that marks the end of one trip
through the input. When the initial READ state reads # the
entire string has been reversed. Cycling through to $ puts it in
the right order and removes the two markers. _

fe
READ:ee
os ey a f [ALL 3|

20. We can construct the PAL G algorithmically. Use the reversing


machine above as a preprocessor and send its output, the
reversed string, into / as input. If the reversed string iS
accepted by / the string is in L7
142

Chapter 21

This is another chapter that should not be skipped. Again it takes only one class and it
pays off in two ways - it organizes the whole structure of the course OPDA. 1PDA, 2PDA,
and at the same time it begins to convince students that TMs are in some sense “ultimate”
expressions of algorithm design: TM = 2PDA = 3PDA = 4PDA =... .

Several of the variations covered in the next chapter could be proven equivalent to kKPDAs
as easily as to TMs directly. We did not do this for fear of disadvantaging the students
who were told to skip this chapter. If this chapter is covered in your course, keep it in mind
as a variant method of covering much of the material in the rest of the course.

Students should also be advised to read the excellent books by Minsky, not only a founder
of the subject but a great expositor.
143

Chapter Twenty One

1.(i) STATE TAPE STACK; STACK, (ii) STATE TAPE STACK, STACK,
START a2255 A A START babah A A
READ #b5 A A READ abab oA A
PUSH; 2 255 2 A PUSH 5 2625 A b
READ 55 a A READ bab A b
PUSH; 2 55 ag A PUSH; « bab a b
READ 6 a2 A READ ab a b
PUSH 4 5 ab PUSH) 5 25 2 bb
READ A a2 b READ 6 a bb
PUSH» A a8 bb PUSH; a 5 a2 bb
READ A PF bb READ A a2 bb
POP; & 2 bb PUSH) 4 4 a2 bbb
POP, A FI b READ A aa bbb
PoP, A A b POP; A P bbb
POP> A A A POP 4 a bb
POP; A A A POP; A d bb
POP, A A A ACCEPT POP, A d b
POP; A d b
POP> A A A CRASH
As the entire string is read, 4's are pushed onto STACK, J's
onto STACK,. When the input is exhausted, the stacks are
compared. To get to accept, they must both run out at the
same time. Therefore, the strings accepted by this machine
must have the same number of #3 as Ds.

PUSH ¢ kK<REaD >

we a FON re /
cer, Gr treT Fors APory POP,
ay * Ne
144

Chapter Twenty One

4.
(START |
ot eo \
(ACCEPT }<READSEREA

%.
( START =o,

VAN
ReSGPOF, S2AFUSHS
EI 5]

2a
oy io:
ACCEPT }4 : f
6.

1p

ACCEPT
145

Chapter Twenty One


({a,b,c,#],=,L)

L)

({a,b,#],=,R)

([a,b,c,#],=,L)

9.

START A ACCEPT

a Cc

Kea
b

READ Y
;
- ADD c

10.

FOP2 x ACCEPT
a
146

Chapter Twenty One

(ii)

<< s
A

Con) aa
a Cc
b

a
p
c

HWE

: 7
147

Chapter Twenty One

6
sb
148

Chapter Twenty One

14.

15.
Chapter Twenty One

16.(i) START £663 240633 Aap 393 Ashk 9 4 Arba 5 Anbs 3 6 Ashe
96Aa0h
296A26/ 3 6Aadr > 6-Ashbe
97 Ash > 8 AAD
+8AAbL°
> 8AALY > 8 AAD +8 AAD 79 AA DL 4 HALT AAD
(ii) START
622 3Aga> 2A52 92 A509 4 Absa > 5 Abas > 6 Absa
>6Absar?
> 6Abse > 6 Aba > 6- Abas? 37-Abse®
11 - bAase >
11- AA se? 11 - AAasse 9 11 - AAage 9 11 - AAaa? 9 12- AAae 3 13 -
AAs? > 14- Adasth > 19 - AAae 63 20 - AAse’ 6 21 - Abas AO 24-
AA satBA> 25 - AA se” BA - CRASH |
17.(i)STATE APE STACK; STACK» (ii) STATE APE STACK; STACK>
START abba A A START bab26 A A
READ bba A A READ asba6 A 4
PUSH? 4 66a A a PUSH? 5 2625 A b
READ ba A a READ bab A b
PUSH2 6 ba) A ba PUSH2 2 620 A ab
READ a A ba READ #5 A ab
PUSH) 62 OA bba PUSH) 5 ab bab
READ A A bbha READ 5 A bab
PUSH2 4 4 A abba PUSH) # 5 A sbhab
READ A A shbs READ A A sbab
POP) A A bbs PUSH 6 AOA babab
PUSH; # A 4 bbha READ A A babab
POP, A a ba POP, A Fal abhab
PUSH; 5A ba bar PUSH, 6 A b abab
POP> A ba a POP? A b bab
PUSH; 5A bba 2 PUSH; « A ab bab
POP, A bha A POP A ab ab
PUSH; 2A abba A PUSH; 6 & bab ab
POP, A sbbe A POP, A bab
POP AG aye PUSH; A abab
POP; A be A POP A ehah A
POP A a A PUSH;5 A babab A
150

Chapter Twenty One

19. Lis accepted by some 2PDA; therefore, by Minsky’s Theorem, there is a PM that accepts L.
In chapter 20, problem 20 there is a PM that accepts L’. (It is not possible to use the
reversing procedure of problem 19 as a preprocessor unless the algorithm either uses only one
stack for processing or simulates a PM, in which case a marker can be uses to separate the
reversed input in one stack from the contents of the other stack.)

20. (i) Use the folded tape model. If all three stacks were beg maintained on a single track tape,
and the tape was folded at the pot of the tape head then two stacks could easily simulate the
tape head movement (move right means pop 2 x, push | x; move left means pop 1 x, push
2 x.) So initially place four #’s one stack 1 subsequently pop 1-push2 the items over to stack
2 to adjust to the top of the desired stack before changing that stack. Finally before the
simulator resumes processing, restore the stacked stacks to stack 1 with a series of pop2-
push operations. For the most part stack 1 will look like:
#
stack |
if
stack 2
i
stack 3

#
(ii) Obvious.
151

Chapter 22

Despite the fact that all of these results are very important, the student who has suffered
through all that has come before should be able to create most of these proofs for herself,
with the possible exception of the equivalence of nondeterminism and determinism. Once
the trick of how to do this is seen it should be obvious that it is not important to use a multi-
track TAPE but that a few markers on the one track TAPE should suffice.

| toyed with the idea of proving that NPM=PM and that N2PDA=2PDA as separate results
independent of TMs. The trouble was that since the best place to organize the idea of “try
every alternative in sequence until we find one that works” is with TMs, and the best time
is after the introduction of PMs and 2PDAs and their equivalence to TMs, by the point that
the proof would be easy it is already moot.

The discussion of Church’s Thesis relys on the two-dimensional TM TAPE. Therefore it


is necessary to cover kTMs, at least conversationally.
152

Chapter Twenty Two

1 (i) a/a, b/b


b/#
STARTR & fetes
aimee ee AMA, #/4# \ 7 alt, b/#
al#t a/a, b/b
a/a, b/b
AJA, #/#

(ii) Each of the two plain states needs a twin state to handle the different move
directions. Also notice in the machine below that we distinguish between a move
left and move right to HALT. The tape head can always move right but moving
left from the cell 1 causes a crash. Hence we needed to add yet one more new
state.
A/a

meee
a/A

())
<u b/

(ii) {a"b"| n > 0}


153

Chapter Twenty Two

{apr |an Sch}

Convert the multi-cell-move machine to a TM by adding states: Ifthe instruction on the


transition from q; to state q; is to move n cells in direction d then between states q; and q,
insert n-1 new states each with the transition (any, =, d). These additional instructions
move the TAPE HEAD as required by the multi-cell-instruction. This algorithm
demonstrates the equivalence of the machines.
154

Chapter Twenty Two

START 7
(: ‘ r)
$, A

Bevand Scan for A (s-) Scan for #


(“3:4
-$,= A, = -$,= (“*)
=
-$, = A-A,=,R emn
-
A, A,

iS # $, $ A, $,
(‘ =, /) (:
$, ‘) (: > >

4,4 /Mark digit \** ; igit \*


Repeat this loop for each digit.

a Clearly k+1 markers are needed to separate the data section of the & tracks. In addition, in
order to keep track of the position of the tape head, we need k more markers of a different
symbol. Notice that this method provides extra versatility, because each of the k abstract
tape heads can move independently of the others. In total that makes 2k+1 markers - not
all of which must be different. Now we already have INSERT and DELETE subroutines,
so we can easily maintain that the markers surround real data without extra working
space. Any time we encounter an end of data section marker for some track we can
always insert the necessary blanks and keep working.

6. (i) 1) Scan the tape inserting zeros on the track 1 for as long as there are digits in a
cell on tracks 2, 3, or 4.
2a) For each combination of values for carryl, digit2, digit3, and digit4 such that
their sum is a single digit, place the result on track 5 and move left.
2b) For each combination of values for carry], digit2, digit3, and digit4 such that
their sum is 10 + a single digit, place the single digit result on track 5,
move left, place a | on track 1 (carry) and do not move the tape head.
2c) For each combination of values for carry1, digit2, digit3, and digit4 such that
their sum is 20 + a single digit, place the digit result on track 5, move left
and place a 2 on track | (carry) and do not move the tape head.
3) Finally when the tape head encounters the $ marking the end of the tape, bring
down any carry that might remain, e.g. copy track 1 cell 1 to track 5 cell 1.
[55

Chapter Twenty Two

(ii)

9<sum<20
\o A : + — A teQo
8

19 <sum+l < 30
Ear)<——
one SERED eee
9 < sum+? < 20

(Assume tracks | and 2 contain “$bit-string*” and tracks 3, 4, and 5 contain “$*”
1) Copy bit-string from track | unto track 4 in between the $ and * markers.
2) Read rightmost bit on track 2. (Scan right to left bitwise)
3) If the bit = 1 then align data on tracks 3 and 4, add the two bit-strings placing sum on
track 5. Copy track 5 onto track 3 (between the markers).
4) Insert 0 to the left of * on track 4.
5) If there is more data on track 2, GOTO step 2.

(i)
156

Chapter Twenty Two

ade)
Monee

(i) For inserting or deleting from track 2, each instruction takes the form (any,
>
any,
Using the notation (state, read, write, move, state) the following is a method for

INSERT d:
(1, a,d,R, 2) (1,b
(2, a, a, R, 2) (2, b, a,
(3, a, b, R,2) (3, b
DELETE: (1,a, #, R, 2) C1, b, #,
(24, 2 2 )4e4D. Dy
(3.3. A. 4). (270 AAs
(4,74. a, L,

(ii)
157

Chapter Twenty Two

5B 98
(:a ye ) (=) Compact a’s

10.

Reset head
to non-A
on Track 2

a (i) Assume that cells 1 contain $’s. Read2 means on track 2, etc.
1) scan $, write2 #
2) readl a, write] A, GOTO 3
read1 5, write] B, GOTO 4
readlA, goto5
3) reset track 2 to # - read2 #, write2 a - write2 # - reset track 1 to A - read1 A,
writel a, move right, GOTO 2.
4) reset track 2 to # - read2 #, write2 A - reset track 2 to $ - move right - read2 a,
write2 # - reset track 1 to B - read1 B, writel 5 - move nght GOTO 2
5) reset track 2 to # - read2 #, write2 a - HALT
158

Chapter Twenty Two

(ii) 5) reset track 2 to # - read2 #, write2 a


6) copy a’s from track 2 to track 3
7) insert 5 end of track 3
8) use modifies matching procedure from problem 10 to find number on track 2
among numbers on track 1
9) delete the number from the sequence including one D.
10) clear track 2
11) GOTO step 2

12. place $0# on track 2


readl a, writel 5, move right - GOTO 3
read1 5, move right - GOTO 2
readl A - HALT
read2 #, move left - GOTO 4
read2 not #, move left - GOTO 3
read2 0, write 1, move right - GOTO 2
read2 1, write 0, move left - GOTO 4
read2 $, INSERT 1, move right - GOTO 2

ee (i) 1) initialize track 2 to "1" binary one


2) copy number from track 2 to track 3
3) compute square on track 4 using the multiplier from problem 7 and tracks 5 and
6 as working space
3) if number on track | is the same as number on track 4 then HALT (square)
4) if length of number on track 4 is greater than length of number on track 1 HALT
(not a square)
5) increment number on track 2 GOTO step 2

(il) 1) use procedure from problem 12 to convert number to binary.


2) use procedure from part one to test number.

14. Use a two track TM


1) Insert #s in the first cells.
2) If Read input = a then erase it and place an $ on track 2; move right.
3) If Read input = 5 then overwrite it with B, find and erase one $ on track 2, and find and
erase B on track 1; move right.
3) If Read input = A then moving left, scan track 2 for $ and HALT: ifno $ found crash.
i?

Chapter Twenty Two

15: The tape looks like this:

@ (ui) (ui) (iv) (¥) (i) (vii) (viii)

A move RIGHT instruction (c,c',R) on 2-way TM is equivalent to the following instructions


on this deterministic TM:
= If in an odd numbered cell then move right twice i.e. (c, c’, R) (any, =, R)
- If in an even numbered cell then move left twice ie. (c, c’, L) (any-$, =, L)
Be careful around the ‘bend’, instead use (c, c’, L) ($,$,R) (any,=,R)
Similarly the move LEFT instruction is simulated as follows:
oe Ifin an odd numbered cell then move left twice ie. (c, c’, L) (any, =, L)
Be careful around the ‘bend’, instead use (c, c’, L) (any, = L) ($,$,R)
- If in an even numbered cell then move right twice ie. (c, c’, R) (any, =, R)

16.

(ii) As in problem 6, place two markers, y and z, on track 2 of the tape, in consecutive cells.
Alternately, move y one cell to the left and z cell to the right until one of the markers
in situated under a non-blank character.

(iii) The machine is as described above, except that the markers are on the same track so the
checking for an input letter must be done before moving the marker each time.

ive (i) NPM=NTM=TM= PM


(ii) N2PDA = NTM = TM = 2PDA
18. (i) PM = TM no
2PDA = TM marginally easier because each stack on separate track.
(ii) They were incomplete without TM = NTM.

19; Using the Myhill Nerode theorem, since r is regular there is a finite number of equivalence
classes for this language. Chop (r) certainly defines fewer equivalence classes. Hence the
number of classes for Chop (r) is finite and Chop (r) is regular.

20. The difference between instructions (right)(left) and (left)(right) is that the net result is the
suffix of one expression in the other as opposed to the prefix. We do not have a suffix
language that is known to be regular. However, if use the transpose if the strings then take
the correct prefix and transpose again then we have essentially obtained the suffix. We
160

Chapter Twenty Two

already know that the Transpose language and Prefix language of regular languages are also
regular. So the agglomerated instruction is

(T [ Pref( T [r,(a+b) ] in T[ (atb)r, ] ) J, L),


(T [ Pref( Chop (T [ (atb)r, ])in T[r, ] )], R),
(A, S)
161

Chapter 23

In the previous edition this was two different chapters. The amalgamation has focused
attention on the r.e. languages as a class with questions similar to those answered or
considered for regular languages and CFLs. | think | previously made too much of the
details of the decoding process. Good programmers and readers of this text up to this
point should be able to write TM code to do most of the tasks required without my help.

The encoding of TMs for the purpose of constructing the languages ALAN and
MATHISON is another example of where we have started with a basically pictorial
description of a machine and then later had to convert it into and algebraic equivalent in
order to establish some results. This again does not mean that it would have been
superior to have started with the algebraic formulation in place of the pictorial one
Originally.

The existence of the loop set, and in fact its inevitability for certain languages not just
certain machines, is a point to dwell on since it is the foreshadowing of the Halting
Problem. The sooner the ineluctability of indecisiveness is appreciated as an essential
of the language and not a fault of the programmer, the better.

We have avoided the standard black-boxing of TMs and tying them together with mystery
strings such as “feed the output of this into that’, or “run it on both machines” and instead
give explicit illustrations of how this can be done. | find that more satisfying. After the
details are covered an overall view of what is going on should emerge (as in Kafka’s Penal
Colony).

The significance of the non-r.e. language should not be minimized since it paradoxically
undercuts the promised universality of the TM. The Halting Problem should be presented
as a problem in two senses. | hope the machine LAMBDA is clear. One could, of course,
write it out as an example for some particular word if one thought that would increase any
student’s understanding.
162

Chapter Twenty Three

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

(b,b,R) (b,b,R)
@aaR)
SS (a,a,R)

(ii) (*,A,R)
(b,b,R)
AAR START (b.A.R), (kK,*,R)

4 & (a,*,L)
x
gee aD
R (a,a,L)

(b,*L) («,*,L)

(iit) (a,a,R) (b,b,R)

(A,A,L)

(a,A,R
(aA

ART) <——___ 48) 7 Hob.1)


(a,a,R) (b,b,R)
(b,A,R)
cY (A)

(a,A,R) (b,A,R)
Chapter Twenty Three
(v) > >*,R)
(*,A,R tR (b,b,R)

ee
“0g (a,*,L)
(*,*,R)
(a,a,R) \hss
FAM ee RED
2. accept(7;) is the language of the regular expression aa*b(a+b)*
reject( 7,) is the language of the regular expression a* + b(a+b)*
loop( 7;) is empty
3. accept( 7,)is the language of the regular expression aa(a+b)?
reject( 7>) is the language of the regular expression
(ab+ba+bb)(a+b)*
loop( 75) is empty
4. J, modified 7p, modified
START @ 2") (START%.
‘pad(630) yaaa
(aber h) ED meee O22 2.GRD
(4455, eyyee Hees ) A#;=,)
($A%5 eo _ M68
a (ariy,=,) (203 (ny,=,0%
7; remember that the INSERT subroutine must also be run in
alternating steps on the two machines as described in 12, above.
164

Chapter Twenty Three

BASE! Cua )
(non#Z,=,2 )
(non ¢,=,24) (4)
(START (#2824) 73 (82,6)
NG5,8,28)
4m, fariy,= 2,8). i Lars,
— )eH
(6

fons
on (ron
* =, 2

JAM)
(non 2,=,2) | Se nat )
(A%,L£
aS LP ene a faye A 2k)y, Thad)
ee
“ (8, 8%5=,22) 17)
; i \(4,4,28)
(8%)
aGny,= 28)!
OP
a
(2,422)
ou2ey19)(4 #5228)
S (i) We use the modifications of Problems 12 and 14. The TAPE
Jp PpXaAA.... The INSERT routines (after states
after Step 1 is #192
2 and 14) leave both 7; and 7; in g, and cell i symbols in the
first data cells. These traces assume this preprocessing has
been done.
(i)1 *1*24A** 9, g,AAAA... Il *192AA¥* 9, Gi AAAA 2°192AA** G7,G)AAAA
Z *1¢2AA™* g) QiAAAA 8 *1¢2AA**A GQ AAAA 10*1#2 AA**A GAAAA

12 *1*2AA**A
GAA GA rhe #1 *2AAFFA gi AA Ba 15*1*2AA**A GAs GA
15 *1*2AA**A
GAA GA 19 *1°2AA*FAAAA A 22*1*2AA*FAAAA AAA

1 *1*2AAPFAAAA G3Q2AAA .. (set for infinite loop)


(ii)1*1¢24A** 9, g, DAA... 1 14244 9, 9, DOAA 2 #1924A? 9, 9; DAA
2 *162KATF
9 DAA 7 *192KAFAG, DOAA 11 #192KA*FA9,2AAA
12 #142 MAMA Gg,aOAA 12 #192 DAMA Gg,244 15 #142 DATA 9, 2244
15 4192 HAM gA aOAA 19 4142 HAP AA ZAAA 1 "142 MA AA abd g
1 #192 DA AA cA G3 212A
AA aA 2 “12 DA™AA ahd g,
3 *1°2AAFAA 2A @3 4 *1°2AA**
GA 24@ ... (set for infinite loop)
165

Chapter Twenty Three

(iii)1°1*2AA**9,9) AAA ZAAADOAA... =| *122AA**9, 9) caAA ZaAADDAA


2 *1t2AA**7,9 2aAAaAAAD DA . . 2 *192AA**
g)9) 2abA ZaAA DDAA
8 *192AA**A 9; gahA aakA DA 9 *192AA**A9, 2ahA 2aAA DAA
12 *1#2AA**A 9) 22g;)AaaKA
DADA ... 12 #122AA*A 9, 22g,A aad DLA
15 *122AA*FA g) 2ag)A aA SDAA. 15 *102AA**A
9,22q,A aA SDAA
19 *1*2AA**AA
229,A aad DDAA 21 *1*2AA*FAA
229,48 2ahA bLAA
1 *1*2AA**AA 22g, 2 aa@AbAAA... 1 *1*2AAAA
zag; G 4ahd DAbA
2 *1*2AA**AA cag) Qa RADOAA. 2 *1°2AA™*
AA zag, G aaAADDAA
& *1°2AA**AA 224 G gahA DDAA 9 *1*2AA**AA
244 G aah\A DDAA
12 *1¢2AA**AA 34\ @ 2ag;ADOAA ... 12 *1°2AA**AA
Zak & 2ag,;ADDAA
15 *1#2AA**AA
2a aagAbOba ... 15 *1*2dA*AA
ca @ sag AbDbA
16 *192AA**AA
eahA zag) ADDAA HALT
(iv)1 *1924A%*
9, 9,@ahADAAA .. 1 *192AA™*
9,9, 2a@\A DAA
2 *192AA**9, 9; 224A
DD ... 2 *1#2AA*
g,g, aA SDAA
8 +1¢2AA**Ag) aad DOAA 9 °1°2AA**A
9, 2ahA DAA
12 *1¢2AA**Ag, 44g;ADDAA .. 12 *1°2AA
AG, 2ag,A DBA
15 *192AA Ag, 24g,A50bA . 15 “1#2AA**A
@) 28Q;A DDAA
19 *1*2AA**AA 229,4 DDAA 21 *1*2AA**AA
229,4 DDAA
1 *1¢2AA*FAA 22g, SDAA ... 1 *1¢24KA**AA
249, G DASA
2 #1*2AAP AA 229; DDDAA .. 2 *1°2AA**AA
22q; G SDAA
8 *1e2AATFAA
aa @ DDAA 11 *1¢24A**AA
aah g DAAA
12 *1¢2AA**® @AaAQODAA .. 12 *1°2AA™*
DA za G SDAA
15 *122AA™* @AaaA GQ ODAA .. 15 *1¢2hA**
DA aa @ DDAA
16 #1*2AA** GA aad ODAA 17 *1°2AA**
@A ahd DOAA
166

Chapter Twenty Three

Use the algorithm in Chapter 19 to convert an FA into a TM. The - state becomes START;
a transition labeled a becomes an instruction (a,A,R); a transition labeled 5 becomes an
instruction (b,A,R); from every + state add the instruction (A,A,R) on an edge to ACCEPT;
from every non+ state add the instruction (A,A,R) on an edge to REJECT. Since every
instruction cause a move right, eventually the input string will be exhausted and when the first
blank cell is encountered, the string is either accepted or rejected.

Yes. Don’t worry about nondeterminism: Make a TM that performs the CYK algorithm,
taking a CFG and a target string as input and accepting the string if it can be generated by the
grammar and rejecting it otherwise. The CYK algorithm is a decision procedure for
membership for CFL’s; no input causes it to loop indefinitely.

Recall Theorem 60 which states that if a language and its complement are both recursively
enumerable then they are both recursive. L is the complement of MUN and MUN is r.e.
(Theorem 62). Hence L is recursive. By symmetry M and N are also recursive.

Notation: R= recursive RE = r.e. but not recursive NRE = notre.


Consider the following table of apparent possibilities:
i p} 3 2 4 5 3 > 6

Equivalent cases have been given the same number. Case | is (i), case 6 is (ii) and case 5 is
(iii) all as given. Cases 2 and 3 are impossible by Theorem 60. Case 4 is impossible by
Theorem 61.

10. (i) Let R, be the machine that accepts L, and R, the machine for L, then we construct R,
by the algorithm of Theorem 61. That is simulate running the two TM’s alternately on the
one input string. Ifthe string was accepted by either machine then it will be accepted by Rg.
Accept (R;)=L,+L,. L(R 3) is recursive because loop (R ,) and loop (R ,) were each
therefore loop(R 3) = @. Reject (R ;) = reject (R ,) M reject (R ,) + reject (R ,) M loop (R 5)
+ loop (R,) N reject (R,) = L,' nL,’.

(i1) Using DeMorgan’s Law, we can conclude that the intersection language is recursive
using the facts already established that complements (in Theorem 60) and union of two
recursive languages (as shown above) are recursive. _ If one wanted to construct the
machine, it would be similar to the union machine; simulate testing a string on both machines
simultaneously in alternation. Only difference is that once one machine accepts the string then
the simulator must continue to test the string on the other machine without forgetting that if
and when this second machine (with longer processing) accepts the simulator accepts and
HALTS.
167

Chapter Twenty Three

11. Ifloop(T) was finite then the machine could be modified to check for those particular set of
strings and reject them. The result would be an empty loop set and the language would be
recursive. Since the language is specifically not recursive the loop language must be infinite
and moreover it cannot be regular.

12. Construct a TM that accepts the product of two r.e. languages. Instead of testing one input
string on two machines, we need to try all possible ways of dividing the input into two strings
testing the first substring on machine for L, and the latter substring on the machine for L,.
(There are +1 ways of dividing the input, where 7 is the length of the input.) Again we must
use sufficient interlacing to avoid getting stuck in loop before exhausting all choices.
Altermatively we can simplify the solution by using a nondeterministic machine to magically
divide the word into two in the most favorable way.
For product the division was simply into two parts, but for Kleene closure there can
be any number of parts. The complexity using the first method would increase dramatically
because of the multiple nesting necessary. However using a similar nondeterministic machine,
Kleene closure can be implemented easily, by magically dividing the string into favorable
parts.

13: Spaces inserted for legibility.


(i) ababababb ababbabab abaabaaaba b‘a(atb)* ALAN

(ii) abaaabaabab aaabababbba aaabaabbaaab a ALAN

(iii) abaaabaabbb abaaaababbbb aaabaaabaaaab aaabaabbaaab aaabaaaababaab


aaaabaaaabababb aaaabaabbaabb aaaabaaabaaabb (at+b)*

(iv) abaaabaaaab abaaabababb aaababaaaab aaababababb aaabaabbabab


(Strings of odd length) ALAN

(v) ababaaaba abaaababaab aaabaaabbabab aaabababaaa o ALAN

(vi) abaabbabab abaaabaabab abaaababbab aaabaaabaabab aaabaaababbab


aaababbaaaa (atb)*

14. Yes, consider the palindrome abaabbaaba for the TM START A,b,L (HALT |

15: (i) ALAN START #,A.R

(ii) ALAN START a,#,L


168

Chapter Twenty Three

(iii) MATHISON START a,#,R

(iv) MATHISON START a,b,R ei} b#,R

ie
(vi) ALAN, this string is not in CWL.

16.
a*ba*b(atb)

(a,a,R)

a eel
b(atb )[(ataaa)a*ba
+ *ba‘b(atb)*]*
b(atb)’} HALT

We use regular expressions as edge lables as a shorthand for inserting their FAs in TM
form. There is a simple regular expression for CWL. However we must be certain
that the word has the two properties that make it a TM: There must be a START
state and there may not be any edges emanating from HALT (state 2). The above
machine accounts for these things. Also note that this machine accepts encoded
nondeterministic TM’s.

7; Blue paint would follow the simple path from START to HALT and claim that the machine
accepts some word(s); in this case (atb)(atb)a. However because of the abilities to move
left and to write on the tape, the existence of a path is not sufficient proof of acceptance of
a word. As we see, since the first edge writes a 5, it can not possibly back up and read an a
from that same cell.

18. Examine T,,; it accepts all strings if and only if T, accepts the particular string w. T, accepts
all strings except w if and only if T, does not accept w. Imagine that there is a machine
ACCEPTALL which takes an encoded TM as input and outputs yes or no. Then use T, to
be the input of ACCEPTALL. The effect is a machine that will decide if T, accepts w. This
is the Halting Problem which is unsolvable. Hence there is no such decision procedure for
ACCEPTALL.

19. Let T, be a machine such that accept ([F ) = (at+b)*, then this EQUIVALENCE machine
reduces to ACCEPTALL of the previous problem.
169

Chapter Twenty Three

20) If the word “heterothetic” is homothetic - it really does describe itself. Of course,
because it is homothetic it also doesn’t describe itself; it describes its opposite. So
“heterothetic” is also heterothetic. But because it is heterothetic, it becomes
homothetic. And so on.
170

Chapter 24

The proof by Chomsky and Schutzenberger (of whose recent passing | have just sadly
learned) that type O = TM, | consider to be a triumph of mankind equal to any work of art
or science. Any who skip this material are risking my personal wrath.

It is always a delight to find a phrase-structure language that is not context-free.


Unfortunately almost none of the examples is easy. Also the limited relevance for
Computer Science (as opposed to mathematical logic or philosophy) disallows us to
indulge in this protracted esoteric adventure.

New in this edition, but important for gratifying comprehensiveness as well as their
important Computer Science application, is the discussion of context-sensitive languages.
The next fifty years will see natural language processing as a dominant issue. Teachers
who know some Al teaching students who want to know some Al will easily bring in the
relevent applications.

How explicit one wants to be about the usefulness of the linear bound in |bas is a matter
of choice. | considered it important for two reasons: it distinguishes a different type of
machine for a different type of grammar, and because in answers the question of
decidability of membership, which distinguishes CSLs from r.e. languages.

The product and Kleene closure of r.e. languages could have been done in the previous
chapter but that would have deprived us of the pleasure of cooking the “obvious”
grammatical proofs. And since this is book about learning what a proof is and ISN’T (as
well as about pleasure) we delayed this presentation till here.
171

Chapter Twenty Four

1(i) S3 ABS > ABABS > ABARA = ABBA .. > abba


(ii) S3 ABS > ABABS AABABABS > ABABABABS
> ABABABABABS > ABABABABABK = ABABABABAB
> BAABABABAB »>BABAABABAB >» BABAABBAAB
%* BABAABBABA *BABAABBBAA.. *babaabbbaa
2. Prop | (a) expands the working string by one A and one 2
Prop | (b) eliminates the Start symbol S Prop's 2 and 3 work on
pairs of A’s and #s, and do not change the number of either. —
Therefore, we must always have the same number of 4's and &s
in any working string. The terminating productions are simple
replacements of A with a and Awith & since we had the same
number of A's and As we must have the same number of Zs and
Os in any strings that we generate.
We offer az algorithm to generate any string having the same
number of 2s as S's. Apply Prop | (a) half as many times as
there are letters in the desired string, then apply Prop | (b).
(This gives us a working string of the right number 4's and Bs in
aiternation.) If the first letter of the desired string is a use PROD
4 on the first A in the string, if it is 5, use ProD 2 and then apply
Prop 5 to the leftmost & Thereafter, if the leftmost capital letter
generates the next desired terminal in the string apply Prop 4 or
5, otherwise, apply Prop 2 or 3 (only one will be possible) to the
leftmost dissimilar nonter minal pair until the leftmost capital
does generate the desired letter, and then apply Prop 4 or 5 to it.
We know that we began this step having the right number each
of A’s and #s. Any leftmost position wrongly occupied by, say,
an A means thal there must be a & somewhere to the right that
we can use to fill the spot. If it is the very next symbol, we doa
simple swap. However, wherever it falls, there must be an 4
immediately to its left, so we can apply Prop 2 as often as we
must until we have applied it to that leftmost out-of-position A.
An analogous argument applies if the leftmost out-of-position
symbol is 2
Every time we place a new symbol in its proper position we
172

Chapter Twenty Four

reduce the number of symbols remaining. Since we must have


had a finite number of symbols at the end of the first step, the
procedure is finite.
Sa ABS \|A
AR AGEA
BAL Ae
Awa aAla
Boab
The grammar of Chapter 13, Problem 19 will also serve.
(1i) Spal toe 2
5,7 Ay A,B, 5 | 2 S29 B, By AS, \ b

AB, > BA, Ayb, > B, Az


BA, > Ay SB, B,Az > Arh
Ay > aA |a A» > @

B 7b Bw bB,\ 6b
(iii) EQUAL is recursive. The TM built in Chapter 24 (pp. 569-573)
accepts or rejects all strings. Or, by Chapter 28, Problem 11, all
CFL's are recursive, and EQUAL was shown to be context free in
Chapter 13.
S.(i) 5 > ABCS > ABCABCS % ABCABCA> ABACBC® ABABCC
> 9 2bacc
(ii) S% ABCS® ABCABCS? ' ABCABCABCS® ABCABCABCA
= ABCABCABC »% ACBABCABC % CABABCABC
» CBAABCABC 9% CBAABCACE % CBAABCCAB
> CBAABCCBA > ..> chaabccha
6. The argument is similar to that of Problem 2: We generate the
terminals 2 Sand c from the simple nonterminals 4A, Band C
respectively; we always add one of each or none at all, so we can
never have a working string with unequal numbers of (Aor 4),
(Zor 6) and (Cor o, and therefore never derive a strings in
which the number of each does not equal the numbers of each of
the others.
Chapter Twenty Four
7. Again, the argument is an extensionof that of Problem 3. Props 2
and 4 say that if an J is out of place to the left, it can be moved
right; Props 3 and 5 say the same for & Props 6 and 7 for C Props
5 and 7 say that if an A is out of place to the right it can be
moved left; Props 2 and 6 say the same for & Props 3 and 4 for C
Therefore, any of these letters (and they are the only ones we
have to work with) can be moved either left or right past either
of the others, so all permutations can be derived.
8.(i) S29 GVY2 AXA
(ii) S39 UWVY> aUyVyY> aUVarxy> avakt> ava = 42
(iii) 5S OVX bUZ4% bYVbX® LALA HALA = bb
(iv) S32 OVA? aUVYyX> aUVarX> abUuZax> abUaZX
> abUaVbAX> abUVabX> abv abXk> ab abi = abab
4. We can make the left w of our string by applying Prop 2 if we
want the next letter to be 2 and Prop 3 if we wantittobe 6. (If
the string is already long enough we use Prop 10.) Until we reach
this point we always have a UV in the working string when we
are ready to choose the next letter, at the beginning from Prop 1,
thereafter because Props 2 and 3 leave a // in the working string
Props 4 and 5 return a V which is moved immediately to the
right of the J by Props 12 and 13.
A working string with a ¥ or a Z in it has added the next letter
to its first half but not yet added that letter to its second half.
Props 2 and 3 introduce the new first half letter and the Y or Z
for the second half. Props 4 and 5 add to the second half of the
string the same letter that was added to the first half. Prods 6-9
move the ¥ or Z to the right where it can be expanded in
combination with the final Y That expansion restores the V to
the working string and Props 12 and 13 move the V left until it
meets the & This leaves the working string in the form wUVwX
which can either be expanded by repeating this process or
terminated with Props 10 and 11, making it ww.
10.) wOVwY (0S pA wy Ul wA WA. We have no other choice.
174

Chapter Twenty Four

Gi) wWOVwY CDs wUVanA= wy (Os vpAw = vw


(> val Yws.> walwy
3 polZv3_% wbhUwZ
We cannot use either the FY or the Z without a final XY nor can
we use the // without a V to its right. These sequences do not
produce words.
(ii) VUVWY 2 wal/YwF (That is, we have lengthened the
first w by adding an z to its right.)
Consider the possibilities: PrRoD 1 needs an 5, and we have
none; Props 2, 3, 10,12 and 13 need a V and we have none;
Props 5, 8 and 9 need a Z and we have none. Prop 4 requires
the F adjacent to the ¥ and if our current wis not null, it is
not. This leaves us with Props 6 and 7, and one of them must
apply if w is not null. In fact, we find that we apply them
until Y is the next to last symbol in the working string
(adjacent to 1) and then we apply Prop 4. This gives us a
working string of the form waUwVaX The only possible
productions we can use are 11 , 12 and 13: If we use 11 we
eliminate our final Y and must either stop (by eventually
eliminating the //V pair) or land in a nonterminating
sequence. If we use 12 or 13 (depending on the last letter of
Ww) we begin the process of moving the V to the left until it
meets the //resulting in a string of the form wal/Vwakt
(iv) By the same token, if we apply Prop 3 the starting point is
wb UZwX which results in a nonterminating sequence (if we
apply Prop 11 prematurely), the word wowd(if we move the
Z tothe right and then apply Prop 5, followed by Prop 11 and,
eventually, Prop 10) or a working string of the form
WLU VW to be expanded further or terminated.
(¥Y) Problem 11 establishes that any string of the form wR can be
generated from this grammar. Problem 12 establishes that no
other strings can be generated from it. Therefore the language
generated by this grammar is exactly the set of all strings of the
form ww, where wis any string of #s and J's: that is,
DOUBLEWORD.
V5

Chapter Twenty Four

}4(i) The nonterminals C and AP always expand the working string.


We do not test them once the length of the string is 5.
ep eer es)
CD ee

AV ORES ti nacre Tiga a fF


AACBBD” AABBD ACEaD Hiv io fs
AABEs A CazD 25Da Aaa
AA£fss AA#as8 sis
A Agsa a288

a3a4A

ii,S (2 CD (3 ACBD 4k AABBD 5) AaBABD


(6) 24 BA BD (5)> 2zaBAABD (5 2aBAgBAD
(> gzaBaABAD 7% aaaBABAD 5% aaaBaBAAD
(= zgaaaBBAAD '8 aaaaBBADa 89 aaaaBBDaa
(> gaaaBEaaa (10 gagakaaaa “9 gaaaaaaaa
(iii) Consider Props 2, 3 and 4. By Prop 2, 5 gives a C part anda D
part. Prop 3 allows the addition of an A to the left of the C and
a # to its right and Prop 4 eliminates the C from the working
string, replacing itby AZ In other words, for any 2, to derive
the string A”?f2D apply Prop 2 once, then apply Prop 3 #1
times and then Prop 4.
(iv) Prop 5 is applied every time there is an A to the left of a & It
adds an ato the string and moves the 4 to the right of the &
(Props 6 and 7 move the newly introduced 2 leftward, so all
upper case symbols are together at the right.) Prop 5 is applied
exactly as many times as there are 4's left of &s. Since we
started with exactly 7 4's to the left of exactly 2 &s, it gives us
m #3 inthe string when every 4 is to the right of every & The
PD that was introduced in Prop 2 has not been affected by any of
these productions, so our working string is of the form
gl Bn AN)
176

Chapter Twenty Four

(v) The working string can contain at most one 2 For every A
preceding it, Prop 8 gives us one a Without removing the Jfrom
the string. Then Prop 9 introduces the new nonterminal £ (at
the first AD occurrence) and an 4 and Prop 10 continues the
process of giving one afor each & We started with exactly 2
A's and no &s. Prop 8 gives us amore 738, ProD 9 gives us one 2
and Prop 10 gives us (71) as, for a total of 27 as. We also
have the nonterminal £ which gives us aby Prop 11. This is
2n+1 4s in addition to the 2 already accounted for in (iii),
for a total of (72+ 27+ 1) 2s, that is, (2 +1)? of them.
(¥) ProD | gives us the word 24 or al?
We have shown that application of Props 2-7 gives us a working
string of the form ga*
Ba An / and that this string eventually
gives us a word of the form 2(#!)?. Therefore, this grammar
can produce any gn
As we have seen from the tree (Par¢ (‘)) _— if we try to apply
ProD 9 before moving the 4's to the right of the 2's we cannot
complete a word. So the only sequences that produce any
words are those which give us 29*for some 2.
This grammar generates the language (26727, n>0.) Wegeta
single initial afrom Prop 1. Then, by applying Prop 2(b)
immediately, we get the word 262 If instead we apply Prop
2(a) we add no more 4s to the left of the string, we keep a
single YY part, add one 4 anda /Z part. Prop 3 moves the 4's
left through the Z's. When the 2's are all to the right of the 4's
(just left of the final 2) they can become 4s, otherwise they do
not terminate. Therefore the only words that can be formed are
those with a solid clump of #s on the right preceded by a solid
clump of 4's, after a single initial 2 Any such string can be
formed: Apply Prop 1, then Prop 2(a) 7-1! times , then apply
Prop 2(b) and finally Props 3 and 4 to terminate. To change this
to a grammar for #°6°#2 make Prop 2(a) read Y¥> 2¥VbA.
3. (i) 2b, aabb, 288bbb, aaaabbbb (ii) (#962)
177

Chapter Twenty Four

14. Combine the context-sensitive grammars the same way we combined CFG. Ensure that the
nonterminal symbols for each language are different. Let the start symbols be S, and S,
respectively. Add the new productions S > S,|S,. Since there is a CSG that generates the
union language, it is context-sensitive.

1D; Again ensure that the symbols used for nonterminals in each grammar are different and add
the new production S > S,S,. The new grammar produces exactly the words of the product
language.

16. We do not have a simple technique for combining the grammars however we can describe the
TM that would accept the intersection of two languages. Using Theorem 79 that shows that
membership is Turing decidable for type 1 languages, let T, be a TM that decides if a string
is m one CSL and let T, be a membership TM for another CSL. Then construct a third TM
which takes an input string and runs it on T, if the result is no then reject the string but if the
result of T, is yes then run the string on T,. Again if the result is negative then reject and if
positive then accept; the string is a word in the intersection language.

7. Even more simple than problems 14 and 15, we can form the grammar for the Kleene closure
language by adding the new productions S > SS| A.

18. To construct a grammar for the reverse language, transpose each side of each production of
the CSG. For example XYZ~- BbaaaT becomes ZYX~- TaaabB. The length
condition is maintained and so the grammar is still context-sensitive.

1S (i) Convert the grammar in a way similar to the way we converted CFG to CNF. Except for
productions which are already i good form, change all terminals to appropriate nonterminals.
When necessary, add the corresponding N - t. Now shorten strings on either side of the rules
by introducing unique nonterminals to represent the remaining part. For example, XYZ -
BaCD first becomes XYZ ~ BACD (adding A > a if it is not already a rule.) Then XYZ
becomes XR, where R , > YZ and BACD becomes BR, where R, > AR, andR,; > CD.
The replacement productions of XYZ + BACD are:
XR, > BR, Ri > ¥Z R, > AR, R, > CD

(ii) Yes. Comparable to the CYK algorithm for CNF, use a variant for KNF where the
various possibilities for each length are tested.

20. (i) Remember that the productions of the grammar are restricted in length such that the
working string of any derivation of any word grows increasingly longer. Since there is no
way of shortening the working string, at every point in the derivation the length of string must
be less than or equal to the input if the word is to be derived at all.

(ii) Recall from the text that the format ofthe tape is $input$
178

Chapter 25

There is no doubt but that this material on recursive functions is anticlimactic, obvious, and
tedious. Still, it is necessary to include it here in order to prepare the student for possible
future investigations into more advanced topics. | wouldn't waste too much time on it
(especially since the term is probably over already anyway) but skip straight to a quick trip
through Church’s Thesis (a must) and on to TMs as language generators.

The theorems about language generation in lexicographic order are not only important but
tie together several key concepts form the course: what the problem in looping is all
about, why algorithms have to have predictable stopping times, why nondeterminism can
be simulated by determinism but at an important cost, and why doing many things at once
is a necessity but not a complete cure.
P79

Chapter Twenty Five

1. (i) START 2262 START 2264- START 22625 1 222431 aaazah


492 48a@a&\ 32 a2aa\A > HALT 22aA\AA
(ii) START 225 START 290 START 22671 2aa,742 aagn
+ HALT aa\A
(iii) START 6aaa4 1 aaaa>\ aaaa> | aaaa4+i aaaavy+2 aaaas
> HALT 222A
(iv) START 63 1 a> 2 a> 2 AA HALT AAA
eae Oe

(START
—$—(8,9,h'}
> — (44,2) 'Oye sO arwai HALT )

ERE es ae -[3,3,2)
59 Pd f
eR EE) (55,4) oa Ve G54)
D, be :

3. Use the aaepaes described on pages 769-771 to add the second


and third addends. Delete one $ and the zeros between where it
was and the other $, and add the result of the first addition to the
first addend by the same means.
4. Step 1. Change the first blank after the second $ to *.
Step 2. Read the character immediately to the left of the second $.
If it is $ go to Step 5. If it is 0 delete it and go to Step 4. If it is |
delete it and go to STEP 3.
Step 3. In the (blank) cells immediately following the occupied
cells, write as many #s as there are *'s after the second $. (That
is, mark the leftmost unmarked *, move right to the first blank
cell, write an 2 move left to the first unmarked *, mark it, etc.
Unmark the *’s.) Go to STEP 4.
Step 4. Double the number of *'s. (This can be done by the same
method that was used to count 2's in STEP 3.) Go to STEP 2.
Step 5. Delete the two $'s and all *'’s. The number of 2's equals
the binary number represented by the original zeros and ones.
180

Chapter Twenty Five

5{i) START 222622 31 Aaabaa 741 Aaabaa 31 Aaahaa


92 Aaabaga 2»2 Aaahag 22 Aaahbam 23 Aaabhag
»5 Aaabas 35 Aaabaa 36 Aaaba 37 Amba
+8 Aaba ~8 A*@bawA 73 Aba 25 Aa bA
+6 ARLE +6 Aa tA +7 AAAbA +7 AAADA
+8 AAA DA +3 AAA DA + 4 AAAAA +4 AAKAAA
> 4 AAAAA + HALT akAAA
(ii) START 262249 1 Abaaa 32 Abaaa 32 Abaaa 22 Abaaa
92 Abaawh>3 Abaag75S Abag 25 Abga 275 Afaa
+6 4b6az2 7~9Aba@A2 710 AAg2? 710 AAAg 710 AAAAA
+ HALT AAAAAA
(iii) START 64a CRASH
(iv ) start 2226 31 Aaah 21 Aaah 41 Aaah »~2 Aaabda
+3 Aaah 794 Aaa’ +24 Aga 34 Jaa + HALT 2@2
6. (This change simplifies the machine on p. 775.) The label from
START reads (5,6,A4). State 4 is eliminated entirely so that (5A,Z)
from 3 goes to HALT. The edge from 6 to 9 is changed to (4,6,A).
This machine assumes that the two inputs inputs have the same
number of bits and that the data is in the proper form. (It is
easy to write a preprocessing routineto check form and pad the
shorter string with high-order zeros.)
The single dot state is a no-borrow state; the double dot marks
the borrow state. In these states we erase (and remember) the
last bit of the minuend. We cycle right to the last bit of the
subtrahend and erase it. Now we go left to the blank in the
minuend and write in the difference, moving into borrow or to
no-borrow as necessary. If we reach the left of the minuend at
no-borrow, we just erase the second $; if we reach it in borrow
we change I's to 0's and erase the $.
181

Chapter Twenty Five


($55) (F4£).
(GL) (CL0,2)
(ae Hes) (11,23
(4.4,.2) (ae 21 Lies i
C10, CEOs ‘ el) bd) MN)
Meia pe) pe y ¥ vod a
“ pee: " 4) . : Wai) ers —esgre
(hay a
. iea @ALe

GNC ae
ML, : Bees

CO Fe
(2.1.2) 8,5] a : lesa

toa) AS CO ner genes pane


(ane) 7 TG) Galt) (LOLA) 7
OLA) N f (GGA) —— (1,1,one ABE
& be, a | yA Ce) AG cer )
(CLOAI Pe Gai 1UVs) (4,4,£5 :
(1,1ANG) \ {4 1 L) ne

Y Lex 99)
3.4)
(START)
C12)

8. (i) START 2aaba 71 Aaaba 71 Aaaba 41 Aazha 7-2 Aaaba


92 Aaabak 73 AaaharS Aaaby +6 Aazaby +37 Aarby
+8 Aarhby 13 Aarby 74 Aarby +4 Aarhyn 710 Aarby
710 Aazfd 210 AzxyAA 710 Agaba +10 AzaA + HALT 222
(ii) START 5224 CRASH [Suggested interpretation: It is always
preferable to design a more robust machine]
(iii) START gahaa 371 Agbaa 71 Aabhaa +2 A#ahga 272 Aabaag
92 Aabam® 493 Aabaa 35 Aabay +5 Aabay +76 Aabay
47 Arbay 48 Arbay 38 Arbay ~3 ATGay 425 Arayy
+6 Arbhbyy 396 Arbyy 29 arbyy 79 aabyy 79 saayy
39 a7a@vpy 79 27@AA 911 27H%A 911 #22X%A 911 22ahd
HALT @ahAA
(iv - vi) Let us call the first number mand the second vw. If m> @
(and a> 0) the TAPE HEAD is in cell 7+2 when the machine
reaches HALT. If they are equal it is in cell m+2; if a> othe
Tare HEAD is in cell ii when the machine reaches HALT.
182

Chapter Twenty Five

9. Again, this machine assumes that the inputs are the same length.
(cha£itaa2) ‘ (START ) 2
CO,0,4")
Guihee geBigsa Co.
es$9.0 4% 4G. ,
~40 B44) e, (LEAF \G)2/8)
’ 7* & se Se my : a

© Ae x :
(AA (ab,8) ye ws ne
mae A) Z Ms
(Aa.0)Sankey)
LT 4
1) aiiee
oa
(1,8,4)des ee
/ A a =
ye
ye (2.38)
,
GIA.
NGSad
ea
DUG).
ya a a] | ats
C12 (44,4)
8 eeuso S @,
“DELETE AGE)
—--———{ }
leSO SVUS) |\ $2)
(y, 2, +) CC,C1,agy) a

epmece beastie ~
WS
10. This machine interprets any string in b(ab)* as Papi Atie
(where mis the number of 4's in the input and the value of the
/'th number is the number of consecutive 4s following the th
b) and leaves as output some string in ba*, where the value of
the number is the number of 4's in the longest input clump.
—_ (Aoeje t4,3,)
( START A © we
F (8,5,4 (aan NeSw mers
(65,8) 2*, *
(2,3,8) -y
. ix ,¥,2) (3A) Bank) aR
Ca9% 1%,4,4)
(SSA! evel 4 mame es
[ 662) J ex) (2,a,8)-
(é,8,2 )-—————— fe SY a at
S00) Ri ie —+ DELETE}
[) 4 HALT
7hCOpare O xmemeg
+ DELETE |
(555)
&,*,5)

11.(i) IDENTITY start 22> 1 223 1 #@\ 3 HALT 2a\A


SUCCESSOR START #2 4 9 22% 9 4@A HALT aaah
183

Chapter Twenty Five

(ii) IDENTITY start gaab2> 1 2gaba> 1 aagba7 \ £88D8 CRASH


SUCCESSOR START #282647 9 #gaba7 9 2agba7g aa8aba CRASH
2. IDENTITY SUCCESSOR
dete
e UA), a
!
eae -
(4,4,4')
START
Teemaocae
: Get)
GES a (a) HALT ¥(01,2)
LS: START DOaaabhabaaha »3* baaabhabaaba +7 4*haaabhabaabha
+ 4*Aaaabhabaaha + 4* Aaaababaaba 2 4*Aaaababaaha
+5 "Azam abhaabha > 5S *Asam@Abazha +S *Aaamdd aaba
+5 *AazasdAdé aha 45 *Azam@sAdssA ba 75 *AaaasAAAAA
> 5 *A @@a@\AAAAAAA Note that this input represents 6, not 5,
inputs. This machine does not check for form.
1 4.(i)
(9,9,7) (2,9,£) (9,9,4°) (2,4,47)
(55) (44,2) Neal (2,44)

(START
(2,4, 3 (Sets

aoe
a (A431
“a : jt
ca soe!
a
ae
4)}———415)
t piers)
{HALT} te
(48,4) uae £Xe
ex)
(445)
(ii) START
aabaaabaabaaba +| Aabaaabaabaaba
~|Aabeaabheaabhaabha ~2 AF aaabhaahaabha 92 Af aaabaabhaaha
23 AF aaabaabaaba >| AA* aaabaabaaba +1 AA* gaabaabaaba
+1 AA* 2zaabaabhaaba +) AA* s2abaabaaba +1 AA* 2226a2abaaba
92 AA* aag aabaaba 932 AA* 2a% aabaaba 92 AA* aad aabaaba
+2 AA* aaz# aabaaba 92 AA* 2am aabaaba +3 AA* aad aabaaba
+4 AA* gaz aabaaba +4 AA* aga aabaaba +4 AA‘ aag aabaaba
94 AA* aad aabaaba 94 AA* aad dabaaba ~94 AA‘ aaa aabaaba
24 AA‘ zaz aabaaba +5 AA* aae aaaaba 95 AA’ aadz a@tAaba
+5 AA* 2am aad ba ~5 AA* 282 a@AsAa +5 AA‘ 284 @&AAAAA
+6 AA* 727 2@aAAAA +6 AA* 72d @asAA +6 AA* 2a 2akA
96 AA* 222 2ah 96 AA* aaz 24 +6 AA* 22% 228
36 AA* 227 24 97 AA* aaah 4a 27 AA* 2a@A 484
27 AA* BAA 228 +7 AA*AAAA 22 77 AAAAAAA28 ~— HALT
184

Chapter Twenty Five

15(i) start babaa> | babaa 7-2 babaa = - 3 babaa


+ 4 babaa + 4 babss > 5S babat® 3 5 baba
+ 5S baba 3 6 babat 7 6 babaze 34 7 babat
+ 8 DAbat 39 Adbaet 3910 MSA 310 MOA
910 ADAMA 311 MbAR a 911 MbAR a 911 MbARS
411 AADAs a 312 DADAM a 910 LADAAR 2 310 LADAATR 2
910 DADA A® a 11 DADAA*ga 911 DADAA® 2211 GADAA®22
4312 ASDA A* 22913 DADA A* 28313 DADA 22313 LAbaA*2a
914 DAbad aa315 DAbadt aa7 5 GAAaeaa 315 DAAAg22
aa 318 AAAAAs£4 > 18 AbAAAsa2a 718
+15 DAAAA* DAAAA sam
+19 DAAAA 224 ~20 AAAAA 2a\ » 20 AAAAAZa 720 AAAAA 22
+18 BAAAzaza 318 DAAAa2z2 318 DbAAzaa 719 AAA 222
920 DAAAZa@ 320 DAAAZR 8320 AAAAa2) 318 AdAza2
318 fAAzz2a 318 DAAaazak 3919 AAbAza 320 AdAaa
320 DAA a2 318 DAa@ga +218 AAzzg 318 LAzaa
919 DAaaa 320 DAaa& +20 DdAga ~20 fdkaa
918 b444 218 6424 +918 b2axH 919 6222
320 baak +20 baa +20 baa 4 HALT 22
(ii) START 82226528 + 2 baaaba + 2 baaasha
>» 2 baaaba +> 2 baaabha + 3 baaabha
~ 4 baaaba > 5 baaah# + 6 baaabhzt
»> 6 baaahe + 6 baaabhs + 6 baaahbge
+ 6 fPaaabhe > 7 bgaabs + 8 fdgabz
+ 8 DAaghe% + 8 DAaahe + 9 pbdaazhz
+10 fAaabA*t 210 BAzabAta > 11 BAaabA*a
411 fdaabA*a 211 baabA* a +13 DAaabA*a
+13 LAaabheXa +14 DAzahe 2 +16 Mashs a
+16 bAazbhza +17 bAaahbFa + 8 hbAab2a
+ 8 AbAzbsta >» 9 DbAshbHa +10 DAA2bA# 2g
+10 DbAazbAta +10 GAA 2b A* ad +11 GdA2bA¥22
911 DAsbA%aa 3911 MAb A* 2a +12 DAAabA*aa
+13 DAasbA*aa 713 DAsh#2a 314 DAAgb#22
+16 DAAabstaa 3917 DbAabsaa + 8 DAAAD#H#
aa
+ 9 DdAA
ba 2a +10 DAAADAR2a + 10 AAAAADA*ga
185

Chapter Twenty Five

> 10 DAAA DLA* 2a +10 DAAALA® ca + 11 AAAADAPF 222


+11 BAAASA# aaa > 11 BAAAS AR 222 +11 BAAASAR 222
+12 DAAA DAR 22a +13 DAAASA* 22a > 13 AbAAD# 222
+14 DAAAD # aaa > 15 DAAAD# 222 3215 DAAAA Ss 42224
> 15 BAAAAAS gag > 18 GAAAAA 2422 +18 GAAAAA 2222
+18 DAAAAA a24g +18 DAAAAA 22a m& +19 DAAAAA 2244
> 20 GAAAAA aaa + 20 DAAAAA 2424 > 20 DAAAAA 248
+20 DAAAAA aaa +18 OAAAA saaa + 18 BAAAA aaea
+718 BAAAA 22282 +18 SAAAA 2828 +18 BAAAA saa
+19 DAAAA 2222 > 20 DAAAA 22a +20 DAAAA 28a
> 20 DAAAA 222 +20 DAAAA 222 +18 DAAAazaa2
+18 AAAA 2222 +218 AAAA 2a28a +18 DAAA saaaK
+19 DAAA @aa2a > 20 DAAA 22a +> 20 DAA saa
> 20 DAAA g22 +20 DAAA saa +18 DAAg@#@asr
+18 DAAgazaa 3218 GAAgazaa +18 DAAaaaa
+218 DAA stam +19 BAgssa +20 AAA saan
+20 DAA Za2@ +20 bAA aaa +20 DdAzaa
+18 fd2a22 +18 bAaaaa +18 LAaaaa
+18 AAazzae +19 bA2a22 +20 DAazaa
+20 fAaa2 +20 fdA2aa +20 fi 2242
+18 624224 +» 18 62242 +218 02222
+18 b4aa72X@ +19 ba48ag +20 b2aa
+20 baaa +20 baaa + 20 baad
+ HALT £422
16. We can do this modification to the first part of the MPY routine (on
page 785) by altering one label and adding two new states:
First, change the instruction from start to state | to(4, & RX),
marking cell i. Then add the instruction (4, 4, #) from state | to
new state 1°, to account for the possibility that the first input is
zero, and the instruction (4, 4,Z) from state 3 to new state 3’, to
account for a second inputof zero. At state 1° there is a loop
labeled (z a, &) (to check the form of the input) and an edge to
3°: (AA,Z). In state 3° the form of the input is correct and one is
zero, so we erase everything: (4A,Z) and (4A,Z). We have
186

Chapter Twenty Five

finished when we reach the # that marks celli. We change it


back and move right and into HALT. (The rest of theTAPE 1s now
blank, so we need not delete anything.) The whole routine is
gia tic ues (3,9,0 7<
Sear Saas
(STA eae
Ht
ee
pM
a besfine “Lad
‘ae al as
1S
aie)
F Fea &pt
manic .
Pe $e an (2.4.6
Sito J
aol ae (BSL) . FM h sf
epee fe
f. HALT 3
17. It is probably easiest to describe the shift and add method for a
3TM, multiplicand in track 1, multiplier in track 2, product in
track 3. If the least significant bit of the multiplier is 0, just
shift the product; if it is 1, shift the product and to it add the
multiplicand in track 1. Delete the least significant bit on track
2 and, if there are more nonblank multiplier cells, repeat. To
avoid the necessity of pushing everything down the TAPE at
each step, either allow for the product as many cells as there
are bits in the two inputs combined or process from right to left,
with the least significant bits at the beginning of the TAPE.
18. Use the Euclidean algorithm.
States | - 7: Check the input, rejecting strings not of the form
ba™baa™. If the dividend is 0, reach state 3, erase the divisor —
leaving the 4's to represent the answer 0, and go to HALT. If
the dividend is not 0 verify the form and reach state 8, where
processing begins from ibe eat end of the input.

<s ane es (oar) if


S hoy, 1S Ae
ae ae ji hn, aa ta ratte eee Se
alee )
ie ro acre 1 ‘i
ee EPR Meade pews nS A) Em
ee ee ee ame=
aN — —-
Ct 9 2,‘C20,
States 8 - 11: New subtraction: Change the last zof the divisor
to A and match it by changing the first zof the dividend to *.
States 12 - 14: Find the character in the divisor left of the
leftmost A.
States 14, 16, 17, 18, 12: Subtraction continues: Pair an 2 from
187

Chapter Twenty Five

the divisor with one from the dividend (changing both to 4).
States 14,15, 8: Subtraction complete: Restore the divisor to
lower case and begin a new subtraction.
States 11,26: The dividend has been entirely marked and a new
subtraction is just beginning; i.e. the remainder is zero.
States 18, 19: The dividend has been entirely marked and a
subtraction is in process. There is a nonzero remainder.
ae [9,02 ) abe
CALA OIL), < (x4 4°) Fr (S52)

a8)j aus)

ao OranG K-42 = HORE


(4,4, 2 = (2,0,4')°
Tan (0,8 os

/ es ee,
Gaz), ono aEeaeey, pula 2a¥

States 19-20-21-22- ae 24: Change the remainder portion back


to 2 (The rightmost* is the last character to change.) Delete all
A's left of that; each * remaining counts one whole subtraction.
Change these to 2 Change the first zof the remainder to Sto
separate it from the quotient; leave the rest of the remainder;
change the 4 following to #2 toreplace the other #changed.
(4; L)- (#,4,2 Mey [$,2, ae Cees
pained) nee) ‘(3,b,8) | £(b,0,%) a
(20
g A
AS
Zs Z

ieAo) “(any=,2)
(GULETE 2)
States 26-27-28: Noremainder. Delete all A's and change * to 2
(4.4.4 Me- (4,2,
aig BR) ISSEY
EN peeps asi

(4,4,51¢ i
ae CELETEL
Dldlenz. erase (he testo the string,
ee
188

Chapter Twenty Five

19. To determine whether a number is prime, we need to know if the number has any divisors.
So we would like to test integers 2 through the object number. We can use repeated
‘subtraction’ to simulate the division process. When subtracting the a’s on the right from the
a’s on the left we change them into b’s so that we can restore the original a" if desired. Next
we need to decide whether
(i) the process is incomplete, subtract again.
or (11) the process is complete and the number is a divisor - the number is not a prime.
or (iii) the “division/subtraction’ of the second number is complete but not a divisor of the
first. In this case, we need to decide whether there is still a possible divisor or not,
in the latter case the first number is a prime.

20. Consider the following TM.

(A; a,b,#; R)

START
It may print out any language, but we cannot know which.
i =a @unr Cwmet)
Pee,
o%

Satagfie = oe ane Score Gy ta BO ae


os OGm. pe Oni we * :
(i) 3 (eSn6e >)Sopa, (oes cok . ;
at (hs, C4 oped & Qaepe= aah Gs Geng Os @enp
Os cen
— by
epee) @ woe omee 4% paged mein » eeapric: hu wats 4
me 0 te mm ~o mee trie ages Conh a6 < abe
~ ao "es toe i Oe Sad > OP

fap @atiae 1's~ 7


ISBN 0-471-17305-3
ae ae | | 90000>

Joun Witey & Sons, Inc. ae :


New Yoru - CHicnester - BRISBANE a;
Toronto - SINGAPORE - WEINHEIM . ; 9780471173052

You might also like