Presentation is loading. Please wait.

Presentation is loading. Please wait.

Compiler Chang Chi-Chung 2008.02.26. Textbook Compilers: Principles, Techniques, and Tools, 2/E.  Alfred V. Aho, Columbia University  Monica S. Lam,

Similar presentations


Presentation on theme: "Compiler Chang Chi-Chung 2008.02.26. Textbook Compilers: Principles, Techniques, and Tools, 2/E.  Alfred V. Aho, Columbia University  Monica S. Lam,"— Presentation transcript:

1 Compiler Chang Chi-Chung 2008.02.26

2 Textbook Compilers: Principles, Techniques, and Tools, 2/E.  Alfred V. Aho, Columbia University  Monica S. Lam, Stanford University  Ravi Sethi, Avaya Labs  Jeffrey D. Ullman, Stanford University Free books  http://freecomputerbooks.com Dragon

3 Score Midterm Examination 20% Final Examination 25% Quizz(2) 30% Project 10% Homework 10% Participation 5%

4 Objectives 瞭解編譯器的構建原理  Know how to use compiler construction tools, such as generators of scanners and parsers  Be able to define LL(1), LR(1), and LALR(1) grammars  Be familiar with compiler analysis and optimization techniques 建立屬於自己的編譯器

5 History Early 1950’s  Mnemonic assembly languages. Macro instructions were added later. In the latter half of the 1950  Fortran: scientific computation. To take 18 man-years  Cobol: business data processing.  Lisp: symbolic computation.

6 History - Grace Hooper

7

8 自然語言 none 高階語言 machine independent 低階語言 machine dependent Programming Languages Chinese English German 機器語言組合語言 C 、 C++ 、 Java …

9 Introduction to Compilers(1) Compiler Source Program Target Program Source program must be equivalent to target program. Definitions  Recognizer  Translator

10 Introduction to Compilers(2) Translator  from one format to another query interpreter text formatter silicon compiler infix notation  postfix notation: 3 + 5 - 6 * 6 ====> 3 5 + 6 6 * - pretty printers Computational theory  power of certain machines the set of languages that can be recognized by this machine  Grammar definition of this machine

11 Impacts on Compilers Programming languages Machine architecture Language theory Algorithms and data structures Software engineering

12 Use the Compiler Techniques Programming Languages (C 、 C++…) Scripts (Javascript 、 bash) Editors (syntax highlighting) Pretty printers (e.g. Doxygen) Static checkers (e.g. Lint and Splint) Interpreters Text formatters (e.g. TeX and LaTeX) Silicon compilers (e.g. VHDL) Query interpreters/compilers (Databases)

13 Compilers 編譯器 Compilation  Translation of a program written in a source language into a semantically equivalent program written in a target language Compiler Error messages Source Program Target Program Input Output

14 Interpreters 直譯器 Interpretation  Performing the operations implied by the source program Interpreter Source Program Input Output Error messages

15 Hybrid Virtual Machine Translator Source Program intermediate program input output

16 Compiler vs. Interpreter 請自己蒐集資料比較 CompilerInterpreter 翻譯次數 1More 除錯較難較易

17 A Language-Processing System source program Preprocessor Compiler Assembler Linker/Loader target machine code modified source program target assembly program relocatable machine code library files relocatable object files Try for example: gcc -v myprog.c

18 Lexical Analyzer Syntax Analyzer Semantic Analyzer Intermediate Code Generator Machine-Independent Code Optimizer Code Generator Machine-Dependent Code Optimizer character stream target-machine code token stream syntax tree intermediate representation target-machine code intermediate representation target-machine code Symbol Table Phases of a Compiler

19 Lexical Analyzer Syntax Analyzer Semantic Analyzer character stream position = initial + rate * 60 = + * 60 = + * inttofloat 60

20 Intermediate Code Generator Machine-Independent Code Optimizer Code Generator t1 = inttofloat(60) t2 = id3 * t1 t3 = id2 + t2 id1 = t3 t1 = id3 * 60.0 id1 = id2 + t1 LDF R2, id3 MULF R2, R2, #60.0 LDF R1, id2 ADDF R1, R1, R2 STF id1, R1 1position… 2initial… 3rate… SYMBOL TABLE

21 The Grouping of Phases Compiler front and back ends:  Front end: analysis (machine independent)  Back end: synthesis (machine dependent) Compiler passes:  A collection of phases is done only once (single pass) or multiple times (multi pass) Single pass: usually requires everything to be defined before being used in source program Multi pass: compiler may have to keep entire program representation in memory

22 The Analysis-Synthesis Model of Compilation There are two parts to compilation:  Analysis determines the operations implied by the source program which are recorded in a tree structure  Synthesis takes the tree structure and translates the operations therein into the target program

23 Compiler-Construction Tools Software development tools are available to implement one or more compiler phases  Scanner generators  Parser generators  Syntax-directed translation engines  Automatic code generators  Data-flow engines

24 Programming Language Basic(1) Static vs. Dynamic  Compile Time vs. Run Time  Examples scope of declarations memory location of variables Environments and States names locations (variables) values environmentstate According to the scope rules of a language

25 Programming Language Basic(2) Static Scope and Block Structure  C/C++ uses { }  Pascal uses begin, end Explicit Access Control  C++, C#, Java provide public, protected, private Dynamic Scope Parameter Passing Mechanisms  caller and callee  actual parameters and formal parameters.  call by value  call by reference (call by address)  call by name Aliasing

26 An Example program TEST; begin x=1; y=2; SUB(x,x+y); print(x); end; procedure SUB(i,j) begin i=i+1; print(x); i=i+j; print(i); end;

27 Results Answer  Call-by-reference 2,5,5  Call-by-value 1,5,1  Call-by-name 2,6,6  Call-by-value and copy-restore 1,5,5


Download ppt "Compiler Chang Chi-Chung 2008.02.26. Textbook Compilers: Principles, Techniques, and Tools, 2/E.  Alfred V. Aho, Columbia University  Monica S. Lam,"

Similar presentations


Ads by Google