Second Edition
Alfred V. Abo
Columbia University
Monica S. Lam
Stanford
University
Ravi Sethi
Avaya
Jeffrey D. Ullman
Stanford
University
Boston San Francisco
New York
London Toronto Sydney Tokyo Singapore Madrid
Mexico City Munich Paris Cape Town Hong Kong Montreal
Editor
Publisher
Editor
Executive
Acquisitions
Project Editor
Associate Managing Editor
Cover Designer
Digital Assets Manager
Media Producer
Senior Marketing
Marketing
Senior Author Support!
Manager
Assistant
Greg Tobin
Michael Hirsch
Matt Goldstein
Katherine Harutunian
Jeffrey Holcomb
Joyce Cosentino
Marianne Groth
Bethany Tidd
Michelle Brown
Sarah Milmore
Wells
Technology
Specialist
Buyer
Senior Manufacturing
Cover Image
Joe Vetere
Carol Melville
Scott Ullman of Strange Tonic Productions
(www.strangetonic.com)
used by manufacturers
and sellers
to distinguish
their
are claimed as trademarks. Where those designations
appear in this
Many of the designations
products
book, and
have been printed
Addison-Wesley
in initial
was aware of a trademark
caps or all caps.
claim, the designations
This interior of this book was composed in LATEX.
Library of Congress Cataloging-in-Publication
Data
Compilers: principles,
techniques,
and tools / Alfred V. Aho ... ret a1.]. -- 2nd ed.
p.cm.
Rev. ed. of: Compilers,
principles,
techniques,
and tools / Alfred V. Aho, Ravi
Sethi, Jeffrey D. Ullman. 1986.
ISBN 0-321-48681-1
1. Compilers
Compilers,
principles,
(alk. paper)
(Computer programs) 1. Aho, Alfred V. II. Aho, Alfred V.
techniques,
and tools.
QA76.76.C65A372007
005.4'53--dc22
2006024333
without the prior
may be reproduced,
Copyright © 2007 Pearson Education,
publication
any form or by any means, electronic,
written permission
otherwise,
United States
material
Inc., Rights and Contracts Department,
MA 02116, fax your request to 617-848-7047,
http://www.
of America. For information
pearsoned.comllegallper
missions.
or e-mail at
htm.
for use of
to Pearson Education,
in this work, please submit a written request
Suite 300, Boston,
Street,
on obtaining
75 Arlington
Inc. All rights reserved.
No part of this
stored in a retrieval
mechanical,
system, or transmitted,
recording,
photocopying,
in
or
Printed in the
of the publisher.
permission
2 3 4 5 6 7 8 9
lO-CW -10 09 08 07 06
Preface
1986 edition
significantly.
problems. Computer
of this book, the world of compiler
to present
of resources
evolved
offer a variety
languages have
Programming
new
of
design
designer must
architectures
take advantage. Perhaps
has found use outside
of code optimization
most interestingly,
that find bugs in software, and most importa
of the "front-end"
code. And much
parsers,
and syntax-directed
compilers.
find
ntly,
-
technology
translators
- are
tools
compiler
In the time since the
has changed
compilation
which the
the venerable
It is now used in
security
grammars, regular
still
expressions,
holes in existing
technology
in wide use.
Thus, our philosophy
that few readers
We recognize
major programming
ated with a compiler
design
and software
most commonly
the source
language.
can be applied
development
encountered
language or
target
from previous
in designing
machine.
versions
will build, or even maintain, a compiler for
Yet the
of the book has not
a
theory, and
to a wide range of problems
algorithms
associ
in software
changed.
models,
. We therefore
a language
problems
that are
processor,
emphasize
regardless of
Use of the Book
in this book. It is common to cover the first half in
an undergraduate
or even two semesters
to cover all or most of
the
half of the book - stressing
or mezzanine
level.
- in
code optimization
Here is an outline
of the
1 contains
and the second
two quarters
at the graduate
It takes at least
material
course
a second course
chapters:
Chapter
issues
Chapter
tant concept
appears
Chapter
scanner-generator
sorts.
in the appendix.
3 covers lexical
motivational
architecture
a miniature
analysis, regular
tools. This material
2 develops
in computer
v
material
and also presents
and programming-la
compiler
nguage
and introduces
later
chapters.
some background
principles.
many of the impor
The compiler itself
s, which are then developed in
expressions,
is fundamental
finite-state
and
of all
to text-processing
machines,
vi
PREFACE
the major parsing
(LR and
, LL)
methods, top-down
s).
5 introduces
ideas in syntax-directed
its variant
definitions
and
(recursive-descent
5 and shows how to use it to generate
programming
language.
environment
s, especially
management
of the run-time
of code from expressions
and basic blocks,
and register-allocation
It covers
generation.
construction
of basic blocks,
theory
7 covers
4 covers
of Chapter
run-time
6 takes the
code for a typical
collection.
8 is on object-code
the principal
translations.
Chapter
and bottom-up
Chapter
syntax-directed
Chapter
intermediate
Chapter
stack and garbage
Chapter
generation
techniques.
9 introduces
Chapter
frameworks,
data-flow
10 covers
Chapter
of parallelism
traction
processors
on single
11 talks
Chapter
the emphasis
multidimensional
arrays.
Chapter
and data-flow analysis
that reach a given point in the code.
the technology
that can do more
is on numeric
and iterative algorithms for
including
of code optimization,
these frameworks.
is on the ex
solving
The emphasis
of instructions
optimization.
flow graphs,
from small sequences
and scheduling
them
instruction-level
about larger-scale parallelism
Here,
codes that have many tight
and exploitation.
loops that range over
than one thing
at once.
detection
12 is on interprocedural
that takes into account
pointer
the sequence
analysis, aliasing,
of procedure
calls
analysis.
It covers
Courses
work in small teams to create
own design. The
from material
in this book have been taught
course
a senior/first-y
using material fr
offered
has been regularly
is a semester-long
project
and translators
chapters. A highlight
ear graduate
of this course
on program
at Columbia
, Harvard,
and Stanford. At Columbia,
ming languages
the first eight
in which students
guage of their
domains
application
puter graphics,
compiler-comp
directed
compilers.
through
machines
12, emphasizing
including
translation
A follow-on
gaming,
onent generators
techniques
including
graduate
matrix
a little
and implement
lan
have covered
music synthesis, com
student-created
languages
computation,
quantum
Students
and many other areas.
operations
such as ANTLR, Lex, and Yacc and the syntax
discussed
in chapters
9
has focused on material
optimization
code generation and
course
processors
in Chapters
diverse
use
om
two and five to build their
for contemporary
network
1 through
from Chapter
and multiprocessor
the mate
code
there is an introduction
to global
architectures.
roughly
course
covers
At Stanford, a one-quarter
introductory
rial in Chapters
optimization
through
ter 7. Students
implementing data-flow
12, plus the more advanced
8, although
9. The second
use a locally
analysis
material
developed, Java-based system
algorithms.
on garbage
compiler
course
covers
Chapters
collection
from Chap
Joeq for
called
9
PREFACE
Prerequisites
vii
The reader
at least
discrete
is useful.
a second
mathematics.
course
should
possess
some "computer-science
sophistication
," including
on programming,
and courses
different
in data structures and
programming
languages
Knowledge
of several
Exercises
The book contains
indicate
hardest exercises
exercises
have a double
extensive
harder
or parts of exercises
exclamation
with an exclamation
point.
point. The
exercises,
with some
for almost
every section.
We
Gradiance. On-Line Homeworks
by Gradiance
Corp. Instructors may
to their class,
is an accompanying
set of on-line
is that there
developed
the new edition
using a technology
these homeworks
in an "omnibus
class"
A feature of
homeworks
assign
enroll
(without
questio
are given specific
instructor
A subscription
ns, but your solutions
or feedback
permits, you are allowed
advice
as a tutorial
look like ordinary
questions
or students
not enrolled
in a class
may
to do the homeworks
If you make an incorrect
choice
you
If your
again,
is offered with all new copies
to help you correct
to try
service
your solution.
you get a perfect score.
until
of this
an instructor-created
class). Gradiance
that allows them
to the Gradiance
are sampled.
text sold in North America.
web site www . aw . com/gradiance
or send
email tocomputing@aw. com.
For more information,
visit
the Addison-Wesley
Support on the Wo rld Wide We b
The book's
home page is
dragonbook. stanford. edu
Here, you will find errata
to make available
teach them, including
descriptions
of importa
as we learn of them, and backup
the notes for each offering of compiler-related
as we
materia
ls. We hope
courses
We also plan to post
homeworks,
nt compilers
solutions,
written
and exams.
by their
implemen
ters.
Acknowledgements
Cover art is by S.
Jon Bentley
D. Ullman
comments
draft of this book. Helpful comments
of Strange Tonic Productions.
of an
from:
on a number of chapters
received
and errata were
gave us extensive
earlier
viii
PREFACE
Bianculli,
Peter Bosch, Marcio
Hazelwood, Gaurav Kc,
Domenico
Vibhav Garg, Kim
Krysta
gratefully
Svore,
acknowledged.
In addition,
Monica
errors
Remaining
would like
to thank her colleagues
are ours, of course.
on the SUIF com
Olivier
Tardieu,
and Jia Zeng. The help of all these people
is
Buss, Marc Eaddy,
Stephen
Edwards,
Wei Li, Mike Smith,
Art Stamness,
team for an 18-year lesson
piler
Saman Amarasinghe,
Diwan,
Avots,
Cheong,
Amer
Anwar Ghuloum, Mary Hall, John Hennessy,
David
Amy Lim, Benjamin
, Heine, Shih-Wei Liao,
Jennifer Anderson,
Livshits,
Carbin,
Michael
Michael
Gerald
Robert
French,
Martin, Dror
on compiling: Gerald Aigner, Dzintars
Maydan,
tin Rinard, Olatunji
Steven
Michael
Whaley,
Wilson,
Smith,
Robert
Todd Mowry, Brian Murphy,
Jeffrey Oplinger,
Ruwase, Constantine Sapuntzaki
Sathyanathan
Karen Pieper, Mar
,
s, Patrick
Christopher
Unkel,
John
Tjiang, Chau-Wen
Christopher
Wilson,
Tseng,
and Michael
Wolf.
NJ
A. V. A., Chatham
M. S. L., Menlo Park CA
R. S., Far Hills
J. D. U., Stanford
CA
June, 2006
NJ
Table of Contents
1 Introduction
. . . . . .
1 . 1 Language
1.2 The Structure
Processors
1.1.1 Exercises
for Section
1.1
of a Compiler
.
Analysis
Analysis
. . .
. .
1.2.1 Lexical
1.2.2 Syntax
1.2.3 Semantic Analysis
1.2.4 Intermediate
Code Generation
1 .2.5 Code Optimization
. . . . .
1.2.6 Code Generation
1.2.7 Symbol-T
1.2.8 The Grouping
1.2.9 Compiler-Construction Tools
able Management
. .
of Phases
into Passes
1
1
3
4
5
8
8
9
10
. . . . . . . . 10
11
11
. . . . 12
12
13
14
1.3 . . . . . . . 14
. . . . . 15
15
and Implementation
15
17
17
19
Architectures 21
22
23
. . . . . 25
25
26
28
31
31
33
. . . .
. . .
Distinction
Structure
. . . .
1.3 The Evolution
of Programming
.
Languages
1.3.1 The Move to Higher-level
Languages
1 .3.2 Impacts
. . . .
1.3.3 E xercises
on Compilers
for Section
1 .4 The Science
of Building
a Compiler
Design
in Compiler
1.4.1 Modeling
1.4.2 The Science
1.5 Applications
of Compiler
Technology
1.5.1 Implementation
1.5.2 Optimizations
for Computer
Computer
1.5.3 Design
1 .5.4 Program
1.5.5 Software
of High-Level
.
Translations
Productivity Tools
of New
1 .6 Programming
Language
Basics
of Code Optimization
. . . . . . . . .
. . . . . . . . . . . .
Programming
Architectures
Languages
and States
Scope and Block
.
1.6.1 The Static/Dynamic
1.6.2 Environments
1.6.3 Static
1.6.4 Explicit
1.6.5 Dynamic
1.6.6 Parameter
Control
Access
Scope. . . . . . . . .
Passing
Mechanisms
. . . .
ix