compiler design nai-wei lin department of computer science national chung cheng university

19
Compiler Design Nai-Wei Lin Department of Computer Sc ience National Chung Cheng Univ ersity

Upload: caitlin-montgomery

Post on 24-Dec-2015

216 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Compiler Design Nai-Wei Lin Department of Computer Science National Chung Cheng University

Compiler Design

Nai-Wei Lin

Department of Computer Science

National Chung Cheng University

Page 2: Compiler Design Nai-Wei Lin Department of Computer Science National Chung Cheng University

Chapter 1

Introduction to Compilers

Page 3: Compiler Design Nai-Wei Lin Department of Computer Science National Chung Cheng University

3

Outline of This Chapter

♥ Introduction to compilers

♥ Introduction to compiler generators

♥ Introduction to automatic tool generators

Page 4: Compiler Design Nai-Wei Lin Department of Computer Science National Chung Cheng University

4

Programming Languages

♥ Human uses human languages to communicate with each other– Chinese, English, French

♥ Human uses programming languages to communicate with computers– Fortran, C, Java

Page 5: Compiler Design Nai-Wei Lin Department of Computer Science National Chung Cheng University

5

Computer Organization

Operating System (Low Level Language)

Hardware Machine

Compiler

Applications (High Level Language)

Page 6: Compiler Design Nai-Wei Lin Department of Computer Science National Chung Cheng University

6

Compilers

Compiler

Programwritten in

High-LevelLanguage

Programwritten in

Low-LevelLanguage

Error Message

Page 7: Compiler Design Nai-Wei Lin Department of Computer Science National Chung Cheng University

7

Components of a Compiler

♥ Analysis– Lexical Analysis– Syntax Analysis– Semantic Analysis

♥ Synthesis– Intermediate Code Generation– Code Optimization– Code Generation

Page 8: Compiler Design Nai-Wei Lin Department of Computer Science National Chung Cheng University

8

Lexical Analysis

Lexical Analysis

final := initial + rate * 60

id1 := id2 + id3 * 60Someone breaks the ice

S o m e o n e b r e a k st h e i c e

Lexical Analysis

Page 9: Compiler Design Nai-Wei Lin Department of Computer Science National Chung Cheng University

9

Syntax Analysis

Syntax Analysis

id1 := id2 + id3 * 60

:=id1 +

id2 *id3 60

Someone breaks the ice

Someone breaks the ice

subject verb object

sentence

Syntax Analysis

Page 10: Compiler Design Nai-Wei Lin Department of Computer Science National Chung Cheng University

10

Semantic Analysis:=

id1 +id2 *

id3 60

:=id1

+

id2*

id3

60

i2r

Someone plays the piano

The piano plays someone

(meaningful)

(meaningless)

Semantic Analysis

Page 11: Compiler Design Nai-Wei Lin Department of Computer Science National Chung Cheng University

11

Intermediate Code Generation

:=id1

+id2

*id3

60i2r

temp1 := i2r ( 60 )temp2 := id3 * temp1temp3 := id2 + temp2id1 := temp3

Intermediate Code Generation

Someone breaks the ice

有人打破冰

Page 12: Compiler Design Nai-Wei Lin Department of Computer Science National Chung Cheng University

12

Code Optimization

temp1 := i2r ( 60 )temp2 := id3 * temp1temp3 := id2 + temp2id1 := temp3

Code Optimization

temp1 := id3 * 60.0id1 := id2 + temp1

有人打破冰

有人打破沉默

Code Optimization

Page 13: Compiler Design Nai-Wei Lin Department of Computer Science National Chung Cheng University

13

Code Generation

Code Generation

temp1 := id3 * 60.0id1 := id2 + temp1

movf id3, r2mulf #60.0, r2movf id2, r1addf r2, r1movf r1, id1

有人打破沉默

有人打破沉默

Page 14: Compiler Design Nai-Wei Lin Department of Computer Science National Chung Cheng University

14

Metalanguages

Metalanguage: a language used to define another language

We will use different metalangauges to define the various components of a programming language so that these components can be generated automatically

Page 15: Compiler Design Nai-Wei Lin Department of Computer Science National Chung Cheng University

15

Compiler Generators

Compiler GeneratorCompiler

Definition inMetalanguage

Compiler

Error Message

Page 16: Compiler Design Nai-Wei Lin Department of Computer Science National Chung Cheng University

16

Definition of Programming Definition of Programming LanguagesLanguages

♥ Lexical analysis: regular expressions♥ Syntax analysis: context free grammars♥ Semantics analysis: attribute grammars♥ Intermediate code generation:

attribute grammars♥ Code generation: tree grammars

Page 17: Compiler Design Nai-Wei Lin Department of Computer Science National Chung Cheng University

17

Automatic Tool Generators

Tool Generator Tool

Error Message

ToolDefinition in

Metalanguage

Page 18: Compiler Design Nai-Wei Lin Department of Computer Science National Chung Cheng University

18

Applications of Compilation Techniques

♥ Web Browsers (HTML, XML, …)♥ Word Processors (postscript, pdf, …)♥ Computer-Aided Software Engineering (UM

L)♥ Computer-Aided Design (VHDL, Verilog, …)♥ Computer-Aided Manufacturing (Pattern Reco

gnition)

Page 19: Compiler Design Nai-Wei Lin Department of Computer Science National Chung Cheng University

19

Outline of This Course

♥ Lexical analysis

♥ Syntax analysis

♥ Semantic analysis

♥ Code generation