Presentation is loading. Please wait.

Presentation is loading. Please wait.

241-437 Compilers: Overview/1 1 Compiler Structures Objective – –what are the main features (structures) in a compiler? 241-437, Semester 1, 2011-2012.

Similar presentations


Presentation on theme: "241-437 Compilers: Overview/1 1 Compiler Structures Objective – –what are the main features (structures) in a compiler? 241-437, Semester 1, 2011-2012."— Presentation transcript:

1 241-437 Compilers: Overview/1 1 Compiler Structures Objective – –what are the main features (structures) in a compiler? 241-437, Semester 1, 2011-2012 1. Overview

2 241-437 Compilers: Overview/1 2 Overview 1. What is a Compiler? 2. Structure of a Compiler 3. Example Compilation 4. Compiler Design Issues 5. Compilers and Interpreters

3 241-437 Compilers: Overview/1 3 1. What is a Compiler? A compiler reads a program written in one language and translates it into another language. The source language is high-level (e.g. C), and the target language is low-level (e.g. machine code). compiler program in a source language program in a target language

4 241-437 Compilers: Overview/1 4 The target program is run later. Compiler Compile-time Error messages Source Program Target Program Input Output Target Program Runtime Error messages

5 241-437 Compilers: Overview/1 5 2. Structure of a Compiler Source Program ? Target Lang. Prog.

6 241-437 Compilers: Overview/1 6 Structure of a Compiler Source Program Target Lang. Prog. Front End Back End Intermediate Code

7 241-437 Compilers: Overview/1 7 Structure of a Compiler Source Program Target Lang. Prog. Semantic Analyzer Syntax Analyzer Lexical Analyzer Front End Back End Int. Code Generator Intermediate Code

8 241-437 Compilers: Overview/1 8 Structure of a Compiler Source Program Target Lang. Prog. Semantic Analyzer Syntax Analyzer Lexical Analyzer Front End Code Optimizer Target Code Generator Back End Int. Code Generator Intermediate Code This course will concentrate on the "front end" tasks performed by a compiler.

9 241-437 Compilers: Overview/1 9 Source Program Target Lang. Prog. Semantic Analyzer Syntax Analyzer Lexical Analyzer Code Optimizer Target Code Generator Int. Code Generator Intermediate Code Source Code: cur_time = start_time + cycles * 60 3. Example Compilation

10 241-437 Compilers: Overview/1 10 Source Program Target Lang. Prog. Semantic Analyzer Syntax Analyzer Lexical Analyzer Code Optimizer Target Code Generator Int. Code Generator Intermediate Code Source Code: cur_time = start_time + cycles * 60 Lexical Analysis: ID(1) ASSIGN ID(2) ADD ID(3) MULT INT(60) Example Compilation

11 241-437 Compilers: Overview/1 11 Source Program Target Lang. Prog. Semantic Analyzer Syntax Analyzer Lexical Analyzer Code Optimizer Target Code Generator Int. Code Generator Intermediate Code Source Code: cur_time = start_time + cycles * 60 Lexical Analysis: ID(1) ASSIGN ID(2) ADD ID(3) MULT INT(60) Syntax Analysis: ASSIGN ID(1) ADD ID(2) MULT ID(3) INT(60) Example Compilation

12 241-437 Compilers: Overview/1 12 Source Program Target Lang. Prog. Semantic Analyzer Syntax Analyzer Lexical Analyzer Code Optimizer Target Code Generator Int. Code Generator Intermediate Code Syntax Analysis: ASSIGN ID(1) ADD ID(2) MULT ID(3) INT(60) Sematic Analysis: ASSIGN ID(1) ADD ID(2) MULT ID(3) int2float INT(60) Example Compilation

13 241-437 Compilers: Overview/1 13 Source Program Target Lang. Prog. Semantic Analyzer Syntax Analyzer Lexical Analyzer Code Optimizer Target Code Generator Int. Code Generator Intermediate Code Sematic Analysis: ASSIGN ID(1) ADD ID(2) MULT ID(3) int2float INT(60) Intermediate Code: temp1 = int2float(60) temp2 = id3 * temp1 temp3 = id2 + temp2 id1 = temp3 Example Compilation

