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

Assignment For Compiler Construction

Assignment for compiler construction
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views

Assignment For Compiler Construction

Assignment for compiler construction
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

ASSIGNMENT # 04 (CLO-IV) 10 points.

Date of Submission: 20-12-23 B.S.(C.S.)-7-B

I. Code Optimization Techniques.

Q 1. a) Describe all the High Level(Source Code) & Low Level (Target Code) Optimization
Techniques with example in each case.
b) From the following Code Segments, Write the name of the Optimization Technique that
can be applied in each case. And also write the modified code after applying the
optimization technique.
Case 1. Case 2. Case 3. Case 4.
i=0; for ( int j = 0 ; j < n ; j ++) S1 = 4 x i DO I = 1, 100
if (i = = 1) { S2 = a[S1] ARRAY(I) = 2.0 * PI * I
{ x=y+z; S3 = 4 x j
a=x+5; a[j] = 6 x j; S4 = 4 x i ENDDO
} } S5 = n
S6 = b[S4] +
S5
Case 5. Case 6. Case 7.
Auto-decrement MOV R0, index ;; value of index → R0 for(int i=0; i<3; i++)
and auto-increment MUL R0, 2 ;; double value in R0 {
addressing modes. MOV R1, &a ;; address of a → R1 color_map[n+i] = i;
ADD R1, R0 ;; add R0 to R1 }
MOV *R1, 6 ;; constant 6 → address
in R1
Case 8. Case 9. Case 10. Case 11.
X= X+ 0 while (i <= limit-2) int cube(int s)
Or do { return s*s*s; } B=Ax2
X= X*1 {loop code} int main( ) (X)2 = ??
{cout << "The cube
X / 1=?? of 3 is: " << cube(3)
<< "\n";
return 0; }
Q. 2 What are the optimization Pitfalls..
------------------------------------------------------------------------------------------------------------
II. Intermediate Code Generation.

Q.3 For the following grammar:

a) E → E + T | E – T | T
T→ T * F | F
F → ( E ) | id | num

i. Write the attribute grammar/semantic rules/ syntax-directed definition.


ii. Draw Directed Acyclic Graph for the following expression with the help of semantic
rules given in part (i).
a + a * (b – c) + (b – c) * d
iii. Show the steps for the construction of DAG of Fig. in part(ii) provided Node and Leaf
return an existing node, if possible, as discussed above. We assume that entry-a points
to the symbol table entry a, and similarly for the other identifiers.

iv. Three-address code is a linearized representation of a syntax tree or a DAG in which


explicit names correspond to the interior nodes of the graph. Write down the
corresponding three-address code sequence for the DAG of the expression in part (ii).

b) Consider the statement (Example 6.5 ullman book)


do i = i + 1;
while (a[i] < v) ;
Write down the two possible translations of the above statement.
a) symbolic labels.
b) position numbers.
And also describe the difference between the above stated two methods.
--------------------------------------------------------------------------------------------------
Q. 4 Consider the following assignment statements.
i) a = b * - c + b * - c ;
ii) a = - b * (c + d) /e
iii) x = - (a +b) * (c + d) + ( a + b + c)

a) Give Three-address codes for the above assignments.


b) Give Quadruples and triples to implement the above three address codes.
c) Give P-code sequence for the above assignments.
---------------------------------------------------------------------------------
Q. 5 Give the sequence of Three-address code instructions as well as P-code instructions
corresponding to each of the following arithmetic expressions.
a. 2+3+4+5
b. 2+ (3+(4+5))
c. a*b+ a*b*c
d. (x = y = 2) + 3*(x = 4)
e. a[a[i]] = b[i=2]
-----------------------------------------------------------------------------------------
Q. 6: Translate the assignment statements:
1. a = b[i] + c[j]
2. a[i] = b*c – b*d
3. x = f(y+1) + 2
4. x = \*p + &y
5. a = b* - (c – d) + b* - (c – d)
The post-fix notation is : a b c d - U * b c d – U * + =
into data structures for the implementation of three-address code. as well as p-code.
a) A syntax tree.
b) Three-address code.
c) Quadruples.
d) Triples.
---------------------------------------------------------------------------------------------------------
Q. 7: Give the sequence of

