Extended edition
Torben Ægidius Mogensen
DEPARTMENT OF COMPUTER SCIENCE UNIVERSITY OF COPENHAGEN
Published through c
lulu.com.
2000 2008
Torben Ægidius Mogensen
torbenm@diku.dk
Department of Computer Science University of Copenhagen Universitetsparken 1 DK-2100 Copenhagen DENMARK
Book homepage:
http://www.diku.dk/∼torbenm/Basics
First published 2000 This edition: July 25, 2008
Contents
1 Introduction
1.1 1.2 1.3 1.4 1.5 1.6 1.7 1.8 What is a compiler? Interpreters . . . . . . . . . . . . . . . . . . . . . The phases of a compiler . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Why learn about compilers? . . . . . . . . . . . . . . . . . The structure of this book . . . . . . . . . . . . . . . . . . To the lecturer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Acknowledgements
1
1 2 3 4 5 6 7 8
Permission to use . . . . . . . . . . . . . . . . . . . . . . .
2 Lexical Analysis
2.1 2.2 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . Regular expressions . . . . . . . . . . . . . . . . . . . . . . 2.2.1 2.2.2 2.3 2.4 2.5 2.6 Shorthands Examples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
9
9 10 13 14 16 19 19 21 23 24 27 30 31 33 35 37 42 44 44
Nondeterministic nite automata . . . . . . . . . . . . . . Converting a regular expression to an NFA . . . . . . . . . 2.4.1 Optimisations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Deterministic nite automata 2.6.1 2.6.2
Converting an NFA to a DFA . . . . . . . . . . . . . . . . Solving set equations . . . . . . . . . . . . . . . . . The subset construction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
2.7 2.8
Size versus speed 2.8.1 2.8.2
Minimisation of DFAs
Example . . . . . . .