CS 321 - Compilers: Outline
CS 321 - Compilers: Outline
Outline
Lectures
Sunday @ 10:10 AM
Monday @ 11:50 AM
CS 321 - Compilers
Teaching Assistant
Eng. Rania Salama
Textbook
Aho, Lam, Sethi, & Ullman, Compilers: Principles, Techniques, and Tools Second
Edition.
Grading
Sheets and Programming Project 20%
Midterm Exam 20%
Final Exam 60%
2
Programming Languages:
Notations for describing computations to people and
machines
Need to be translated to executable languages
Compiler:
Program that translates from one programming
language to another programming languages (maybe
machine language).
Structure of a Compiler
3/9/2014
Be able to build a
(programming) language
compiler
for
(simplified)
Interpreters
Compilers
Output
Input
Source
Program
Error messages
Compiler
Error messages
Target
Program
Output
3/9/2014
Source Program
Target Assembly
program
Pre-Processor
Compiler
Assembler
Relocatable
Machine Code
Loader
Link/Editor
Library,
relocatable object files
10
Preprocessors
Ex: Macro Processing
#define Y A*B+C
11
12
3/9/2014
Phases of a Compiler
Source Program
3
Symbol-table
Manager
Semantic Analyzer
Intermediate Code
Generator
Code Optimizer
Code Generator
Target Program
13
14
identifier
:=__ initial
*_ 60
_______ _+ rate
___
__ _;
position
:=
expression
identifier
initial
Blanks, Line breaks, etc. are scanned out
assignment
statement
For Example:
Position
__________
Syntax Analyzer
Error Handler
4
Lexical Analyzer
expression
+
expression
*
expression
expression
identifier
rate
number
60
16
3/9/2014
statement
Identify Only Individual words that are the the Tokens of the Language
if statement, or ...
assignment statement
is an
expression
is an
identifier := expression ;
(expression), or
expression + expression, or
expression * expression, or
number, or
17
identifier, or ...
18
:=
position
position
initial
*
rate
60
initial
*
rate
inttoreal
. Etc.
60
Compressed Tree
Conversion Action
19
20
3/9/2014
Phases of a Compiler
Source Program
3
Symbol-table
Manager
Lexical Analyzer
Syntax Analyzer
Semantic Analyzer
Error Handler
Error Handling
5
Intermediate
Code Generator
Code Optimizer
Code Generator
Target Program
21
22
lexical analyzer
id1 := id2 + id3 * 60
syntax analyzer
:=
+
id1
id2
Code Optimization
*
id3
60
semantic analyzer
:=
Symbol
Table
position ....
id1
id2l
*
id3
rate.
inttoreal
60
initial .
intermediate code generator
E
r
r
o
r
s
24
3/9/2014
Symbol Table
E
r
r
o
r
s
position ....
initial .
intermediate code generator
rate.
temp1 := inttoreal(60)
temp2 := id3 * temp1
temp3 := id2 + temp2
id1 := temp3
vs.
Number of Passes:
3 address code
code optimizer
temp1 := id3 * 60.0
id1 := id2 + temp1
Front End
Tradeoffs ..
25
Back End
scanning
parsing
sem. analysis
Parser Generators:
code generation
intermediate
representation
language-dependent
Java
C
Pascal
26
Scanner Generators:
machine-dependent
Pentium
PowerPC
SPARC
Advantages
Disadvantages
better portability
many combinations between front ends
and back ends possible
optimizations are easier on the intermediate
representation than on source code
slower
needs more memory
28
3/9/2014
Lexical Analysis
Syntax Analysis
Semantic Analysis
Intermediate Code Generation
29
30