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

Post on 24-Dec-2015

216 Views

Category:

Documents

0 Downloads

Preview:

Click to see full reader

TRANSCRIPT

Compiler Design

Nai-Wei Lin

Department of Computer Science

National Chung Cheng University

Chapter 1

Introduction to Compilers

3

Outline of This Chapter

♥ Introduction to compilers

♥ Introduction to compiler generators

♥ Introduction to automatic tool generators

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

5

Computer Organization

Operating System (Low Level Language)

Hardware Machine

Compiler

Applications (High Level Language)

6

Compilers

Compiler

Programwritten in

High-LevelLanguage

Programwritten in

Low-LevelLanguage

Error Message

7

Components of a Compiler

♥ Analysis– Lexical Analysis– Syntax Analysis– Semantic Analysis

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

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

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

10

Semantic Analysis:=

id1 +id2 *

id3 60

:=id1

+

id2*

id3

60

i2r

Someone plays the piano

The piano plays someone

(meaningful)

(meaningless)

Semantic Analysis

11

Intermediate Code Generation

:=id1

+id2

*id3

60i2r

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

Intermediate Code Generation

Someone breaks the ice

有人打破冰

12

Code Optimization

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

Code Optimization

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

有人打破冰

有人打破沉默

Code Optimization

13

Code Generation

Code Generation

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

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

有人打破沉默

有人打破沉默

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

15

Compiler Generators

Compiler GeneratorCompiler

Definition inMetalanguage

Compiler

Error Message

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

17

Automatic Tool Generators

Tool Generator Tool

Error Message

ToolDefinition in

Metalanguage

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)

19

Outline of This Course

♥ Lexical analysis

♥ Syntax analysis

♥ Semantic analysis

♥ Code generation

top related