compilers course 379k, tth 9:30-11:00 instructor: dr. doron a. peled office hours: mon 11:00-12:00

18
Compilers Course 379K, TTH 9:30- 11:00 Instructor: Dr. Doron A. Peled Office Hours: Mon 11:00- 12:00

Upload: kimberly-kelly

Post on 28-Mar-2015

216 views

Category:

Documents


3 download

TRANSCRIPT

Page 1: Compilers Course 379K, TTH 9:30-11:00 Instructor: Dr. Doron A. Peled Office Hours: Mon 11:00-12:00

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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).