Title: Compiler
1Compiler
- Chang Chi-Chung
- 2008.02.26
2Textbook
- Compilers Principles, Techniques, and Tools,
2/E. - Alfred V. Aho, Columbia University
- Monica S. Lam, Stanford University
- Ravi Sethi, Avaya Labs
- Jeffrey D. Ullman, Stanford University
- Free books
- http//freecomputerbooks.com
Dragon
3Score
- Midterm Examination 20
- Final Examination 25
- Quizz(2) 30
- Project 10
- Homework 10
- Participation 5
4Objectives
- ??????????
- Know how to use compiler construction tools, such
as generators of scanners and parsers - Be able to define LL(1), LR(1), and LALR(1)
grammars - Be familiar with compiler analysis and
optimization techniques - ??????????
5History
- Early 1950s
- Mnemonic assembly languages.
- Macro instructions were added later.
- In the latter half of the 1950
- Fortran scientific computation.
- To take 18 man-years
- Cobol business data processing.
- Lisp symbolic computation.
6History - Grace Hooper
7(No Transcript)
8Programming Languages
???? none
???? machine independent
???? machine dependent
Chinese English German
????
????
C?C?Java
9Introduction to Compilers(1)
Compiler
Source Program
Target Program
- Source program must be equivalent to target
program. - Definitions
- Recognizer
- Translator
10Introduction to Compilers(2)
- Translator
- from one format to another
- query interpreter
- text formatter
- silicon compiler
- infix notation ? postfix notation
- 3 5 - 6 6 gt 3 5 6 6 -
- pretty printers
- Computational theory
- power of certain machines
- the set of languages that can be recognized by
this machine - Grammar
- definition of this machine
11Impacts on Compilers
- Programming languages
- Machine architecture
- Language theory
- Algorithms and data structures
- Software engineering
12Use the Compiler Techniques
- Programming Languages (C?C)
- Scripts (Javascript?bash)
- Editors (syntax highlighting)
- Pretty printers (e.g. Doxygen)
- Static checkers (e.g. Lint and Splint)
- Interpreters
- Text formatters (e.g. TeX and LaTeX)
- Silicon compilers (e.g. VHDL)
- Query interpreters/compilers (Databases)
13Compilers ???
- Compilation
- Translation of a program written in a source
language into a semantically equivalent program
written in a target language
14Interpreters ???
- Interpretation
- Performing the operations implied by the source
program
15Hybrid
Source Program
Translator
Virtual Machine
intermediate program
input
output
16Compiler vs. Interpreter
Compiler Interpreter
???? 1 More
?? ?? ??
17A Language-Processing System
source program
Preprocessor
Try for example gcc -v myprog.c
modified source program
Compiler
target assembly program
Assembler
relocatable machine code
library files relocatable object files
Linker/Loader
target machine code
18character stream
Phases of a Compiler
Lexical Analyzer
token stream
Syntax Analyzer
syntax tree
Semantic Analyzer
syntax tree
Symbol Table
Intermediate Code Generator
intermediate representation
Machine-Independent Code Optimizer
intermediate representation
Code Generator
target-machine code
Machine-Dependent Code Optimizer
target-machine code
target-machine code
19character stream
position initial rate 60
Lexical Analyzer
ltid,1gt ltgt ltid,2gt ltgt ltid,3gt ltgt lt60gt
Syntax Analyzer
Semantic Analyzer
20Intermediate Code Generator
t1 inttofloat(60) t2 id3 t1 t3 id2
t2 id1 t3
Machine-Independent Code Optimizer
t1 id3 60.0 id1 id2 t1
Code Generator
1 position
2 initial
3 rate
LDF R2, id3 MULF R2, R2, 60.0 LDF R1, id2 ADDF
R1, R1, R2 STF id1, R1
SYMBOL TABLE
21The Grouping of Phases
- Compiler front and back ends
- Front end analysis (machine independent)
- Back end synthesis (machine dependent)
- Compiler passes
- A collection of phases is done only once (single
pass) or multiple times (multi pass) - Single pass usually requires everything to be
defined before being used in source program - Multi pass compiler may have to keep entire
program representation in memory
22The Analysis-Synthesis Model of Compilation
- There are two parts to compilation
- Analysis determines the operations implied by the
source program which are recorded in a tree
structure - Synthesis takes the tree structure and translates
the operations therein into the target program
23Compiler-Construction Tools
- Software development tools are available to
implement one or more compiler phases - Scanner generators
- Parser generators
- Syntax-directed translation engines
- Automatic code generators
- Data-flow engines
24Programming Language Basic(1)
- Static vs. Dynamic
- Compile Time vs. Run Time
- Examples
- scope of declarations
- memory location of variables
- Environments and States
environment
state
locations (variables)
names
values
According to the scope rules of a language
25Programming Language Basic(2)
- Static Scope and Block Structure
- C/C uses
- Pascal uses begin, end
- Explicit Access Control
- C, C, Java provide public, protected, private
- Dynamic Scope
- Parameter Passing Mechanisms
- caller and callee
- actual parameters and formal parameters.
- call by value
- call by reference (call by address)
- call by name
- Aliasing
26An Example
procedure SUB(i,j) begin ii1 print(x)
iij print(i) end
- program TEST
- beginx1y2SUB(x,xy)print(x)
- end
27Results
- Answer
- Call-by-reference 2,5,5
- Call-by-value 1,5,1
- Call-by-name 2,6,6
- Call-by-value and copy-restore 1,5,5