0% found this document useful (0 votes)
5 views

Compiler 2

The document discusses the phases of a compiler and provides an example. It explains the different phases - lexical analysis, syntax analysis, semantic analysis, intermediate code generation, code optimization, and code generation. It provides the output after each phase for the statement a = b + c - d * 5.

Uploaded by

Sakshi Sonavane
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Compiler 2

The document discusses the phases of a compiler and provides an example. It explains the different phases - lexical analysis, syntax analysis, semantic analysis, intermediate code generation, code optimization, and code generation. It provides the output after each phase for the statement a = b + c - d * 5.

Uploaded by

Sakshi Sonavane
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

61Compiles Semaster-6 Topper's Solut

Temp 3=Temp 2 * Temp 1 MOVE F REG, Temp 2


MULF REG, Temp 1
MOVE F REG, Temp 3

ID1=P+Temp3 MOVE F REG, P


ADDF REG, Temp 3
MOVE F REG, ID1

Here FFloating Point Arithmetic and REG Register to store value.

Q41 Explain the different phases of compiler. Illustrate the output after each phae.
ase
forthe following statement: a = b+c-d*5

Ans: [10M - May1

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 1 d * I n tto Real (5)

Temp 2 b + c

a =Temp 2- Temp 1

V) Code Generation:
b+C-d *5 is given below:
Assembly codefor a
=

Intermediate Code Assembly Code

Temp 1 = d * Int to Real (5) MOVE F REG, d

MULF REG, Int to Real (5)


MOVE F REG, Temp1

Temp 2 b+C MOVE F REG, b


ADD F REG, C
MOVE F REG, Temp 2

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

Q11 Interpreter v/s compiler

Ans:

INTERPRETERV/S COMPILER:
Table 6.1 shows the difference between Interpreter and Compiler.

Table 6.1: Difference between Interpreter and Compileer.

Interpreter Compiler
Interpreter takes single instruction as input. Compiler takes entire program as input.

| No intermediate code is generated.


Intermediate code is generated.
Memory Requirement is less.
Memory Requirement is more.
Errors are
displayed for every instruction Errors are
displayed after entire program is
1nterpreted. checked.

Execution Time is larger. Execution Time is less.


t does not
creates an object file. It creates an
object file.
Software cost is less.
Software cost is high.
It
pertorms conversion directly.
a
It
pertorms the static conversion.
Size of
interpreter is small.
Size of compiler is large.
Code Size of
interpreter is small.
Code Size of
Every time higher level compiler is nuch larger.
program is converted
into lower level Program need not be
program. compiled every time
Example: BASIC.
Example: CCompiler

Page 52 of 12
71LebnlPbnaltyris Semester -66 Topper's Solations
CHAPTER -7: LEXICAL ANALYSIS
Q LEX.

Ans: [2M-May15, Deci6 &Dec17]


LEX
LEX Stands for Lexical Analyzer Generator.
It is commonly used with YACC Parser Generator.

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

Following tokens are generated


Y 30

9 Figure 7.1 shows the LEX.


Tokewstds X,,2
Input Program Stream of Tokens op t,x
LEX Do. 30
Figure 7.1: LEX.

Q2] What is the role of an automate in compiler design


Q31 What is role of finite automata in compiler theory?
Ans:
[Q21 5M-Mayi6] &[Q315M -Dec16]
FINITE AUTOMATA:
1. Lexical Analyzer is used to scan the source program from left to right
It then
separates the source program into tokens.
Thus the lexical analyzer stores the input in a buffer.
It builds the
regular expression for coresponding tokens.
From this regular
expressions, finite automata's are build.
Finite Automata are used as the mathematical model that can be used to recognize the regular
expressions.
Paqe 53 of t
71Lecieal Anatysis
Semester-b
Soluti on
machine.
automata is a state
1 Finite state accordingly.
and changes its
string of symbols as input
8. It takes a
the pattern of
the lexeme to fy the
identify the tokens
ti

are built to match


9. The regular expressions can be created.
tokens the regular expressions
In order to recognize
10. converted into the equivalent NFA.
then can be
11. These regular expressions
be converted to DFA.
12. The NFA then can

lexical analysis is done using the finite au


Input string is read character by character and omat
13.
valid tokens can be generated.
so that
finite automata in compiler.
14. Figure 7.2 shows the role of

Input Buffer

LEXEME

Finite Automata

NFANFA is built H DFA Generated

Pif-Non-deienmnesic
Finite atemtio Patterns
PA De jtzmin fJfic finiHe Pattern Matching
Ciome40 Algorithm

Tokens

Figure 7.2: Role of finite automata in


compiler.
(Q4) Write a note on: Input buffering scheme of lexical
analyzer
Ans:
L5M - May1

INPUT BUFFERING SCHEME:


Lexicalanalyzer reads the source program
character by character from
Reading the input character by secondary storage.
character from the
3. Therefore, a block of data is first read secondary storage is costly.
into a buffer and then scanned
This known as input
is by the lexical er.
buffering. analy
The lexical analyzer scans
the characters of the source
6. Because of large amount program one at a
of time can time to
be
consumed scanning discove
techniques have been developed to characters, ering

characte.
reduce the aniount
of overhead required
speciali1Zed Du
to inpul
process a
Paaa t

You might also like