Presentation is loading. Please wait.

Presentation is loading. Please wait.

Compiler design Lecture 1: Compiler Overview Sulaimany University 2 Oct. 2011.

Similar presentations


Presentation on theme: "Compiler design Lecture 1: Compiler Overview Sulaimany University 2 Oct. 2011."— Presentation transcript:

1 Compiler design Lecture 1: Compiler Overview Sulaimany University 2 Oct. 2011

2 Text book Compilers: Principles, Techniques, and Tools (2nd ed.) Aho, Lam, Sethi, Ullman

3 What is Compiler Compiler: is a computer program that can read a program in one language – the source language – and translates it into an equivalent program in another language – the target language. Compiler Source program Target program Error message

4

5 In order to translate statements in a language, one needs to understand both ◦ the structure of the language: the way “sentences” are constructed in the language, and ◦ the meaning of the language: what each “sentence” stands for. ◦ Structure = Syntax ◦ Meaning = Semantics

6 Why study compiler design Only a few people will ever write their own compiler. So why bother? In addition to the development of a compiler, the techniques used in compiler design can be applicable to many problems in computer science. ◦ Techniques used in a lexical analyzer can be used in text editors, information retrieval system (reading, manipulating and writing data), and pattern recognition programs. ◦ Techniques used in a parser can be used in a query processing system such as SQL. Most of the techniques used in compiler design can be used in Natural Language Processing (NLP) systems.

7 Terminology The language the compiler itself is written is called the implementation language. Sometimes a compiler is written in the same language for which one is writing a compiler. This is done through Bootstrapping. interpreter: A program that reads an executable program and produces the results of executing that program

8 Major Parts of Compilers There are two major parts of a compiler: Analysis and Synthesis In analysis phase, an intermediate representation is created from the given source program. In synthesis phase, the equivalent target program is created from this intermediate representation. The analysis part is often called the front end of the compiler; the synthesis part is the back end.

9 Compiler Phases Analysis Synthesis

10 Compiler Phases (cont.) Each phase transforms the source program from one representation into another representation. They communicate with error handlers. They communicate with the symbol table.

11 Scanner (lexical analysis) Takes a stream of characters and identifies tokens from the lexemes. Eliminates comments and redundant whitespace. Keeps track of line numbers and column numbers and passes them as parameters to the other phases to enable error-reporting to the user.

12 Scanner (lexical analysis) (Cont.)

13 Syntax Analyzer (Parser) A Syntax Analyzer reads the tokens produced by the scanner and performs syntactic analysis, creates the syntactic structure (generally a parse tree) of the given program.

14 Syntax Analyzer (Parser) (Cont.)

15 Parsing Techniques Depending on how the parse tree is created, there are different parsing techniques. These parsing techniques are categorized into two groups: ◦ Top-Down Parsing, ◦ Bottom-Up Parsing Top-Down Parsing: ◦ Construction of the parse tree starts at the root, and proceeds towards the leaves. Bottom-Up Parsing: ◦ Construction of the parse tree starts at the leaves, and proceeds towards the root.

16 semantic analyzer A semantic analyzer checks the source program for semantic errors and collects the type information for the code generation. Type-checking is an important part of semantic analyzer. Ex: newval := oldval + 12  The type of the identifier newval must match with type of the expression (oldval+12)

17 CS416 Compiler Design17 Intermediate Code Generation A compiler may produce an explicit intermediate codes representing the source program. These intermediate codes are generally machine (architecture independent). But the level of intermediate codes is close to the level of machine codes.

18 Intermediate Code Generation (cont.) Ex: newval := oldval * fact + 1 id1 := id2 * id3 + 1 MULT id2,id3,temp1 ADDtemp1,#1,temp2 MOVtemp2,,id1

19 CS416 Compiler Design19 Code Optimizer (for Intermediate Code Generator) The code optimizer optimizes the code produced by the intermediate code generator in the terms of time and space. Ex: MULT id2,id3,temp1 ADDtemp1,#1,id1

20 CS416 Compiler Design20 Code Generator Produces the target language in a specific architecture. The target program is normally is a relocatable object file containing the machine codes.


Download ppt "Compiler design Lecture 1: Compiler Overview Sulaimany University 2 Oct. 2011."

Similar presentations


Ads by Google