Download - Compilers Course 379K, TTH 9:30-11:00 Instructor: Dr. Doron A. Peled Office Hours: Mon 11:00-12:00
![Page 1: Compilers Course 379K, TTH 9:30-11:00 Instructor: Dr. Doron A. Peled Office Hours: Mon 11:00-12:00](https://reader036.vdocuments.us/reader036/viewer/2022082805/55160e5e55034694308b51f3/html5/thumbnails/1.jpg)
Compilers
Course 379K, TTH 9:30-11:00Instructor: Dr. Doron A. Peled
Office Hours: Mon 11:00-12:00
![Page 2: Compilers Course 379K, TTH 9:30-11:00 Instructor: Dr. Doron A. Peled Office Hours: Mon 11:00-12:00](https://reader036.vdocuments.us/reader036/viewer/2022082805/55160e5e55034694308b51f3/html5/thumbnails/2.jpg)
Textbook
CompilersPrinciples, Techniques and Tools.
Addison Wesley 1986
![Page 3: Compilers Course 379K, TTH 9:30-11:00 Instructor: Dr. Doron A. Peled Office Hours: Mon 11:00-12:00](https://reader036.vdocuments.us/reader036/viewer/2022082805/55160e5e55034694308b51f3/html5/thumbnails/3.jpg)
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).
![Page 4: Compilers Course 379K, TTH 9:30-11:00 Instructor: Dr. Doron A. Peled Office Hours: Mon 11:00-12:00](https://reader036.vdocuments.us/reader036/viewer/2022082805/55160e5e55034694308b51f3/html5/thumbnails/4.jpg)
What is a compiler?
CompilerSourceprogram
TargetProgram(circuit)
Error messages
![Page 5: Compilers Course 379K, TTH 9:30-11:00 Instructor: Dr. Doron A. Peled Office Hours: Mon 11:00-12:00](https://reader036.vdocuments.us/reader036/viewer/2022082805/55160e5e55034694308b51f3/html5/thumbnails/5.jpg)
How to translate a program?
preprocessor
compiler
assembler
Loader/link editor
Source code
Source program
Target assembly program
Relocatable machine code
Absolute machine code
![Page 6: Compilers Course 379K, TTH 9:30-11:00 Instructor: Dr. Doron A. Peled Office Hours: Mon 11:00-12:00](https://reader036.vdocuments.us/reader036/viewer/2022082805/55160e5e55034694308b51f3/html5/thumbnails/6.jpg)
Compiler structure
Lexical analyzer
Syntax analyzer
Semantic analyzer
Intermediate codegenerator
Code optimizer
Code generator
Source program
Target program
Symbol table Error handling
![Page 7: Compilers Course 379K, TTH 9:30-11:00 Instructor: Dr. Doron A. Peled Office Hours: Mon 11:00-12:00](https://reader036.vdocuments.us/reader036/viewer/2022082805/55160e5e55034694308b51f3/html5/thumbnails/7.jpg)
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.
![Page 8: Compilers Course 379K, TTH 9:30-11:00 Instructor: Dr. Doron A. Peled Office Hours: Mon 11:00-12:00](https://reader036.vdocuments.us/reader036/viewer/2022082805/55160e5e55034694308b51f3/html5/thumbnails/8.jpg)
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).
![Page 9: Compilers Course 379K, TTH 9:30-11:00 Instructor: Dr. Doron A. Peled Office Hours: Mon 11:00-12:00](https://reader036.vdocuments.us/reader036/viewer/2022082805/55160e5e55034694308b51f3/html5/thumbnails/9.jpg)
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 ?
![Page 10: Compilers Course 379K, TTH 9:30-11:00 Instructor: Dr. Doron A. Peled Office Hours: Mon 11:00-12:00](https://reader036.vdocuments.us/reader036/viewer/2022082805/55160e5e55034694308b51f3/html5/thumbnails/10.jpg)
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).
![Page 11: Compilers Course 379K, TTH 9:30-11:00 Instructor: Dr. Doron A. Peled Office Hours: Mon 11:00-12:00](https://reader036.vdocuments.us/reader036/viewer/2022082805/55160e5e55034694308b51f3/html5/thumbnails/11.jpg)
Semantic analyzer
Can check consistency in types between the different components of the expression.
Can check correspondence between parameters in procedure calls.
![Page 12: Compilers Course 379K, TTH 9:30-11:00 Instructor: Dr. Doron A. Peled Office Hours: Mon 11:00-12:00](https://reader036.vdocuments.us/reader036/viewer/2022082805/55160e5e55034694308b51f3/html5/thumbnails/12.jpg)
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
![Page 13: Compilers Course 379K, TTH 9:30-11:00 Instructor: Dr. Doron A. Peled Office Hours: Mon 11:00-12:00](https://reader036.vdocuments.us/reader036/viewer/2022082805/55160e5e55034694308b51f3/html5/thumbnails/13.jpg)
Code optimization
7+(8*y)/2
expr
expr
expr
expr7
8
2
+
( )
*
/
y
a := y
a := a * 8
a := a / 2
a := a + 7
![Page 14: Compilers Course 379K, TTH 9:30-11:00 Instructor: Dr. Doron A. Peled Office Hours: Mon 11:00-12:00](https://reader036.vdocuments.us/reader036/viewer/2022082805/55160e5e55034694308b51f3/html5/thumbnails/14.jpg)
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
![Page 15: Compilers Course 379K, TTH 9:30-11:00 Instructor: Dr. Doron A. Peled Office Hours: Mon 11:00-12:00](https://reader036.vdocuments.us/reader036/viewer/2022082805/55160e5e55034694308b51f3/html5/thumbnails/15.jpg)
Symbol table
Needs to update in various stages. Define variables. Check type consistency. Enforce scope rules. Generate run-time environment.
![Page 16: Compilers Course 379K, TTH 9:30-11:00 Instructor: Dr. Doron A. Peled Office Hours: Mon 11:00-12:00](https://reader036.vdocuments.us/reader036/viewer/2022082805/55160e5e55034694308b51f3/html5/thumbnails/16.jpg)
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?
![Page 17: Compilers Course 379K, TTH 9:30-11:00 Instructor: Dr. Doron A. Peled Office Hours: Mon 11:00-12:00](https://reader036.vdocuments.us/reader036/viewer/2022082805/55160e5e55034694308b51f3/html5/thumbnails/17.jpg)
Architecture: pipeline
Lexical analyzer
Syntax analyzer
Semantic analyzer
Intermediate codegenerator
Code optimizer
Code generator
Source program
Target program
Symbol table Symbol table
![Page 18: Compilers Course 379K, TTH 9:30-11:00 Instructor: Dr. Doron A. Peled Office Hours: Mon 11:00-12:00](https://reader036.vdocuments.us/reader036/viewer/2022082805/55160e5e55034694308b51f3/html5/thumbnails/18.jpg)
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).