a) Three address code and b) P code for factorial c code.

{ sample program in C language that computes factorial}


Read x; { input an integer}
if 0 < x then { do not compute if x < = 0 }
fact := 1;
repeat
fact := fact * x;
x := x-1
until x=0;
write fact { output factorial of x }
end.
Q 8. Give the sequence of

a) Three-address & (b) P-code


corresponding to the following piece of code.
Greatest Common Divisor program }
read u;
read v; { input two integers}
if v = 0 then v:= 0 { do nothing }
else
repeat
temp := v;
v := u - u/v*v; { computes u mod v }
u := temp
until v = 0
end;
write u { output gcd of original u & v}
----------------------------------------------------------------------
III. Run-time Environment ( 3 marks)
Q 9. Draw a possible organization for the run time environment of the following C program.

a) After entry into block A in function f.


b) After entry into block B in function g.

int a[10];
char * s= “hello”;
int f(int i, int b[ ]) void g (char * s) main ( )
{ { char c = s[0]; {
int j=i; B: { int a [5]; int x=1;
A: { int i=j; -------- x= f(x, a);
char c = b[i]; -------- g(s);
---------- } return 0;
---------- } } }
return 0
}
Q 10. Consider the following C- language code fragment.
void f (int x, char c)
{
int a[10];
double y;
……………..
}
a) Show (i.e. draw) the complete activation record for a call to f in the stack-based runtime
environment.
b) Assuming two bytes for integers, four bytes for addresses, one byte for characters, and eight
bytes for double-precision floating point. Calculate the off-set values for all the four
variables.
-------------------------------------------------------------------------------------------------------
Q 11. Consider the C code of the following figure:
int x = 2;
void g(int); /*prototype */

void f(int n)
{
static int x=1;
g(n);
x--; }

void g(int m)
{ int y= m-1;
if (y > 0)
{
f(y);
x --;
g(y);
}}
main( )
{
g(x);
return 0;
}
Draw the stack based run-time environment for the above C program during first,second
and third call to g.
------------------------------------------------------------------------------------------------------------------
Q. 12
Consider the simple recursive implementation of Euclid’s algorithm to compute the greatest
common divisor of two non-negative integers whose code(in C) is given as follows:

# include <stdio.h>

int x,y;

int gcd (int u, int v)


{
if (v = = 0) return u;
else return gcd(v, u % v);
}

main ( )
{
scanf( “%d%d”, &x, &y);
Printf(“%d\n”,gcd(x,y));
return 0;
}
Suppose the user inputs the values 15 and 10 to this program, so that main initially makes the
call gcd(15,10). This call results in a second, recursive call gcd(10, 5) (since 15 % 10 =5), and
this results in a third call gcd(5,0) (since 10%5=0), which then returns the value 5.
Note also that the fp points to the control link of the current activation record, so on the next call
the current fp becomes the control link of the next activation record.
Draw the stack-based runtime environment for the above program.
---------------------------------------------------------------------------------------
Q.13
Consider the following procedure in C syntax:
void f ( char c, char s[10], double r )
{
int * x;
int y[5];
------
}
Using the standard C parameter passing conventions, and assuming the data sizes:
integer = 2 bytes, char = 1 byte , double = 8 bytes , address = 4 bytes, determine the offsets
from the fp of the following, using the activation record structure of the runtime environment:
(1) c (2) s[7] (3) y[2] .

b. Repeat (a) assuming all parameters are passed by value(including arrays).


c. Repeat (a) assuming all parameters are passed by reference.
--------------------------------------THE END ---------------------------------------------------

You might also like