Compiler 2
Compiler 2
Q41 Explain the different phases of compiler. Illustrate the output after each phae.
ase
forthe following statement: a = b+c-d*5
CONPILER&PHASES OFCOMPILER:
Refer Q1.
EXAMPLE
Given:
a=b-c-d*5
Solution:E
PHASES OF COMPILER:
LexicalAnalysis
Tokensfora =b+c-d*5are:
I SVntax Analysis:
Syntax Tree generated using Syntax Analysis is shown below:
Page 50 of12
Topper's Solutions
Semester -6
61Compilers
Semantic Analysis:
II)
Sematic Analysis is shown below:
Syntax Tree generated using
Int to Real
IV) IntermediateCodeGeneration:
below:
Intermediate Code fora =b+c-d *5 isshown
Temp 1=d*Intto Real (5)
Temp2 =b+c
1
Temp 3 =Temp 2 Temp
-
a= Temp 3
CodeOptimization:
b+c-d *5 is shown below:
Optimized Code for a
=
Temp 2 b + c
a =Temp 2- Temp 1
V) Code Generation:
b+C-d *5 is given below:
Assembly codefor a
=
Page 51 oft2
Semester-6 Topper's Solubio
6 1Compilers
2
MOVE F REG, Tempp
a =
Temp 2- Temp 1 REG, Temp 1
SUB F
MOVE F REG, a
value.
and REG Register to store
Here F Floating Point Arithmetic
EXTRA QUESTION
Ans:
INTERPRETERV/S COMPILER:
Table 6.1 shows the difference between Interpreter and Compiler.
Interpreter Compiler
Interpreter takes single instruction as input. Compiler takes entire program as input.
Page 52 of 12
71LebnlPbnaltyris Semester -66 Topper's Solations
CHAPTER -7: LEXICAL ANALYSIS
Q LEX.
LEX is a program generator designed for lexical processing of character input streams.
LEX is used to scan the source program from left to right.
It then separates the source program into tokens.
A token is a sequence of characters having a collection meaning.
Tokens are usually separated by blanks, operators and special symbols.
Example: Lexical Analysis on statement
X = Y+ Z x 30
Input Buffer
LEXEME
Finite Automata
Pif-Non-deienmnesic
Finite atemtio Patterns
PA De jtzmin fJfic finiHe Pattern Matching
Ciome40 Algorithm
Tokens
characte.
reduce the aniount
of overhead required
speciali1Zed Du
to inpul
process a
Paaa t