Sei sulla pagina 1di 11

Design Problem: - 1

System Programming

CSE 368

Ques.: - Implement all the steps of a compiler for the


following program module.

while (A<C AND B>D OR E>F)


{
If (A==1) Call ADD (A+B*-C, -D- -F*-G)
Else
W [I, J] = X [I, J] + Y[W[k, L]] + Z[I+J]
U [I, J, K] = V [I, J, K, L]
}

Expectations:

1. Find out all the tokens generated by lexical analyzer.


2. Perform the syntax tree for last two operations (else
statements) used in program.
3. Perform the semantic analysis over the syntax.
4. Evaluate intermediate code (3-address code) for the
given loop.
5. Optimize the code in terms of memory allocation.
Solution:
1. Find out all the tokens generated by lexical analyzer.
Lexical analyzer is basically used for the analysis of keywords, identifiers
using a table called symbol maintenance table. Its classification depends on
the source language that we used for the implementation of all the items
produced by the lexical analyzer. Analyzer is basically used for the creation
or development of the descriptor i.e. lexical. Token basically consist of two
parts i.e. class code and number in class. Class code is used to address the
class to which a lexical unit belongs. Number in the class is the entry number
of the lexical unit in the relevant table.

According to the question All elements are divided into following


categories:-

a) Identifier-it basically uses id as a symbol or token class code for the


identifier. Numbers can be used as per our requirements.

So, here are all the identifiers and all the corresponding tokens used for
them.

S. NO. TOKEN

I.A id#1

II.B id#2

III.C id#3

IV.D id#4

V .E id#5

VI.F id#6

VII. G id#7

VIII .W id#8

IX .X id#9

X. Y id#10

XII . Z id#11

XIII .U id#12

XIV .V id#13
XV . I id#14

XVI .J id#15

XVII.K id#16

XVII.L id#17

b).keywords-it is a name that has a special meaning and functionality. It can


be also used to represent symbol or as an operator. In this the class code is a
key.

SR NO TOKEN

I .if key#1

II .while key#2

III. ADD key#3

IV. else key#4


c.) Operator- Their main purpose is to compare between the various
variables and identifiers .they are represented by the symbol (op)

SR NO TOKEN

I .< op#1

II. > op#2

III. AND op#3

IV. OR op#4

V. + op#5

VI. - op#6

VII. * op#7

VIII.== op#8

d.) Special symbol- These are special notations like array parameter etc…
the class code is (ss).

SR NO TOKEN

I.( ss#1

II.) ss#2

III .[ ss#3

IV.] ss#4

V., ss#5
Expectation2:- Perform the syntax tree for last two operations (else
statements) used in program.

Solution: A)
B)
Expectation3:- Perform the semantic analysis over the syntax.

Solution: It is a perception that semantic analysis is always depends on the


syntactic but it is not so it also provides the info. About the syntax provided.
Main purpose of semantic is to do the generation of the intermediate code
from the source language. And it is also used to define an action that has to
be implemented on the source statement. Semantic analysis not only
determines the meaning of the parse tree that has been created by the help
of syntax analysis but it also adds info. To the tables these tables and
sequences results in the production of their in the analysis phase.

Node # Semantic action

(1) L.place = i
(2) E.place = i
(3) Elist.array = X
Elist.place= i

Elist.dim=1

(4) L.place = j
(5) E.place= j
(6) M=2 , t1 (newtemp) t1=i*d2
Elist.place = t1 t1=t1+j

Elist.dim =m

Elist.array = X

(7) L.place = t2 (newtemp) t2= C(X)


L.offset = t3 (newtemp) t3=t1*w

(8) L.place = i

(9) E.place = i

(10) L.place=j

(11) E.place = j

(12) t4 (newtemp ) t4 = i+j

(13) Elist.array = Y

Elist.place = t4

Elist.dim =1
(14) L.place = k
(15) E.place = k
(16) m=2 , t5 (newtemp) t5=t4*d4
Elist.array = Y

Elist.place = t5 t5=t5 + k

Elist.dim=2

(17) t6 (newtemp) t6= C(Y)


t7 (newtemp) t7= t5*w

(18) t8 (newtemp) t8=t6[t7]

(19) L.place = z

L.offset= NULL

(20) L.place= z

(21) t9 (newtemp) t9 = t8 + z

(22) t2[t3] := t9

Expectation4:- Evaluate intermediate code (3-address code) for the given


loop.

Solution: it is an activity performed by the analyzer for the generation of


the code .but to d this first we have to understand the architecture of the
target language. It helps in understanding of the complete knowledge of all
the instructions and addressing modes that has been used in the target
computer.

We have three steps to generate the code:-

1. Allocation of the intermediate result in the memory which leads to a


separate process.

2. Specification of the instruction that has to be used for type


conversion of operations.

3. Apecification of the all the specification modes that has been used
for the variable accessing.
Code generation is as follows:-

1) Compare (<) ( id # 1 ) to (id # 2) ---> (id # 3)

2) Compare (>) (id # 4) to (id # 5) ( id # 6)

3) We are giving comparison , again them, we get ( id # 2)

4) Assignment operator (=) , with (id # 1) to (id # 20) which -( id # 10)

5) ( - ) -minus sign ; negative (id # 2),which is giving another id ( id #


2)

6) Now comes , multiplication, (id # 11) with (id # 4) (id #11)

7) Next step is of addition (id # 1) with (id # 11) which will give (id # 11)

8) Again there is unary operation (negation) from (id # 12) after (id # 18)

9) After this , it is repetition of unary operation (negation) from (id # 14)


to (id # 13)

10) After this we multiply (id # 12) with (id # 13) giving (id # 13)

11) Unary operation – negation (id # 15) (id # 12)

12) After doing all operations , we substrate (id # 12) from (id # 13)

13) Addition of (id # 21 ) with (id # 22) gives (id # 17)

14) Addition of (id # 25) gives (id # 16)  (id # 17)

15) Store the result in (id # 19)

16) Store (id # 19) in (id # 15) which is the result.


Expectation 5:-.optimize the code in terms of memory allocation.

Solution: - Its main concern is about proper usage or proper management of


the memory by the analyzer and to divide memory in order to get memory
optimization. Memory optimization can be done in different ways but before
doing that we must know how to allocate the memory and use it in our code.
So first we will concern about the memory allocation and after that steps to
reduce the memory used by us.

Code optimization is as follows:

Symb type addre


ol ss

A Real 2001

C Real 2002

Temp Real 2003


1

B Real 2004

D Real 2005

temp Real 2006


2
E Real 2007

F Real 2008

temp Real 2009


3

temp Real 2010


4

temp Real 2011


5

temp Real 2012


6

temp Real 2013


7

b1 Real 2014

w[i, j] arra 2015


y

x[i,j] arra 2025


y

temp Real 2026


9

w[i,j] arra 2027


y

x[i,j] arra 2037


y

Potrebbero piacerti anche