Compiler Principle and Technology - PowerPoint PPT Presentation

1 / 45
About This Presentation
Title:

Compiler Principle and Technology

Description:

Textbook: COMPILER CONSTRUCTION Principle and Practice. by Kenneth C. Louden (China Machine Press) Course Web http://netmedia.zju.edu.cn/compiler – PowerPoint PPT presentation

Number of Views:345
Avg rating:3.0/5.0
Slides: 46
Provided by: educ5458
Category:

less

Transcript and Presenter's Notes

Title: Compiler Principle and Technology


1
Compiler Principle and Technology
  • Prof. Dongming LU
  • Mar. 11th, 2015

2
  • Textbook
  • COMPILER CONSTRUCTION Principle and Practice
  • by Kenneth C. Louden
  • (China Machine Press)
  • Course Webhttp//netmedia.zju.edu.cn/compiler
  • Email ldm_at_cs.zju.edu.cn (Prof. Lu Dongming)

3
  • Reference books
  • 1.Compilers -- Principles, Techniques and
    Tools, (Dragon Book), by Aho, Sethi and Ullman
    (1986)
  • 2. Modern Compiler Implementation in Java,
    by Andrew Appel (2002)
  • 3. ??????????(?3?),???????,????
  • 4. Compiler Design in C, Prentice Hall,
    Allen I. Holub
  • 5. ???????,???????,?????
  • TA youyou_chen_at_foxmail.com (???)

4
Evaluation 1. Homeworks 15     2.
Quizzes 10     3. Mid-Term Exam 20     4.
Final Exams 55
5
Content
  1. INTRODUCTION
  2. SCANNING
  3. CONTEXT-FREE GRMMARS AND PARSING
  4. TOP-DOWN PARSING
  5. BOTTOM-UP PARSING
  6. SEMANTIC ANALYSIS
  7. RUNTIME ENVIRONMENT
  8. CODE GENERATION

6
1. INTRODUCTION
7
What is a compiler?
  • It translates one language to another

extension
8
The purpose of this text
  • To provide basic knowledge
  • To help us understand the design pattern and
    module division methods in software engineering
  • A compiler can be regarded as a special
    software.
  • To give necessary tools and practical experience
    (In Summer Term)
  • A series of simple examples
  • TINY, C-Minus

How to translate?
9
Main Topics
  • 1.1 Why Compilers? A Brief History
  • 1.2 Programs Related to Compilers
  • 1.3 The Translation Process
  • 1.4 Major Data Structures in a Compiler
  • 1.5 Other Issues in Compiler Structure
  • 1.6 Bootstrapping and Porting

10
1.1 Why? A Brief History
11
Why Compiler
time consuming and tedious, not easy to understand
Still not easy to write and understand
X2
Mov x, 2
C7 06 0000 0002
12
Brief History of Compiler
Theories and algorithmsFA, CFG et al
The Chomsky Hierarchy was also studied
Some tools Yacc, Lex
1954-1957First compiler
1960s 1970s
1970s1990s
The basic of compiler design have not changed much
last 20 years IDE
13
1.2 Programs related to Compiler
14
Programs related to compiler
Interpreters
Editors
Debuggers
similar to
Compiler
Project managers
Assembler
Linker
Processor
Loader
Preprocessor
Programms above compiler
Programms under compiler
15
Interpreters
  • Execute the source program immediately rather
    than generating object code
  • Examples BASIC, LISP
  • Used often in educational or development
    situations
  • Speed of execution is slower than compiled code
    by a factor of 10 or more
  • Share many of their operations with compilers

16
Interpreters VS compilers
compilers
interpreters
interpreter
X86
execute
X86
execute
Basic,Lisp, Php source code
C,C source code
compiler
execute
execute
interpreter
ARM
ARM
execute
execute
MIPS
MIPS
interpreter
17
Programs related to compiler
Editors, IDE
Source code
Compiler
Assembly code
Assembler
Object code
Linker
depends on OS and processor
Executable file
Loader
Executable file
Preprocessor
Executable file
Processor
18
1.3 The Translation Process
19
The Phases of a Compiler
Source code
Scanner
Tokens
Literal Table
Parser
Syntax Tree
Semantics Analyzer
Symbol Table
Annotated Tree
Source Code Optimizer
Error Handler
Intermediate code
Code Generator
Target code
Important data structures
Target Code Optimizer
Target code
20
The phases of a compiler
  • Six phases
  • Scanner
  • Parser
  • Semantic Analyzer
  • Source code optimizer
  • Code generator
  • Target Code Optimizer
  • Three auxiliary components
  • Literal table
  • Symbol table
  • Error Handler

21
The Scanner
  • Lexical analysis sequences of characters-gttokens
  • eg aindex42
  • ?id, 1? ?? ?id, 2? ?? ?? ?4?
    ?? ?2?
  • a id
  • left bracket
  • index id
  • right bracket
  • assignment
  • 4 number
  • plus sign
  • 2 number

