1 / 27

Compiler

Compiler. Chang Chi-Chung 2008.02.26. 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.

marc
Download Presentation

Compiler

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 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. Programming Languages 低階語言 machine dependent 高階語言 machine independent 自然語言 none 機器語言 組合語言 C、C++、Java … Chinese English German

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

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

  10. Impacts on Compilers • Programming languages • Machine architecture • Language theory • Algorithms and data structures • Software engineering

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

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

  13. Source Program Interpreter Output Input Error messages Interpreters 直譯器 • Interpretation • Performing the operations implied by the source program

  14. Hybrid Source Program Translator Virtual Machine intermediate program input output

  15. Compiler vs. Interpreter • 請自己蒐集資料比較

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

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

  18. = = + <id,1> + <id,1> * <id,2> * <id,2> <id,3> 60 <id,3> inttofloat 60 character stream position = initial + rate * 60 Lexical Analyzer <id,1> <=> <id,2> <+> <id,3> <*> <60> Syntax Analyzer Semantic Analyzer

  19. Intermediate Code Generator t1 = inttofloat(60) t2 = id3 * t1 t3 = id2 + t2 id1 = t3 Machine-Independent Code Optimizer t1 = id3 * 60.0 id1 = id2 + t1 Code Generator LDF R2, id3 MULF R2, R2, #60.0 LDF R1, id2 ADDF R1, R1, R2 STF id1, R1 SYMBOL TABLE

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

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

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

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

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

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

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

More Related