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

## 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:

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

Textbook Compilers Principles, 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? Compiler Source program Target Program (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 code generator 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 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 ?

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

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

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

Code generation 7+(8*y)/2 expr 7 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 not well defined, etc.). How to best guess users intention?

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

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