Vous êtes sur la page 1sur 19

COMPILERS

Module 3

begins

..

Introduction to Compilers

Translator A translator is a program that takes a program written in one programming language as input and produces a program in another language as output. If the source language is a high level language and the object language is a low level language , then such a translator is called a compiler.

Source Program

Compiler

Object Program

Structure of compiler

Source program Lexical Analysis

Syntax Analysis Table management Intermediate code generation Code optimization Code generation Target program (Fig) Phases of compiler Error handling

Passes - Portions of one or more phases - Reads the source program or o/p of previous pass, make transformation and writes o/p into an intermediate file, which then be read by a subsequent pass

Number of passes and grouping of phases, depends Programming language & machine Structure of source language Environment where compiler works

Compilers are some times classified into multi pass, Single pass, Load and go, debugging or optimizing compiler depending on How they have been constructed Or what functions they have are supposed to perform There are two part of compilation Analysis and Synthesis Analysis part breaks the source program into cnstituent pieces and creates intermediate representation of source program The synthesis part constructs the desired target program from the intermediate code

Lexical Analysis - token formation interface between the source program and the compiler Consider the statement, IF (5 .EQ. Max) GOTO 100 Here the tokens are IF; (; 5; EQ; MAX; ); GOTO; 100.

Specific strings- eg: if, semicolon etc

Tokens Class of strings eg: keywords, constants, labels Worked with parser as a co routine or under the parser Finding of tokens

The token stream for the above statement is IF ( [const,341] EQ [id,729] ) GOTO [label,554]

341

Constant, integer, value=5 . . .

554

Label, value= 100 . . . variable, integer, value= MAX

729

Fig : Symbol table

Syntax Analysis Functions Checking the syntax (eg: A+/B ) imposes on tokens a tree like structure

(i) eg: A/B*C it has two interpretations a) divide A by B and then multiply by C b) multiply B by C and then use the result to divide A

Each of these interpretations can be represented by a parse tree which is a tree representation that exhibits the syntactic structure of the expression.

The rules for a programming language form the syntactic specification of the language for which context free grammars are used.

expression

expression

expression

expression

expression

expression

expression

expression

expression

expression

(a)

(b)

Intermediate Code Generation Popularly used method is Three address code

So, T1 := A/B; T2 := T1 *C; Eg : Consider While A>B & A<=2*B-5 do A := A + B Token form, While [id,n1] > [id,n2] & [id,n1] <= [const,n3] * [id,n2] - [const,n4] do [id,n1] := [id,n1] + [id,n2] ;

Statement

While statement

while condition relation exp Id (A) relop >

condition & condition relation exp relop <= exp *

do

statement assignment location := exp + exp Id(B)

exp

exp

Id(A)

exp Id(A)

Id (B) Id (A)

exp - exp exp Id (B)

Const(5)

Const(2)

Intermediate Code

L1: L2:

L4: L3 :

if A> B goto L2 goto L3 T1 := 2*B T2 := T1 5 If A <= T2 goto L4 goto L3 A:= A + B goto L1


Intermediate code for while stmt

Code Optimization This phase tries to apply transformations to the o/p of the intermediate code generatior, in an attempt to produce a more improved intermediate language representation L1: L2: if A> B goto L2 goto L3 T1 := 2*B T2 := T1 5 If A <= T2 goto L4 goto L3 A:= A + B goto L1
Intermediate code for while stmt

L4: L3 :

Optimization Techniques Local Optimization If A>B goto L2 Goto L3 L2: It can be replaced by If A <= B goto L3 Elimination of common subexpressions A:= B+C+D E:= B+C+F can be evaluated as T1 := B+C; A := T1 +D E := T1 +F;

Loop Optimization It move a computation that produces the same result each time around the loop, to a point in the program just before the loop is entered. Such a computation is called Loop Invariant. Code generation This phase converts the intermediate code ito a sequence of m/c insructions. A simple code generator may map the statement A:= B + C into the m/c code sequence Load B Add C Store A

Book keeping Collects all the information about the data objects that appear in the source program.

Error Handling One of the most important functions of a compiler is the detection and reporting of errors in the source program.

Errors can be encountered by all of the phases of the compiler. For eg, LA may be unable to proceed bse of the next token is misspelled SA may unable to infer a structure for its i/p bse a of a syntactic error has occurred

Vous aimerez peut-être aussi