Compiler Construction Lab Manual
Compiler Construction Lab Manual
LAB MANUAL
Basic Blocks
11 Lab - 11 23
Flow Graph
12 Lab - 12 25
16 Lab - 16 Quiz 31
Lab No. 01: String operations: Uppercase
Lab Work:
Program:
1
Output:
2
Lab No. 02: String operations: Lowercase
Lab Work:
Program:
3
Output:
4
Lab No. 03: String operations: Integer
Lab Work:
Program:
5
Output:
6
Lab No. 04: String operations: Vowel
Lab Work:
Program:
7
Output:
8
Lab No. 05: String operations: Consonant
Lab Work:
Program:
9
Output:
10
Lab No. 06: Token Generations
Lab Work:
Program:
11
Output:
12
Lab No. 07: Symbol Table
Lab Work:
ALGORITHM:
2. Get the input from the user with the terminating symbol ‘$’.
4. If the next character of the symbol is an operator then only the memory is
allocated.
5. While reading, the input symbol is inserted into symbol table along with its
memory address.
7. To reach a variable, enter the variable to the searched and symbol table has
been checked for corresponding variable, the variable along its address is
displayed as result.
13
Code:
#include<stdio.h>
#include<conio.h>
#include<malloc.h>
#include<string.h>
#include<math.h>
#include<ctype.h>
void main()
char ch,srch,b[15],d[15],c;
//clrscr();
while((c=getchar())!='$')
b[i]=c; i++;
n=i-1;
printf("given expression:");
i=0;
while(i<=n)
14
printf("%c",b[i]); i++;
printf("symbol table\n");
printf("symbol\taddr\ttype\n");
while(j<=n)
c=b[j]; if(isalpha(toascii(c)))
if(j==n)
p=malloc(c); add[x]=p;
d[x]=c;
printf("%c\t%d\tidentifier\n",c,p);
else
ch=b[j+1];
if(ch=='+'||ch=='-'||ch=='*'||ch=='=')
p=malloc(c);
add[x]=p;
d[x]=c;
15
printf("%c\t%d\tidentifier\n",c,p);
x++;
} j++;
srch=getch();
for(i=0;i<=x;i++)
if(srch==d[i])
printf("symbol found\n");
printf("%c%s%d\n",srch,"@address",add[i]);
flag=1;
if(flag==0)
//getch();
16
Output:
17
Lab No. 08: Three Address code
Lab Work:
Problem 01:
Generate three address code for the following codec= 0
do
{
if (a < b) then
x++;
else
x–;
c++;
} while (c < 5)
Solution-
Three address code for the given code is-
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)
18
Problem-02:
Generate three address code for the following codewhile
(A < C and B > D) do
if A = 1 then C = C + 1
else
while A <= D
do A = A + B
Solution-
Three address code for the given code is-
1. if (A < C) goto (3)
2. goto (15)
3. if (B > D) goto (5)
4. goto (15)
5. if (A = 1) goto (7)
6. goto (10)
7. T1 = c + 1
8. c = T1
9. goto (1)
10. if (A <= D) goto (12)
11. goto (1)
12. T2 = A + B
13. A = T2
14. goto (10)
Problem-03:
Generate three address code for the following codeswitch
(ch)
{
case 1 : c = a + b;
break;
case 2 : c = a – b;
break;
}
Solution-
Three address code for the given code isif
ch = 1 goto L1
if ch = 2 goto L2
L1:
T1 = a + b
c = T1
goto Last
L2:
T1 = a – b
c = T2
goto Last
Last:
19
Lab No. 09: Directed Acyclic Graph
Lab Work:
Problem:
Construct a DAG for the following three address code-
1. a = b + c
2. t1 = a x a
3. b = t1 + a
4. c = t1 x b
5. t2 = c + b
6. a = t2 + t2
20
Solution:
Directed acyclic graph for the given three address code is-
21
Lab No. 10: Three Address code II
Lab Work:
Problem:
Consider the following codeprod
=0;
i=1;
do
{
prod = prod + a[ i ] x b[ i ] ;
i=i+1;
} while (i <= 10) ;
1. Compute the three address code.
2. Compute the basic blocks and draw the flow graph.
Solution:
Part-01
Three address code for the given code isprod
=0
i=1
T1 = 4 x i
T2 = a[T1]
T3 = 4 x i
T4 = b[T3]
T5 = T2 x T4
T6 = T5 + prod
prod = T6
T7 = i + 1
i = T7
if (i <= 10) goto (3)
22
Lab No. 11: Basic Blocks
Lab Work:
Problem:
Consider the following codeprod
=0;
i=1;
do
{
prod = prod + a[ i ] x b[ i ] ;
i=i+1;
} while (i <= 10) ;
1. Compute the three address code.
2. Compute the basic blocks and draw the flow graph.
Part-02:
Step-01:
We identify the leader statements asprod
= 0 is a leader because first statement is a leader.
T1 = 4 x i is a leader because target of conditional or unconditional goto is a leader.
Step-02:
The above generated three address code can be partitioned into 2 basic blocks as
23
24
Lab No. 12: Flow Graph
Lab Work:
Step-03:
25
To gain better understanding about these Miscellaneous Problems,
26
Lab No. 13: Data Structure for Symbol Table
Lab Work:
Global symbol table can be accessed by all the procedures and scope symbol
table.
The scope of a name and symbol table is arranged in the hierarchy structure as
shown below:
int value=10;
void sum_num()
{
int num_1;
int num_2;
{
int num_3;
int num_4;
}
int num_5;
{
int_num 6;
int_num 7;
}
}
Void sum_id
{
int id_1;
int id_2;
{
int id_3;
int id_4;
}
int num_5;}
27
The above grammar can be represented in a hierarchical data structure of symbol tables:
The global symbol table contains one global variable and two procedure names. The name
mentioned in
the sum_num table is not available for sum_id and its child tables
28
Lab No. 14: Practice
Lab Work:
29
Lab No. 15: Practice
Lab Work:
30
Lab No. 16: Quiz
Lab Work:
31