220 likes | 476 Views
Compiler Structures. 241-437 , Semester 1 , 2011-2012. Objective what are the main features (structures) in a compiler?. 1. Overview. Overview. 1 . What is a Compiler? 2 . Structure of a Compiler 3 . Example Compilation 4 . Compiler Design Issues
E N D
Compiler Structures 241-437, Semester 1, 2011-2012 • Objective • what are the main features (structures) in a compiler? 1. Overview
Overview 1. What is a Compiler? 2. Structure of a Compiler 3. Example Compilation 4. Compiler Design Issues 5. Compilers and Interpreters
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). program in a source language program in a target language compiler
The target program is run later. Input Compiler Target Program Target Program Source Program Compile-time Error messages Output Runtime Error messages
Source Program 2. Structure of a Compiler ? Target Lang. Prog.
Source Program Structure of a Compiler Front End Intermediate Code Back End Target Lang. Prog.
Source Program Structure of a Compiler Lexical Analyzer Front End Syntax Analyzer Semantic Analyzer Int. Code Generator Intermediate Code Back End Target Lang. Prog.
Source Program Structure of a Compiler Lexical Analyzer Front End Syntax Analyzer Semantic Analyzer Int. Code Generator This course will concentrate on the "front end" tasks performed by a compiler. Intermediate Code Code Optimizer Back End Target Code Generator Target Lang. Prog.
Code Optimizer Target Code Generator Source Program 3. Example Compilation Lexical Analyzer Source Code: cur_time = start_time + cycles * 60 Syntax Analyzer Semantic Analyzer Int. Code Generator Intermediate Code Target Lang. Prog.
Code Optimizer Target Code Generator Example Compilation Source Program Lexical Analyzer Source Code: cur_time = start_time + cycles * 60 Lexical Analysis: ID(1)ASSIGN ID(2)ADD ID(3)MULT INT(60) Syntax Analyzer Semantic Analyzer Int. Code Generator Intermediate Code Target Lang. Prog.
Code Optimizer Target Code Generator Example Compilation Source Program Lexical Analyzer 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) Syntax Analyzer Semantic Analyzer Int. Code Generator Intermediate Code Target Lang. Prog.
Code Optimizer Target Code Generator Example Compilation Source Program 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) Lexical Analyzer Syntax Analyzer Semantic Analyzer Int. Code Generator Intermediate Code Target Lang. Prog.
Code Optimizer Target Code Generator Example Compilation Source Program 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 Lexical Analyzer Syntax Analyzer Semantic Analyzer Int. Code Generator Intermediate Code Target Lang. Prog.
Code Optimizer Target Code Generator Example Compilation Source Program 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 Lexical Analyzer Syntax Analyzer Semantic Analyzer Int. Code Generator Intermediate Code Target Lang. Prog.
Code Optimizer Target Code Generator Example Compilation Source Program 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 Lexical Analyzer Syntax Analyzer Semantic Analyzer Int. Code Generator Intermediate Code Target Lang. Prog.
Code Optimizer Target Code Generator Example Compilation Source Program 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 Lexical Analyzer Syntax Analyzer Semantic Analyzer Int. Code Generator Intermediate Code Target Lang. Prog.
Code Optimizer Target Code Generator Example Compilation Source Program Intermediate Code: temp1 = int2float(60) temp2 = id3 * temp1 temp3 = id2 + temp2 id1 = temp3 Optimized Code (step 3): temp2 = id3 * 60.0 id1 = id2 + temp2 Lexical Analyzer Syntax Analyzer Semantic Analyzer Int. Code Generator Intermediate Code Target Lang. Prog.
Code Optimizer Target Code Generator Example Compilation Source Program Intermediate Code: temp1 = int2float(60) temp2 = id3 * temp1 temp3 = id2 + temp2 id1 = temp3 Optimized Code: temp1 = id3 * 60.0 id1 = id2 + temp1 Lexical Analyzer Syntax Analyzer Semantic Analyzer Int. Code Generator Intermediate Code Target Lang. Prog.
Code Optimizer Target Code Generator Example Compilation Source Program 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 Lexical Analyzer Syntax Analyzer Semantic Analyzer Int. Code Generator Intermediate Code Target Lang. Prog.
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
5. Compilers and Interpreters • A compiler generates a target program that can be run later. Input Compiler Target Program Target Program Source Program Compile-time Error messages Output Runtime Error messages
An interpreter 'compiles' and runs the source program all at once • it usually executes the intermediate code Source Program interpreter Output Input Error messages