compilers course 379k, tth 9:30-11:00 instructor: dr. doron a. peled office hours: mon 11:00-12:00
TRANSCRIPT
Compilers
Course 379K, TTH 9:30-11:00Instructor: Dr. Doron A. Peled
Office Hours: Mon 11:00-12:00
Textbook
CompilersPrinciples, Techniques and Tools.
Addison Wesley 1986
Technical stuff
Required: basic course in algorithms or data structures.
Not required: basic theory in CS (automata, regular expressions).
Exercises: Using compiler construction tools (LEX, YACC).
No Exam final project. Work in groups(depending on registration).
What is a compiler?
CompilerSourceprogram
TargetProgram(circuit)
Error messages
How to translate a program?
preprocessor
compiler
assembler
Loader/link editor
Source code
Source program
Target assembly program
Relocatable machine code
Absolute machine code
Compiler structure
Lexical analyzer
Syntax analyzer
Semantic analyzer
Intermediate codegenerator
Code optimizer
Code generator
Source program
Target program
Symbol table Error handling
Lexical analysis:Recognizing the basic tokens
if x>y*7 then x:=x-1 else print “ OK “
Ignore spaces (but not inside quotes).
if, x, >, y, *, 7, then, x, :=, x, -, 1, else, print, “ OK “
Make classification of tokens.
Continue Notation to denote the composition of
tokens (regular expressions). Internal representation of token
definition(finite state machines).
Algorithms for identifying tokens. Tools for defining the tokens, which
automatically generate a program for identifying them (e.g., LEX).
Syntax analysisexpr ::= const | var | ( expr ) | expr + expr |
expr * expr | expr – expr | expr / expr7+(8*y)/2
expr
expr
expr
expr7
8
2
+
( )
*
/
y
What about
7+8*y/2 ?
Continue
Notation to denote the syntax of aprogram (Bachus Naur Form, Context-free grammar).
Internal representation (Pushdown automata, Parse trees).
Algorithms for syntax analysis. Tools for generating the syntax
analyzer(YACC).
Semantic analyzer
Can check consistency in types between the different components of the expression.
Can check correspondence between parameters in procedure calls.
Intermediate code generator
7+(8*y)/2
expr
expr
expr
expr7
8
2
+
( )
*
/
y
a := 8
b := y
c := a * b
a := c
b := 2
c: = a / b
a := 7
b := c
c := a + b
Code optimization
7+(8*y)/2
expr
expr
expr
expr7
8
2
+
( )
*
/
y
a := y
a := a * 8
a := a / 2
a := a + 7
Code generation
7+(8*y)/2
expr
expr
expr
expr7
8
2
+
( )
*
/
y
Load a, y
Mult a, 8
Div a, 2
Add a, 7
Symbol table
Needs to update in various stages. Define variables. Check type consistency. Enforce scope rules. Generate run-time environment.
Error handling
Important in all stages. How to generate informative error
messages (syntax error at line 17…). How to prevent error propagation
(variable not declared, so expression not well defined, so procedure call notwell defined, etc.).
How to best guess user’s intention?
Architecture: pipeline
Lexical analyzer
Syntax analyzer
Semantic analyzer
Intermediate codegenerator
Code optimizer
Code generator
Source program
Target program
Symbol table Symbol table
Other benefits
Can generate easy search and substitute:%s/<\([0-9]*\)>/[\1]/g
Used in hardware design. Ideas used in software analysis
(finding dead code, slicing).