compiler - introd
TRANSCRIPT
8/2/2019 Compiler - Introd
http://slidepdf.com/reader/full/compiler-introd 1/19
COMPILERS
Module 3 begins..
8/2/2019 Compiler - Introd
http://slidepdf.com/reader/full/compiler-introd 2/19
Compiler
Introduction to Compilers
Translator
A translator is a program that takes a program writtenin one programming language as input and produces aprogram in another language as output. If the source
language is a high level language and the object language is alow level language , then such a translator is called acompiler.
Source
Program
Object
Program
8/2/2019 Compiler - Introd
http://slidepdf.com/reader/full/compiler-introd 3/19
Structure of compiler
Lexical Analysis
Tablemanagement
Errorhandling
Codegeneration
Code
optimization
Intermediatecode generation
S yntax Analysis
Source program
Target program (Fig) Phases of compiler
8/2/2019 Compiler - Introd
http://slidepdf.com/reader/full/compiler-introd 4/19
Passes- Portions of one or more phases
- R eads the source program or o/p of previous pass,make transformation and writes o/p into anintermediate file, which then be read by a subsequentpass
Number of passes and grouping of phases, depends
Programming language & machine
Structure of source language Environment where compiler works
8/2/2019 Compiler - Introd
http://slidepdf.com/reader/full/compiler-introd 5/19
Compilers are some times classified into multi pass,Single pass, Load and go, debugging or optimizing compilerdepending on
How they have been constructed
Or what functions they have are supposed to perform
There are two part of compilation
Analysis and S ynthesis
Analysis part breaks the source program into cnstituent piecesand creates intermediate representation of source programThe synthesis part constructs the desired target program from
the intermediate code
8/2/2019 Compiler - Introd
http://slidepdf.com/reader/full/compiler-introd 6/19
Lexical Analysis
- token formation ± interface between the sourceprogram and the compiler
Consider the statement,
IF (5 .EQ. Max) GOTO 100
Here the tokens are IF; (; 5; EQ; MAX; ); GOTO; 100.
8/2/2019 Compiler - Introd
http://slidepdf.com/reader/full/compiler-introd 7/19
Specific strings
-eg: if, semicolon etc
Tokens
Class of strings ± eg: keywords, constants,labels
Worked with parser as a co routine or under theparser
Finding of tokens
8/2/2019 Compiler - Introd
http://slidepdf.com/reader/full/compiler-introd 8/19
The token stream for the above statement is
IF ( [const,341] EQ [id,729] ) GOTO [label,554]
Constant, integer, value=5
variable, integer, value= MAX
Label, value= 100
341
554
729
.
.
.
.
.
.
Fig : S ymbol table
8/2/2019 Compiler - Introd
http://slidepdf.com/reader/full/compiler-introd 9/19
S yntax 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
8/2/2019 Compiler - Introd
http://slidepdf.com/reader/full/compiler-introd 10/19
Each of these interpretations can be represented
by a parse tree which is a tree representation thatexhibits the syntactic structure of the expression.
The rules for a programming language form thesyntactic specification of the language for whichcontext free grammars are used.
8/2/2019 Compiler - Introd
http://slidepdf.com/reader/full/compiler-introd 11/19
expression
expression
expressionexpression
A
expression
/ B * C
(a)
expression
expression
A / B
(b)
expression
expression
* C
expression
8/2/2019 Compiler - Introd
http://slidepdf.com/reader/full/compiler-introd 12/19
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] ;
8/2/2019 Compiler - Introd
http://slidepdf.com/reader/full/compiler-introd 13/19
Statement
while
While statement
Id (A)
do
> <=
&
exp
relation
statementcondition
condition condition
relation
exp expexp reloprelop
Id (B)Const(2)
*
-Id (A)Id (B) expexp
exp exp Const(5)
assignment
location :=
Id(A)
+ expexp
exp
Id(B)
Id(A)
8/2/2019 Compiler - Introd
http://slidepdf.com/reader/full/compiler-introd 14/19
Intermediate Code
L1: if A> B goto L2goto L3
L2: T1 := 2*BT2 := T1 ± 5If A <= T2 goto L4goto L3
L4: A:= A + Bgoto L1
L3 :Intermediate code for while stmt
8/2/2019 Compiler - Introd
http://slidepdf.com/reader/full/compiler-introd 15/19
Code Optimization
This phase tries to apply transformations to the o/pof the intermediate code generatior, in an attempt toproduce a more improved intermediate languagerepresentation
L1: if A> B goto L2goto L3
L2: T1 := 2*BT2 := T1 ± 5
If A <= T2 goto L4goto L3
L4: A:= A + Bgoto L1
L3 :Intermediate code for while stmt
8/2/2019 Compiler - Introd
http://slidepdf.com/reader/full/compiler-introd 16/19
Optimization Techniques
Local Optimization
If A>B goto L2Goto 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
8/2/2019 Compiler - Introd
http://slidepdf.com/reader/full/compiler-introd 17/19
Loop Optimization
It move a computation that produces the same resulteach time around the loop, to a point in the program just
before the loop is entered. Such a computation is calledLoop Invariant.
Code generationThis 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
8/2/2019 Compiler - Introd
http://slidepdf.com/reader/full/compiler-introd 18/19
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 isthe detection and reporting of errors in the sourceprogram.
8/2/2019 Compiler - Introd
http://slidepdf.com/reader/full/compiler-introd 19/19
Errors can be encountered by all of the phases of thecompiler.
For eg,
L A ma y be una ble to proceed b¶se of the next tokenis misspelled
S A m
a y un
a ble to infer
astructure for its i/p b¶se
aof
a syntactic error has occurred««