2. compiler construction manual.pdf

(2198 KB) Pobierz
Compiler Construction
A Practical Approach
F.J.F. Benders
J.W. Haaring
T.H. Janssen
D. Meert
A.C. van Oostenrijk
January 11, 2003
2
Abstract
Plenty of literature is available to learn about compiler construction,
but most of it is either too easy, covering only the very basics, or
too dicult and accessible only to academics. We nd that, most
notably, literature about code generation is lacking and it is in this
area that this book attempts to ll in the gaps.
In this book, we design a new language (Inger), and explain how
to write a compiler for it that compiles to Intel assembly language.
We discuss lexical analysis (scanning), LL(1) grammars, recursive
descent parsing, syntax error recovery, identication, type checking
and code generation using templates and give practical advice on
tackling each part of the compiler.
3
Acknowledgements
The authors would like to extend their gratitude to a number of
people who were invaluable in the conception and writing of this
book. The compiler construction project of which this book is the
result was started with the help of Frits Feldbrugge and Robert
Holwerda. The Inger language was named after Inger Vermeir (in
the good tradition of naming languages after people, like Ada). The
project team was coordinated by Marco Devillers, who proved to be
a valuable source of advice.
We would also like to thank Carola Doumen for her help in struc-
turing the project and coordinating presentations given about the
project. Cees Haaring helped us getting a number of copies of the
book printed.
Furthermore, we thank Mike Wertman for letting us study his source
of a Pascal compiler written in Java, and Hans Meijer of the Uni-
versity of Nijmegen for his invaluable compiler construction course.
Finally, we would like to thank the University of Arnhem and Ni-
jmegen for letting us use a project room and computer equipment
for as long as we wanted.
4
Contents
1
Introduction
9
1.1
Translation and Interpretation
. . . . . . . . . . . . . . . . . . .
9
1.2
Roadmap
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
10
1.3
A Sample Interpreter . . . . . . . . . . . . . . . . . . . . . . . . .
11
2
Compiler History
16
2.1
Procedural Programming
. . . . . . . . . . . . . . . . . . . . . .
16
2.2
Functional Programming . . . . . . . . . . . . . . . . . . . . . . .
17
2.3
Object Oriented Programming
. . . . . . . . . . . . . . . . . . .
17
2.4
Timeline . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
18
I
Inger
23
3
Language Specication
24
3.1
Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
24
3.2
Program Structure . . . . . . . . . . . . . . . . . . . . . . . . . .
24
3.3
Notation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
29
3.4
Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
33
3.4.1
bool
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
33
3.4.2
int . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
34
3.4.3
oat . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
35
3.4.4
char
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
36
3.4.5
untyped . . . . . . . . . . . . . . . . . . . . . . . . . . . .
36
3.5
Declarations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
37
3.6
Action . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
40
3.6.1
Simple Statements . . . . . . . . . . . . . . . . . . . . . .
40
3.6.2
Compound Statements . . . . . . . . . . . . . . . . . . . .
42
3.6.3
Repetitive Statements
. . . . . . . . . . . . . . . . . . . .
42
3.6.4
Conditional Statements
. . . . . . . . . . . . . . . . . . .
43
3.6.5
Flow Control Statements
. . . . . . . . . . . . . . . . . .
46
3.7
Array
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
50
3.8
Pointers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
50
3.9
Functions
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
52
3.10 Modules . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
55
3.11 Libraries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
56
3.12 Conclusion
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
57
5
Zgłoś jeśli naruszono regulamin