Está en la página 1de 12
b d An Introductionto FormalLanguages andAutomata Third Edition PeterLinz Universityof Californiaat Davis
b d
An Introductionto
FormalLanguages
andAutomata
Third Edition
PeterLinz
Universityof Californiaat Davis
dtry'l-,tlti,tFI,
.rf;ti.
6l;l
etig*
o
t'
hgfryfl6a
filru;;;:
';;'ut:
\ n qf
I"
A\ ,r'f7 lA ,obi
IONES AND BARTLETT P,UBLISHERS
Stdlnry, Massnclrrsrtr
.BOSTON
TORONT'O
LONDON
SINGAPORE
Workl Headquerters IonesandBartlettPuhlishers Jonesand Bartlett Publishers Jonesand BarJlettPublishers Canada
Workl Headquerters
IonesandBartlettPuhlishers
Jonesand Bartlett Publishers Jonesand BarJlettPublishers
Canada
40TallPineDrive
2406NikannaRoad
Sudbury,MA 01776
978-443-5000
Mississauga,ON L5C 2W6
CANADA
International
Barb House,Barb Mews
LondonW6 7PA
UK
info@jbpub.corn
www.jbpub.com
CopyrightO 2001by JonesandBartlettPublishers,Inc.
All rightsreserved.No partof the materialprotectedby thiscopyrightnoticemay be reproducedor
utilizedin anyfonn, elcctronicor mechanical,includingphotocopying,recording,or anyinfotmation
storageor retrievalsy$tem,without writtenpermissionf'romthecopyrightowner.
G'
A
Library
of Congress Cataloging-in-Puhtication Data
Linz, Peter.
An introductionto formallanguagesandautomata/ PeterLinz'--3'dcd
p. cm.
Includesbihliographicalref'erencesandindex.
rsBN0-7637-1422-4
.L 5 4
Loo I
l.
Formallanguages. 2. Machinetheory.
l. Title.
QA267.3 .Ls62000
5| 1.3--dc2l
00-062546
Chief ExecutiveOfficer: ClaytonJones
Chief OperatingOfficer: Don W. Jones,Jr.
ExecutiveVicc PresidentandPublisher:Tom Manning
V.P., ManagingEditor: JudithH. Hauck
V.P ColleseEditorial Director: Brian L. McKean
V.P;,Dcsigir'and"Prodgction: \ Anne $pencer
V.P., Salcs anit*ffrarket+rg-i . FauI Shefiardson
;
#F*F*.,.
V.P.,Manufaeturingjandilnhrr'trrry dpntrol : ThereseBriiucr
SeniorAgquisitionsEditor; Michacl $tranz
f)evelopment and Product Managcr: f,lny Rose
MarkctingDirector:Jennifer.Iacobson
Production CoordinationI Tri{ litrm
-Pt'oj ect Managcment
CoverDesign;Night & Day Design
Composition:NortheastCompositors
PrintingandBinding: CourierWestford
Coverprinting:JohnPow Cotnpany,Inc.
Covel Imasc O Jim Wehtie
This bookwastypesetin Texturcs2.I on a MacintoshG4. Thefbnt familiesusedwereComputer
Modern,Optima,andF'utura.The first printing wasprintedon 50 lb. Decision94 Opaque.
Printedin theUnitedStatesof Arnerica
-'_
I
04030201
lo987654321
I
lch- ,r./1,il.t!\
L
4

26+,3

his book is designed for an introductory course orr forrnir,llarrguages, autornatir, txlmputability, and rclated matters.
his book is designed for an introductory course orr forrnir,llarrguages,
autornatir, txlmputability, and rclated matters.
These topics form
a major part of whnt is known as tht: theory of cornputation. A
course on this strbitx:t rnatter is now stir,nda,rdin the comprrter sci-
ence curriculurn ancl is oftrlrr ta,ught fairly early irr the prograrn. Hence,
the Jrrospective audience for this book consists prirnrr,rily of sophomores and
juniors rnirjrlring in computer scicntxlor computer errgirrwring.
Prerequisites for the material in this book are a knowledge of sorne
higher-level prograrnrning la,nguage (cornmonly C, C++,
or .Iava) and fa-
trrilinritv with ihe furrdarn<lnta,lsof data structures and algoriihms. A colrr$e
in discretc mathematics that irx:hrclesset theory, furrctions,relations, logic,
and elernerrtsof mathematical reasorringis essential. Such a corlrseis part
of the standard introductory computer sciencecurriculum.
The study of the theory of cornputa.tionhas severalpurposc$,most im-
prortantly (1) to fa,miliarizestuderrtswith the fbundations and principles of
computer sciettce,(2) to teach tnaterial that is useful in subsequerrtcolrrres!
rrnd (3) to strengtlrcrr utudents' ability tu t:ilrry out formal and rigorous
rrrirthematical argurnerrts. The presentatiorr I ha,vechosen for this text fa-
III
lv FRHr-AciE vors the first two purpose$r although I would rr.rguethat it a,lsoserves the thircl.
lv
FRHr-AciE
vors the first two purpose$r although I would rr.rguethat it a,lsoserves the
thircl. To prt:sentideasclenrly arrd1,ogive strrdcrrtsinsight into the material,
tlte text stressesintuitive rnotivation and ilhrstration of idcir.sthrough ex*
a,m1llcs.When there is ir choice, I prefcr arguments thtr,t a,reeasily grer,sptxl
to thosr.'tlnt are concisr,land elegant brrt rlifficult in concellt. I state clefini-
tiorrs ancl theorems llrecisely and givt: the tnotiva,tion ftlr proofs, brrt tlf'tt:rr
k:aveout the rorrtirreand tediorrsrlctails. I believetlrrr.tthis is desirnblc for
peclagogir:nlrcasdhs, Many proofs are unexc:itirrgapplications of irrduction
or contra,clit:tiotr,with diff'ererrt:esthat are sptx:ificto particuLrr llrobletns.
Presenting $rrdr arguments in full
detail is not orrly ullllecessary,lrtrt inter-
feres with the flow of the storv. Therefore, quite a few of the proofs are
sketchy irrrclsomeone wlxl irrsists on complerttlrressInay consitlclr tltern lack-
ing in cletrr.il.I do not seq:this as a clrawback. Mathematica,l skills are uot
the byproduct of reading sorrreorleelse'sargutttents,but comc frorn think-
ing atrout the essenrxlof a problem, disrxlvtlrirrg idea-ssrritatllc to make the
poirrt, thel carrying tltetn out in prtruistldetail. The kr,tter skill certainly
has to be lea,rnerd,arrd I lhink th.r,t the proof sketches irr this text providc
very appropriir,tcstartiug points fbr such a practitx:.
StudentS irr courputer sclit1rrcesornetitnes vi(lw a course in the theory of
computation
aa urlnecessarily abstract and of little
practical con$(xpelrce.
'Ib
convinr:c thetn otherwi$e, t)nc treeclsto appeir.ltcl their specific irrterests
and strengths, suclt a,stena,t:ity and inventivttntlss itt clealing with
hard-to-
solver llroblettts. Beca,userof tltis, tny a,pprtlitt:hempha,sizeslea.rnirrgthrough
probletn solving.
By a problem-solvitrgapproa,ch,I rrteattthat students learn the material
prirnarily througlt problem-type illustrative examplcs that show the moti-
vation bohirrd the concepts, a^swell as their conncction to the theorcrns attd
clefinitiotrs.At the sa,metirne, the examplesrrriryinvolve a nontrivial aspect,
for whir:hstudents must dist:ovc:ra solution. In such an approach,htlrnework
exrrrc:isescontribute to ir,rrrajor part of the leartting procefJs.The exercises
:rt the end of eachsectiorrare designedto illutrftrate and ilhrstrate the ma-
tr:rial and call orr sttrdents' problem-solving ability a,tvtr,riotrslevels. Some
of the exerci$csare fairly sirnple,pickirrg up where the discussiotrin the text
Ieaves ofl and asking students to carry ou for antlther step or two. Other
extrrcisesare very difficult, challenging evtrrrthe best ntinds. A good rnix
of such exercisest:ilrrbe a very eff'ectivt:teaching tool. Ttr help instructors,
I have provitled separately an instructor's guide thrr.toutlines the sohrtitlrrs
of the exercise$irrrdsuggeststheir pcdagogicalvalue. Students need not trrr
asked to solvc all problems bqt should be assigned those which support tlte
goals of the course and the viewpoint of the instnrt:tor. Computer sr:ience
currir:ulir tliffer from institrrtiorr to iilstitutiorr; while a few emphasize the
theoretir:nl side, others are alrnost entirely orientt:d toward practiclnl appli-
cation. I believethat this tt:xt can serveeitlNlr of these extremes, llrclvided
that the exercisesa,restllected carefully witli the students' btr,c:kgroundatld
intertlsts in mind. At ttle same time, the irrstructor needs to irrform tlle
PRnrecn students aborrt the level of abstraction that is expected of tlxrm. This is say
PRnrecn
students aborrt the level of abstraction that is expected of tlxrm. This is
say "llrove that"
particularly
tnre of the proof-orietrttxl exercises. When I
"show
or
that," I hrr,vein mind that the student should think about how a,
proof rnight be cclnstrur:ted ancl then produr:e a,clear argurnent. How fbr-
rrtal srrcha,proof should bc needsto be deterrnined by the instructor,
ancl
stutltlnts should be
Tltc content of
given guitlrllines on this early irr the txlrrse.
the text, is allllropriate for a one-sernestcrtxrurse.Most
of the nraterial can be covered, although some choice of errrpha.siswill have
to be rnirde. In my classes,I gencrirlly gloss over proofs, skilr4rv as they are
itr tlte tcxt. I usually givejust enoughcoverageto make the rcsult plausible,
asking strrdentsto read the rest orr their own. Overall, though, little can
be skippexlentirely witltout potential difficulties later on. A few uections,
which are rnrlrked with an asterisk, c:rr,nbe omitted without
loss to later
material. Most of tht: m:r,teria,l,however. is esscrrtial ancl must be covered.
The first edition of this book wrr,upublishedin 1990,thc:stxxrnda,ppeared
in 1906. The need for yet another cdition is gratifying and irrtlic;atesthat
tny a1l1lrorr,ch,via languagesrathcr than computations, is still viable. The
charrgcsftrr the second edition werclt)volutionary rather than rcvolrrtionary
and addressedthe inevitable itrirct:rrra,c:iesand obscuritiesof thtl Iirst edition.
It seertrs,however,that the secondr:dition had reacheda point of strrbility
that requiresf'ewchanges,so thc tlrlk of the third editiorr is idcntical to the
previous one. The major new featurtl of the third edition is the irrc:hrsionof
a set of solved exercises.
Initially,
I felt that giving solutions to exerciseswas undesirable hecause
it lirrritcd the number of problerrts thir.t r:a,nbe a,ssignedfor hourework. How-
ever, over tlre years I have received so rrrany requests for assistance from
students evt:rywherethat I concluded that it is time to relent. In this edi-
tion I havc irrcluded solutions to a srnall rrumber of exercises. I have also
added solrrorrew exercisesto keep frorn rtxhrcing the unsolved problems too
much. Irr strlec:tingexercises for solutiorr, I have favored those that have
signiflcant instructioner,lver,lues.For this reasorr,I givc not onlv the answers,
brrt show the reasonirrgthat is the ba,sisfor the firml result. Merny exercises
have thtl ser,metheme; often I choose a rupresentative case to solve, hoping
that a studerrt who can follow the reasorrirrgwill be able to transfer it to a
set of similar instances. I bclicrvethat soluiions to a carcfirlly selected set
ttf exercisescan help studerrts irrr:rea"retheir problern-solvirrg skills and still
lcave instructors
whir:h rr,solution
a good set of unuolved exercises. In the text, {lxercisesfor
or a hint is g-ivcrr rr,rqridentified with {ffi.
Also in responseto suggcstitlns,I have identified sonre of therharder
exercist:s. This is not always easv, sirrt:ethe exercises span a spectrrrm of
diffic;ulty and because a problen that seemseasy to one student rnay givr:
considerabletrouble to another. But thcre are some exercisesthat havcl
posed a challcnge fbr a majority of my studcnts. These are rnarked witlr
a single star (*). There are also a few exercisosthat are different from
most in that they have rro r:lear-cut answer. They rnay crrll f'or upeculation,
vt PRnrncp suggest additional reading, or require some computer programming. While they,are not suitable for
vt
PRnrncp
suggest additional reading, or require some computer programming. While
they,are not suitable for routine homework assignment, they can serve &s
entry points for furtlter study. Such exercisesare marked with a double star
(**
)
.
Over the last ten years I have received helpful suggestions from numer-
ous reviewers, instructors, and students. While there are too many individ-
uals to mention by name, I am grateful to all of them. Their feedback has
been in'aluable
in my attempts to improve the text.
Peter Linz
Chapter 1 fntroduction to the Theory of Computation 1.1 Matlrenratical Prelirrrirrrlricuar,ndNotation Sets 3
Chapter
1
fntroduction
to the
Theory
of Computation
1.1
Matlrenratical Prelirrrirrrlricuar,ndNotation
Sets 3
Functions and Relations 5
Craphs and l}'ees 7
Proof Techniques I
3
1.2
Three Basic Concepts
Lirrrgrrir,ges 15
Grarnrnilrs 19
Automala 25
15
+1.3
SomeApplications
29
Chapter 2
Finite
Autornata
35
2,1
I)eterrrrinistit:Finite Accepters
36
I)ctc:rrnirristicAcceptersand'IIrrnsitiorr Grir,phs
36
Languir,gcsand Dfa,s 38
R.t:gulil,rL:lngrrages 42
2.2
Nondeterrriinistit:FiniteAccepters 47
Definilion of a Nonrleterministic Accepler 48
Whv Notxlctt:rrninism'1 52
vll
vlll CoNrnNrs 2.3 Equivalenceof Deterministic and Notrdeterministic Finite Accepters 55 +2.4 Reduction of the Number
vlll
CoNrnNrs
2.3 Equivalenceof Deterministic and Notrdeterministic Finite
Accepters
55
+2.4
Reduction of the Number of Statesin Finite Automata
62
Chapter
3
Regular
Languages
and
Regular
Grammars
fl
3.1
Regular Expressions 7I
Forma,l Delinition of a Regular Expression 72
Languages Associated with Regular Expressions 73
3.2
Connection Between Regular Expressions and Regular
Languages 78
Regular Expressions Denote Regular Languages 78
Regula,r Expressions for
Regular Languages
81
Regular Expressions for Describing Simple Patterns 85
3.3
Regular Gra.trrnars
89
Right- anrl Left-Linear
Grammars
89
Right-Linear Grammars Generate Regular Languages 91
Right-Linear Grammars for Regular Languages 93
Equivalence Bctween Regular Languages and Regular
Gra,mma,rs 95
Chapter
4
Properties of Regular Languages 99
4.1
Closure Propertitrs of Regular Languages 100
Closure under Simple Set Operations 100
Closure under Otlter Operations 103
4.2
Elementary Qrrestions about Regular Languages
111
4.3
Identifving Nonregular Languages
114
llsirrg the PigeonholePrinciple 114
A Pumping Lemma 115
Chapter
5
Context-Free
Languages
L25
5.1
Corrtext-FreeGrammars
126
Exarrrples of Context-Flee Languages 127
Leftntost and Rightmost Dt'rivations 129
Derivation Tl'ees 130
R.elation Between Sentential Fttrms and Derivation
'llees
13?
5.2
Parsing and Ambiguity 136
Parsing and Mcnbership 136
Anlbiguity in Grarnrnarsand Latrguages 141
5.3
Context-Ftcc Gramrnars and Programmirtg
Ltr,rrgrrages 146
CoNrEr-rts ix Chapter 6 Simplification of Context-Flee Grammars 149 6.1 Methods for TfansforrrringGrammars 150 A
CoNrEr-rts
ix
Chapter
6
Simplification
of Context-Flee
Grammars
149
6.1
Methods for TfansforrrringGrammars 150
A Useful Substitution Rule
150
6.2
+6.3
Removing UselessProductions 15?
Removing.\-Productions 156
Removing Unit-Productiorrs 158
Two Important Normal Forrns 165
Chomsky Normal Form 165
Greibach Normtr,l Form 168
A Me:mbershipAlgorithm for Context--F]'eeGrarnrnrr,rs 1,72
Chapter
7
Pushdown
Automata
175
7.7
Nondeterrnirfstic Pushdown Automata 176
Definition of a Pushdown Arrtomaton
tTti
A LangrrageAccepted by a PushdowrrAutomaton I79
7.2
Pushdown Automata and Context-Free Larrguagcs 184
Pushdown Autorrrata fbr Context-Flee Languages 184
Corrtcxt-Floe Grammars for Pushdown Autorrrata 189
7.3
DerterrrinisticPushdown Autornataand Deterrrfnistir: Context-
Fr{lc Lirnglrri;r,ges195
*7.4
Gramma,rsfbr Deterministic Corrtext-F}ct:Langua,ges 200
Chapter
8
Properties
of Context-Flee
Languages
205
8.1
Two Pumping Lemmas 206
A Purnpirrg Lcrnrna fbr Context-Flee Languages 206
A Purnping Letrrnil firr Linear La,ngua,ges 210
8.2
Closure Propcrtien and Decision Algorithrns for Context-
Free Languages 213
Closure of Context-Free LangJuages ?13
Some Decidable Properties of Contcxt-Fre,'e
Languages
218
Chapter
9
Turing Machines
221
9.1
The Standard T\rring Machine 222
Definition of a Thring Machine 222
T\rring Machines as Language Accepters 229
Tlrring Ma,chinesas Tlansducers 232
9.2
Combining Tlrring Machines for Cornplicated Tasks 238
9.3
T\rring's Thesis 244
(,'onrnrlrs Chapter 10 Other Models of Turing Machines 249 10.1 Mirxlr Virriatiotrs on the T\rring
(,'onrnrlrs
Chapter
10
Other Models of Turing Machines
249
10.1
Mirxlr Virriatiotrs on the T\rring Ma,t:hint:Therne 25t)
Eqrrivalcrrt:tlclf Classesof Autonrata, 250
Ttrrirrg Machineswith a,Sta,y-Option 251
Thring Machines with Semi-Infinitc Tape 253
The Off-Line Tttrirrg Mat:hine 255
10.2
'I\rring Ma,chineswith Morc Cotttplex Storage 258
Mullitape Ttrring Ma,chiners 258
Mttltidimensional T[rring Mtr.chirrt:s 261
10.3
NorrtletertninisticT\rring Ma,chines 263
10.4
A lJrriversalI\rring Machine 266
10.5
Liricar Bouttded Autotnata 270
Chapter
Ll.
A Hierarchy
of Formal
Languages
and Autornata
278
11.1
Recursiveand ReclrrsivelyEuurnerableLanguages
276
LanguagesThat Art: Not R,tx:ursivelyEnumera,ble 278
A Language That Is Not R,t:cursivelyEnumerable
279
A Language That
Is Rer:rrrsivr:lyErlrrrterable But Not
Recursive
28.l
11.2
Uurestricted Grarnmars
283
11,3
Context-Sensitivc (]rarnrna,rs arrd Lirnguages 289
Conterxt-Srlnsitivc Languages and Litrear Bounded
Aulomata
29t)
Relation Betweeu Recursiveand Ctlrrtt:xt-Setuitive
Languages
2gz
11.4
I'he Chomskv Hierarchv
29Ir
Chapter
12
Limits
of Algorithrnic
Cornputatiorr
299
12.1
SomeProbletns That (ltr,rrnot Bc:Solved By l\rring
Machines
300
The T\ring Machine llalting Problem 301
H.etlucitrgOne Undecidable Problem to Another 304
12.2
Uritlt:c:itlrrbleI'robletns for Recursivelv llnrtmertr,ltlrr
Languages 308
12.3
Tlte I'osLCorrespondencePtoblem Sl2
12.4
[JndccidableProblems for Context-Free Lir.nguages 318
Cot-{reNrs xi Chapter L3 Other Models of Computation 323 13.1 RecursiveFunctions 325 P-rimitiveRecursiveF\nctions
Cot-{reNrs
xi
Chapter L3
Other Models of Computation
323
13.1
RecursiveFunctions 325
P-rimitiveRecursiveF\nctions 326
Ackermann'sF\rnction 330
13.2
PostSystern$ 334
13.3
R.ewritingSystems 337
Markov Algorithms
L-Systems 340
339
Chapter 14
An Introduction
to Computational
Complexity
343
14.1
Efficiencyof Computation 344
14.2
Ttrring Machinesand Complexity 346
14.3
LanguageFamiliesand Complexity Classes 350
I4.4
The Complexity ClassesP and NP
353
Answers to Selected Exercises
357
References
405
Index
4OT