COP4020

Coding

Languages

Compiler phases

Prof. Xin Yuan

Overview

Compiler stages

10/2/2013

Lexical examination

Syntax evaluation

Semantic research

Intermediate (machine-independent) code technology

Intermediate code optimization

Goal (machine-dependent) code generation

Target code optimization

COP4020 Early spring 2013

two

Source program with macros

A typical compilation process

Preprocessor

Source program

Compiler

Target assembly plan

Try g++ with –v, -E, -S flags

on linprog.

assembler

Relocatable machine code

linker

Absolute machine code

10/2/2013

COP4020 Early spring 2013

three or more

What exactly compiler?

A program that reads a plan written in one language

(source language) and translates that into an equivalent

program in one other language (target language).

Two components

Understand the program (make sure it truly is correct)

Rewrite the program in the target terminology.

Traditionally, the original source language can be described as high level language

and the target language is actually a low level terminology (machine

code).

Source

system

compiler

Focus on

program

Error message

10/2/2013

COP4020 Early spring 2013

5

Compilation Phases and Goes

Compilation of a program proceeds by using a fixed

series of phases

Each period use an (intermediate) form of the program produced by an early on phase

Succeeding phases operate on lower-level code representations

Every phase may consist of numerous passes over the

program representation

10/2/2013

Pascal, FORTRAN, C 'languages' designed for one-pass

compilation, which will explains the advantages of function representative models Single-pass compilers need fewer memory to use

Java and ADA will be multi-pass

COP4020 Spring 2013

5

Compiler Front- and Back-end

Summary syntax tree or

other intermediate contact form

Source system (character stream)

Scanner

(lexical analysis)

MachineIndependent Code

Improvement

Parser

(syntax analysis)

Parse tree

Back

synthesis

Front

analysis

Bridal party

Semantic Analysis

and Advanced

Code Generation

Abstract format tree or

other advanced form

10/2/2013

Modified more advanced form

Target Code

Era

Assembly or perhaps object code

Machine-Specific

Code Improvement

Revised assembly or perhaps object code

COP4020 Planting season 2013

6th

Scanner: Lexical Analysis

Lexical evaluation breaks up a plan into tokens

Collection characters into non-separatable models (tokens)

 Changing a stream to characters into a stream of tokens

software gcd (input, output);

va i, t: integer;

start

read (i, j);

while i j carry out

if i > j i then: = i - t else t: = t - i actually;

writeln (i)

end.

system

var

go through

i

then simply

: =

)

10/2/2013

gcd

i

(

i

i

end

(

,

i

t

: =

.

insight

j

,

do

i

i actually

,

:

j

in the event that

;

output

integer

)

i actually

j

writeln

COP4020 Early spring 2013

)

;

;

>

else

(

;

start

while

m

j

my spouse and i

7

Scanning device: Lexical Analysis

What kind of errors can be through lexical analyzer?

A sama dengan b + @3;

10/2/2013

COP4020 Spring 2013

almost eight

Parser: Format Analysis

Checks whether the token stream meets the

grammatical specs of the vocabulary and

builds the syntax tree.

A syntax mistake is made by the compiler when the plan

does not satisfy the grammatical specs.

For grammatically correct software, this phase generates an

internal portrayal that is simple to manipulate in later phases 

Typically a syntax tree (also known as parse tree).

A grammar of a programming language is usually

described with a context cost-free grammer, which in turn also defines

the composition of the parse tree.

10/2/2013

COP4020 Planting season 2013

being unfaithful

Context-Free Grammars

A context-free grammar defines the format of a coding

language

The syntax specifies the syntactic categories for language constructs 

Transactions

...