?????
22
The Parser
  • Syntax analysis
  • Tokens -gt Parse tree / Syntax tree ( abstract
    syntax tree)

aindex42
The Parse Tree
The Syntax Tree
23
The Semantic Analyzer
  • The semantics of a program are its meaning,
    including semantics and type checking
  • eg aindex42

The Syntax Tree annotated with attributes
24
1.4 Other Issues in Compiler Structure
25
The Structure of Compiler
  • Multiple views from different angles
  • Logical Structure
  • Physical Structure
  • Sequencing of the operations
  • A major impact of the structure
  • Reliability, efficiency
  • Usefulness, maintainability

26
Analysis and Synthesis
  • The analysis part of the compiler analyzes the
    source program to compute its properties
  • Lexical analysis, syntax analysis and semantics
    analysis, as well as optimization
  • More mathematical and better understood
  • The synthesis part of the compiler produces the
    translated codes
  • Code generation, as well as optimization
  • More specialized
  • The two parts can be changed independently of the
    other

27
Front End and Back End
Front end-orient to people depend on the source
language independent of target language
This is important for compiler portability
Back end- orient to computerdepend on the target
language independent of source language
28
Passes
  • The repetitions to process the entire source
    program before generating code are referred as
    passes.
  • A compiler can be one pass or multiple passes
  • One pass efficient compilation less efficient
    target code
  • -gt space consuming
  • Multiple passes efficient target code
    complicated compilation
  • -gt time consuming
  • Most compilers with optimization use more than
    one pass
  • One Pass for scanning and parsing
  • One Pass for semantic analysis and source-level
    optimization
  • The third Pass for code generation and
    target-level optimization

29
Language Definition and compilers
  • The lexical and syntactic structure of a
    programming language
  • Regular expressions
  • Context-free grammar
  • The semantics of a programming language in
    English descriptions
  • Language reference manual, or language definition.

30
Language Definition and compilers
  • A language definition and a compiler are often
    developed simultaneously
  • The techniques have a major impact on definition
  • The definition has a major impact on the
    techniques

31
Compiler options and interfaces
  • Mechanisms for interfacing with the operation
    system
  • Input and output facilities
  • Access to the file system of the target machine
  • Options to the user for various purposes
  • Specification of listing characteristic
  • Code optimization options

32
Error Handling
  • Static (or compile-time) errors must be reported
    by a compiler
  • Generate meaningful error messages and resume
    compilation after each error
  • Each phase of a compiler needs different kind of
    error handing
  • Exception handling
  • Generate extra code to perform suitable runtime
    tests to guarantee all such errors to cause an
    appropriate event during execution.

33
1.5 Bootstrapping and Porting
34
Third Language for Compiler Construction
  • Machine language
  • Compiler to execute immediately
  • Another language with existed compiler on the
    same target machine (First Scenario)
  • Compile the new compiler with existing compiler
  • Another language with existed compiler on
    different machine (Second Scenario)
  • Compilation produce a cross compiler

35
T-Diagram Describing Complex Situation
  • A compiler written in language H that translates
    language S into language T.
  • S T
  • H
  • T-Diagram can be combined in two basic ways.

36
The First T-diagram Combination
  • A B B C A C
  • H H H
  • Two compilers run on the same machine H
  • First from A to B
  • Second from B to C
  • Result from A to C on H

37
The Second T-diagram Combination
  • A B A B
  • H H K K
  • M
  • Translate implementation language of a compiler
    from H to K
  • Use another compiler from H to K

38
The First Scenario
  • A H A H
  • B B H H
  • H
  • Translate a compiler from A to H written in B
  • Use an existing compiler for language B on
    machine H

39
The Second Scenario
  • A H A H
  • B B K K
  • K
  • Use an existing compiler for language B on
    different machine K
  • Result in a cross compiler

40
Process of Bootstrapping
  • Write a compiler in the same language
  • S T
  • S
  • No compiler for source language yet
  • Porting to a new host machine

41
The First step in bootstrap
  • A H A H
  • A A H H
  • H
  • quick and dirty compiler written in machine
    language H
  • Compiler written in its own language A
  • Result in running but inefficient compiler

42
The Second step in bootstrap
  • A H A H
  • A A H H
  • H
  • Running but inefficient compiler
  • Compiler written in its own language A
  • Result in final version of the compiler

43
The step 1 in porting
  • A K A K
  • A A H H
  • H
  • Original compiler
  • Compiler source code retargeted to K
  • Result in Cross Compiler

44
The step 2 in porting
  • A K A K
  • A A K K
  • H
  • Cross compiler
  • Compiler source code retargeted to K
  • Result in Retargeted Compiler

45
End of Chapter OneThanks
  • Mar. 11th, 2015
Write a Comment
User Comments (0)
About PowerShow.com