Assignment # 5 - 5
Assignment # 5 - 5
------------------------------------------------------------------------------------------------------------
II. Intermediate Code Generation.
Q.2 For the following grammar:
E → E+ T | E– T | T
T→ T * F | F
F → ( E ) | id | num
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.
Q.3 Give the sequence of Three-address code instructions as well as P-code insrtuctions
corresponding to each of the following arithmetic expressions.
a. 2+3+4+5
b. 2+ (3+(4+5))
c. a*b+ a*b*c
Q.4 Give the sequence of Three-address code instructions as well as P-code insrtuctions
corresponding to each of the following arithmetic expressions.
a. (x = y = 2) + 3*(x = 4)
b. 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.
b) Quadruples.
c) Triples.
---------------------------------------------------------------------------------------------------------
Q. 7 : Give the sequence of
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. This code contains variables that has
its basic operation as follows:
int x = 2;
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; }
------------------------------------------------------------------------------------------
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;
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. During the
third call the runtime environment may be visualized as in figure. Note how each call to gcd adds
a new activation record of exactly the same size to the top of the stack and in each activation
record , the control link points to the control link of the previous activation record.
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.
------------------------------------------------------------------------------------------------------------
Q. 13 (Example 6.5) : Consider the statement
Two possible translations of this statement is shown below. The translation in the figure uses a
symbolic label L, attached to the first instruction. The translation in (b) shows position numbers
for the instructions, starting arbitrarily at position 100. In both translations, the last instruction is
a conditional jump to the first instruction. The multiplication i*8 is appropriate for an array 0f
elements that each take 8 units of space.
From section 2.8.3, l- and r- values are appropriate on the left and right sides of assignment
respectively.
L: t1= i + 1 100: t1 = i +1
i = t1 101: i = t1
t2 = i * 8 102: t2 = i* 8
t3 = a [t2] 103: t3 = a [t2]
if t3 < v goto L 104: if t3 < v goto 100
The choice of allowable operators is an important issue in the design of an intermediate form.
The operator set clearly must be rich enough to implement the operations in the source language.
Operators that are close to machine instructions make it easier to implement the intermediate
form on a target machine. However, if the front end must generate long sequence of instructions
for some source language operations, then the optimizer and code generator may have to work
harder to re-discover the structure and generate good code for these operations.