Download as pdf or txt
Download as pdf or txt
You are on page 1of 5

Semesfer-6 Topper's Solutions

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:

i,, It should be bug free.


, It should run fast.
, It should generate correct machine code.
, It should be easy & simple to understand.

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

Figure 6.1: Phases of compiler.

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

Following tokens are generated

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.

A syntax tree for expression X = Y + z x 30 is shown below.

III) Semantic Analysis:


Semantic Analysis is for semantic errors.
Type checking is an important aspect of semantic analysis.
Each operator should have suitable operands.
Some example of .
semantic errors are given below:
• 5 x 'ABC" - · · .
• M I ; . . Mult1phcation of an integer and a string is not permitted
u Lphcat1on of 2 • ·
pointer Pl & P2 is not allowed
A syntax tree in semantic a . .
nalys1s for expression X = y + Z x 30. h .
15 s own be1uw.

P"_Je 44 o{ff2.
<'Topper'! Solution~
Semester- 6

Intermediate Code Generation:


IV)
Intermediate Code Generation is us.ed to separates machine independent phases of a compiler

from machine dependent phases of a compiler.


► The intermediate representation can h'ave a variety of form.

,- These forms includes:


• Three Address Code.
• Quadruple.
• Triple.
• Postfix Notabon.
• Syntax Tree.
► Three Address Code for X = Y + Z x A is
Temp 1 =ZxA
Temp 2 = Y + Temp 1
X = Temp 2

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

VI) Code Generation:


Assembly language code is generated in this phase.

It is easy to translate an intermediate instruction into a sequence of machine instructions.

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:

*
+
=

II) Syntax Analysis:


Syntax Tree generated using Syntax Analysis is shown below:

tic Analysis is shown below:


Semesfcr-6

JV) Intermediate Code Generation:


Intermediate Code for Position= initial +rate* 60 is shown below:

Temp 1 = ID3 * Int to Real (60)


Temp 2 = IDZ + Temp 1
ID1 = Temp 2

VJ Code Optimization:

Optimized Code for Position= initial +rate* 60 is shown below:

Temp 1 = ID3 * Int to Real (60)


ID1 = ID2 + Temp 1

VJ) Code Generation:

Assembly code for Position =initial+ rate* 60 is given below:

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

F ➔ Floating Point Arithmetic and REG ➔ Register to store value.

Pa!Je 47 of112.

You might also like