Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Compilers Course 379K, TTH 9:30-11:00 Instructor: Dr. Doron A. Peled Office Hours: Mon 11:00-12:00."— Presentation transcript:

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

2 Textbook Compilers Principles, Techniques and Tools. Addison Wesley 1986

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

4 What is a compiler? Compiler Source program Target Program (circuit) Error messages

5 How to translate a program? preprocessor compiler assembler Loader/link editor Source code Source program Target assembly program Relocatable machine code Absolute machine code

6 Compiler structure Lexical analyzer Syntax analyzer Semantic analyzer Intermediate code generator Code optimizer Code generator Source program Target program Symbol table Error handling

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

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

9 Syntax analysis expr ::= const | var | ( expr ) | expr + expr | expr * expr | expr – expr | expr / expr 7+(8*y)/2 expr 7 8 2 + () * / y What about 7+8*y/2 ?

10 Continue Notation to denote the syntax of a program (Bachus Naur Form, Context- free grammar). Internal representation (Pushdown automata, Parse trees). Algorithms for syntax analysis. Tools for generating the syntax analyzer (YACC).

11 Semantic analyzer Can check consistency in types between the different components of the expression. Can check correspondence between parameters in procedure calls.

12 Intermediate code generator 7+(8*y)/2 expr 7 8 2 + () * / y a := 8 b := y c := a * b a := c b := 2 c: = a / b a := 7 b := c c := a + b

13 Code optimization 7+(8*y)/2 expr 7 8 2 + () * / y a := y a := a * 8 a := a / 2 a := a + 7

14 Code generation 7+(8*y)/2 expr 7 8 2 + () * / y Load a, y Mult a, 8 Div a, 2 Add a, 7

15 Symbol table Needs to update in various stages. Define variables. Check type consistency. Enforce scope rules. Generate run-time environment.

16 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 not well defined, etc.). How to best guess users intention?

17 Architecture: pipeline Lexical analyzer Syntax analyzer Semantic analyzer Intermediate code generator Code optimizer Code generator Source program Target program Symbol table

18 Other benefits Can generate easy search and substitute: %s/ /[\1]/g Used in hardware design. Ideas used in software analysis (finding dead code, slicing).


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

Similar presentations


Ads by Google