Professional Documents
Culture Documents
Assignment - 1: Compiler Design (Assignments)
Assignment - 1: 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
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