14 241-437 Compilers: Overview/1 14 Source Program Target Lang. Prog. Semantic Analyzer Syntax Analyzer Lexical Analyzer Code Optimizer Target Code Generator Int. Code Generator Intermediate Code Intermediate Code: temp1 = int2float(60) temp2 = id3 * temp1 temp3 = id2 + temp2 id1 = temp3 Optimized Code (step 0): temp1 = int2float(60) temp2 = id3 * temp1 temp3 = id2 + temp2 id1 = temp3 Example Compilation

15 241-437 Compilers: Overview/1 15 Source Program Target Lang. Prog. Semantic Analyzer Syntax Analyzer Lexical Analyzer Code Optimizer Target Code Generator Int. Code Generator Intermediate Code Intermediate Code: temp1 = int2float(60) temp2 = id3 * temp1 temp3 = id2 + temp2 id1 = temp3 Optimized Code (step 1): temp1 = 60.0 temp2 = id3 * temp1 temp3 = id2 + temp2 id1 = temp3 Example Compilation

16 241-437 Compilers: Overview/1 16 Source Program Target Lang. Prog. Semantic Analyzer Syntax Analyzer Lexical Analyzer Code Optimizer Target Code Generator Int. Code Generator Intermediate Code Intermediate Code: temp1 = int2float(60) temp2 = id3 * temp1 temp3 = id2 + temp2 id1 = temp3 Optimized Code (step 2): temp2 = id3 * 60.0 temp3 = id2 + temp2 id1 = temp3 Example Compilation

17 241-437 Compilers: Overview/1 17 Source Program Target Lang. Prog. Semantic Analyzer Syntax Analyzer Lexical Analyzer Code Optimizer Target Code Generator Int. Code Generator Intermediate Code Intermediate Code: temp1 = int2float(60) temp2 = id3 * temp1 temp3 = id2 + temp2 id1 = temp3 Optimized Code (step 3): temp2 = id3 * 60.0 id1 = id2 + temp2 Example Compilation

18 241-437 Compilers: Overview/1 18 Source Program Target Lang. Prog. Semantic Analyzer Syntax Analyzer Lexical Analyzer Code Optimizer Target Code Generator Int. Code Generator Intermediate Code Intermediate Code: temp1 = int2float(60) temp2 = id3 * temp1 temp3 = id2 + temp2 id1 = temp3 Optimized Code: temp1 = id3 * 60.0 id1 = id2 + temp1 Example Compilation

19 241-437 Compilers: Overview/1 19 Source Program Target Lang. Prog. Semantic Analyzer Syntax Analyzer Lexical Analyzer Code Optimizer Target Code Generator Int. Code Generator Intermediate Code Intermediate Code: temp1 = int2float(60) temp2 = id3 * temp1 temp3 = id2 + temp2 id1 = temp3 Optimized Code: temp1 = id3 * 60.0 id1 = id2 + temp1 Target Code: MOVF id3, R2 MULF #60.0, R2 MOVF id2, R1 ADDF R2, R1 MOVF R1, id1 Example Compilation

20 241-437 Compilers: Overview/1 20 4. Compiler Design Issues Correctness – –does the target do the 'same' as the source? Speed (compile time and runtime) – –code optimizations – –multiple passes over code Memory space required Good feedback to user – –error reporting

21 241-437 Compilers: Overview/1 21 5. Compilers and Interpreters A compiler generates a target program that can be run later. Compiler Compile-time Error messages Source Program Target Program Input Output Target Program Runtime Error messages

22 241-437 Compilers: Overview/1 22 interpreter Source Program Input Output Error messages An interpreter 'compiles' and runs the source program all at once – –it usually executes the intermediate code


Download ppt "241-437 Compilers: Overview/1 1 Compiler Structures Objective – –what are the main features (structures) in a compiler? 241-437, Semester 1, 2011-2012."

Similar presentations


Ads by Google