12
1 Languages and Compilers (SProg og Oversættere) Structure of the compiler

Languages and Compilers (SProg og Oversættere)

  • Upload
    yori

  • View
    29

  • Download
    0

Embed Size (px)

DESCRIPTION

Languages and Compilers (SProg og Oversættere). Structure of the compiler. Structure of the compiler. Describe the phases of the compiler and give an overall description of what the purpose of each phase is and how the phases interface. The “Phases” of a Compiler. Source Program. - PowerPoint PPT Presentation

Citation preview

Page 1: Languages and Compilers (SProg og Oversættere)

1

Languages and Compilers(SProg og Oversættere)

Structure of the compiler

Page 2: Languages and Compilers (SProg og Oversættere)

2

Structure of the compiler

• Describe the phases of the compiler and give an overall description of what the purpose of each phase is and how the phases interface

Page 3: Languages and Compilers (SProg og Oversættere)

3

The “Phases” of a Compiler

Syntax Analysis

Contextual Analysis

Code Generation

Source Program

Abstract Syntax Tree

Decorated Abstract Syntax Tree

Object Code

Error Reports

Error Reports

Page 4: Languages and Compilers (SProg og Oversættere)

4

Single Pass Compiler

Compiler Driver

Syntactic Analyzer

calls

calls

Contextual Analyzer Code Generator

calls

Dependency diagram of a typical Single Pass Compiler:

A single pass compiler makes a single pass over the source text, parsing, analyzing and generating code all at once.

Page 5: Languages and Compilers (SProg og Oversættere)

5

Multi Pass Compiler

Compiler Driver

Syntactic Analyzer

callscalls

Contextual Analyzer Code Generator

calls

Dependency diagram of a typical Multi Pass Compiler:

A multi pass compiler makes several passes over the program. The output of a preceding phase is stored in a data structure and used by subsequent phases.

input

Source Text

output

AST

input output

Decorated AST

input output

Object Code

Page 6: Languages and Compilers (SProg og Oversættere)

6

1) Syntax Analysis -> AST

Program

LetCommand

SequentialDeclaration

n Integer c Char c ‘&’ n n + 1

Ident Ident Ident Ident Ident Ident Ident OpChar.Lit Int.Lit

SimpleT

VarDecl

SimpleT

VarDecl

SimpleV

Char.Expr

SimpleV

VNameExp Int.Expr

AssignCommand BinaryExpr

SequentialCommand

AssignCommand

Page 7: Languages and Compilers (SProg og Oversættere)

7

2) Contextual Analysis -> Decorated AST

Contextual Analysis

Decorated Abstract Syntax Tree

Error Reports

Abstract Syntax Tree

Contextual analysis:

• Scope checking: verify that all applied occurrences of identifiers are declared

• Type checking: verify that all operations in the program are used according to their type rules.

Annotate AST:• Applied identifier occurrences => declaration• Expressions => Type

Page 8: Languages and Compilers (SProg og Oversættere)

8

2) Contextual Analysis -> Decorated AST

Program

LetCommand

SequentialDeclaration

n

Ident Ident Ident Ident

SimpleT

VarDecl

SimpleT

VarDecl

Integer c Char c ‘&’ n n + 1

Ident Ident Ident OpChar.Lit Int.Lit

SimpleV

Char.Expr

SimpleV

VNameExp Int.Expr

AssignCommand BinaryExpr

SequentialCommand

AssignCommand

:char

:char

:int

:int

:int :int

Page 9: Languages and Compilers (SProg og Oversættere)

9

Contextual Analysis

Finds scope and type errors.

AssignCommand

:char

Example 1:

:int

***TYPE ERROR (incompatible types in assigncommand)

Example 2:

foo

Ident

SimpleV

foo not found

***SCOPE ERROR: undeclared variable foo

Page 10: Languages and Compilers (SProg og Oversættere)

10

3) Code Generation

• Assumes that program has been thoroughly checked and is well formed (scope & type rules)

• Takes into account semantics of the source language as well as the target language.

• Transforms source program into target code.

Code Generation

Decorated Abstract Syntax Tree

Object Code

Page 11: Languages and Compilers (SProg og Oversættere)

11

3) Code Generation

let var n: integer; var c: charin begin c := ‘&’; n := n+1end

PUSH 2LOADL 38STORE 1[SB]LOAD 0LOADL 1CALL addSTORE 0[SB]POP 2HALT

n

Ident Ident

SimpleT

VarDecl

Integer

address = 0[SB]

Page 12: Languages and Compilers (SProg og Oversættere)

12

A somewhat more complex compiler