Compiler Design - Notes 1
Compiler Design - Notes 1
Modern Compiler
Implementation
in C
ANDREW W. APPEL
Princeton University
http://www.cambridge.org
A catalogue record for this book is available from the British Library
Contents
Preface ix
1 Introduction 3
1.1 Modules and interfaces 4
1.2 Tools and software 5
1.3 Data structures for tree languages 7
2 Lexical Analysis 16
2.1 Lexical tokens 17
2.2 Regular expressions 18
2.3 Finite automata 21
2.4 Nondeterministic finite automata 24
2.5 Lex: a lexical analyzer generator 30
3 Parsing 39
3.1 Context-free grammars 41
3.2 Predictive parsing 46
3.3 LR parsing 56
3.4 Using parser generators 69
3.5 Error recovery 76
4 Abstract Syntax 88
4.1 Semantic actions 88
4.2 Abstract parse trees 92
CONTENTS
vi
CONTENTS
vii
CONTENTS
Bibliography 528
Index 537
viii
Preface
Over the past decade, there have been several shifts in the way compilers are
built. New kinds of programming languages are being used: object-oriented
languages with dynamic methods, functional languages with nested scope
and first-class function closures; and many of these languages require garbage
collection. New machines have large register sets and a high penalty for mem-
ory access, and can often run much faster with compiler assistance in schedul-
ing instructions and managing instructions and data for cache locality.
This book is intended as a textbook for a one- or two-semester course
in compilers. Students will see the theory behind different components of a
compiler, the programming techniques used to put the theory into practice,
and the interfaces used to modularize the compiler. To make the interfaces
and programming examples clear and concrete, I have written them in the C
programming language. Other editions of this book are available that use the
Java and ML languages.
http://www.cs.princeton.edu/˜appel/modern/c
ix
PREFACE
Course sequence. The figure shows how the chapters depend on each other.
Lexical Abstract Semantic
2. 3. Parsing 4. 5.
Quarter
Analysis Syntax Analysis
Semester
6. 7. 8.
Records Intermediate Code and Traces
Instruction Putting it
1. Introduction 9. 12.
Selection All Together
Quarter
Liveness Register
10. 11.
Analysis Allocation
Static Single-
Dataflow Loop
17. 18. 19. Assignment
Analysis Optimizations
Form
Semester
Functional Polymorphic Pipelining,
15. 16. 20.
Languages Types Scheduling
• A one-semester course could cover all of Part I (Chapters 1–12), with students
implementing the project compiler (perhaps working in groups); in addition,
lectures could cover selected topics from Part II.
• An advanced or graduate course could cover Part II, as well as additional
topics from the current literature. Many of the Part II chapters can stand inde-
pendently from Part I, so that an advanced course could be taught to students
who have used a different book for their first course.
• In a two-quarter sequence, the first quarter could cover Chapters 1–8, and the
second quarter could cover Chapters 9–12 and some chapters from Part II.