Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 4

Compiler Design (Assignments)

Assignment – 1
Q.1- Construct the TAC for the following code segment
for (i = 1 to n)
{
j = 1;
while (j <= n)
{
Q = b * c/d;
}
}
Ans-
1) i = 1;
2) if (i < n) go to 4
3) go to next
4) j = 1;
5) if (j <= n) go to 9
6) t1 = i + 1;
7) i = t1;
8) go to 2
9) t2 = b * c;
10) t3 = t2/d;
11) a = t3;
12) t4 = j+1;
13) j = t4;
14) go to 5

Q.2- Construct the TAC for the following code segment?


If (a < b + c*20)
{
Q = a * b – 50;
d = (a(b) + 25);
}
Ans.
1) t1 = c * 20
2) t2 = b + t1
3) if (Q < t2) go to 5
4) go to next
5) t3 = a * b
6) t4 = t3 – 50
7) a = t4
8) t5 = a/b
9) t6 = t5 + 25
10) d = t6
11) go to next

Q.3- Construct the TAC for the following expression


I. w = (A + B) – (C + D) + (A + B + C)
II. a = (a + b * c) ↑(b * c) + b * c ↑ a
Ans.
I.
1) t1 = A + B
2) t2 = C + D
3) t3 = A + B
4) t4 = t3 + C
5) t5 = t1 – t2
6) t6 = t5 + t4
7) w = t6
II.
1) t1 = b * c
2) t2 = a + t1
3) t3 = b * c
4) t4 = t2↑t3
5) t5 = c↑a
6) t6 = b * t5
7) t7 = t4 + t6
8) a = t7

Assignment – 2
Q.1- Generate three address code for the following code segment-
c=0
do
{
if (a < b) then
x++;
else
x--;
c++;
} while (c < 5)
Ans.
1) c = 0
2) if (a < b) goto (4)
3) goto (7)
4) t1 = x + 1
5) x = t1
6) goto (9)
7) t2 = x – 1
8) x = t2
9) t3 = c + 1
10) c = t3
11) if (c < 5) goto (2)
Q.2- Generate three address code for the following code segment-
for (i = 1; i<=10; i++)
{
A [i] = x * 5;
}
Ans.
1) i = 1
2) t1 = x * 5
3) t2 = &a
4) t3 = sizeof(int)
5) t5 = t2 + t4
6) *t5 = t1
7) i = i + 1
8) if i <= 10 goto 2

You might also like