Professional Documents
Culture Documents
Compiler 1
Compiler 1
CH.APTER - 6: COMPILERS
Qt] What are the different phases of Compiler? Illustrate compilers internal
representa tion of source program for following statement after each phase
Position= initial+ rate* 60.
Explain the different phases of compiler with suitable example
s: [Ql I 10M- Dec15 & Q2 I 10M-Dec1 7]
COMPILER:
Compiler is a translator that takes source language that is written in high level language as input and
produces assembly language or machine language as output.
FEATURES OF COMPILER:
f.HASES OF COMPILER:
, The process of compilation is very complex and hence it is partitioned into a series of sub-
processes called phases.
r Figure 6.1 shows the phases of con,plier.
Source Program
Lexical Analyzer
Syntax Analysis
Semantic Analysis
System Tables
Enor Handler
Intermediate Code Generation
Code Optimization
Code Generation ,
Target Program
Pa3e 43 offf2.
Semesfer-6
6 l Cumfilers
I) Lexical Analyzer:
am from left to right.
Lexical Analyzer is used to scan the source pro gr
►
It then separates the source program into tokens.
A token is a sequence of characters having a collection meaning.
Tokens are usually separated by blan k s, opera t o rs and special symbols.
Example: Lexical Analysis on statement
X = Y + Z x 30
LJ
11) Syntax Analysis:
It is also known as hierarchical analyzing.
In this phase, the compiler has to determine that each statement in
written as per the gram mer.
,,... Tokens forming a statement are grouped together.
Usually a source statement is represented using a parse tree.
A syntax tre_e is a compressed representation ofa parse tree.
P"_Je 44 o{ff2.
<'Topper'! Solution~
Semester- 6
V) Code Optimization:
► In this phase, the compiler tries to improve the intermediate code.
► It helps to derive smaller and faster running code.
► Therefore for above code obtained in intermediate code generation we generates
Temp 1 =Zx 30
X = Y + Temp 1
fltJ]e 45 o{ff2.
I 't-,
Sm,Mter-6
► __
_ _Example:
For ~!-i::.sc::,:;:nbly_C-odc
__:__ _ _ _ _ _ _ _ _ _..,-:A ----------:,i
Intermediate Code -
1------------------r;MovE r:riEG, z
Tt.•mp l =Zx A MUL F REG, A
MOVE p REG, TemP 1
L-------------rwrv10VE F REG, Y
X= y + Temp 1 ADD F REG, TernP 1
MOVf F REG, X
EXAMPLE:
Given:
Position= initial+ rate" 60
Solution:
Let, Position= IDl, Initial= !02 and Rate== ID3
PH,<\SES OF COMPILER:
I) Lexical Analvsis:
Tokens for Position= initial +rate* 60 are:
*
+
=
VJ Code Optimization:
I • I • • I I• Assembly Code
Temp 1 = rD3 * Int to Real (60) MOVE F REG, ID3
MUL F REG, lnt to Real (60)
MOVE F REG, Temp 1
MOVE F REG, 1D2
ADD P RF.G, Temp 1
MOVE F REG, 101
Pa!Je 47 of112.