1 / 22

Compiler Structures

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

erik
Download Presentation

Compiler Structures

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


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

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

  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). program in a source language program in a target language compiler

  4. The target program is run later. Input Compiler Target Program Target Program Source Program Compile-time Error messages Output Runtime Error messages

  5. Source Program 2. Structure of a Compiler ? Target Lang. Prog.

  6. Source Program Structure of a Compiler Front End Intermediate Code Back End Target Lang. Prog.

  7. Source Program Structure of a Compiler Lexical Analyzer Front End Syntax Analyzer Semantic Analyzer Int. Code Generator Intermediate Code Back End Target Lang. Prog.

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

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

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

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

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

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

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

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

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

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

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

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

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

  22. An interpreter 'compiles' and runs the source program all at once • it usually executes the intermediate code Source Program interpreter Output Input Error messages

More Related