0% found this document useful (0 votes)
84 views33 pages

Compiler Construction Lab Manual

book

Uploaded by

Muhammad Shakeel
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
84 views33 pages

Compiler Construction Lab Manual

book

Uploaded by

Muhammad Shakeel
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 33

The University of Faisalabad

Department of Computational Sciences

Compiler Construction– 3(2-1)

Subject Code: CS-411

LAB MANUAL

Subject Instructor: Sobia Riaz

Prepared By: Aneeha Shabbir


Contents
Sr. No. Lab No. Topic Page No.
String operations: Uppercase
1 Lab - 1 1

String operations: Lowercase


2 Lab - 2 3
String operations: Integer
3 Lab - 3 5
String operations: Vowel
4 Lab - 4 7
String operations: Consonant
5 Lab - 5 9
Token Generations
6 Lab - 6 11
Symbol Table
7 Lab - 7 13
Three Address code
8 Lab - 8 18
Directed Acyclic Graph
9 Lab - 9 20

Three Address code II


10 Lab - 10 22

Basic Blocks
11 Lab - 11 23

Flow Graph
12 Lab - 12 25

Data Structure for Symbol Table


13 Lab - 13 27
Practice
14 Lab - 14 29
Practice
15 Lab - 15 30

16 Lab - 16 Quiz 31
Lab No. 01: String operations: Uppercase

Learning Objectives / Outcomes:


After completion of this Lab, students will be able:

 To understand the concept of string operations uppercase.

Lab Work:
Program:

1
Output:

2
Lab No. 02: String operations: Lowercase

Learning Objectives / Outcomes:


After completion of this Lab, students will be able:

 To understand the concept of string operations lowercase.

Lab Work:
Program:

3
Output:

4
Lab No. 03: String operations: Integer

Learning Objectives / Outcomes:


After completion of this Lab, students will be able:

 To understand the concept of string operations integer.

Lab Work:
Program:

5
Output:

6
Lab No. 04: String operations: Vowel

Learning Objectives / Outcomes:


After completion of this Lab, students will be able:

 To understand the concept of string operations vowel.

Lab Work:
Program:

7
Output:

8
Lab No. 05: String operations: Consonant

Learning Objectives / Outcomes:


After completion of this Lab, students will be able:

 To understand the concept of string operations consonant.

Lab Work:
Program:

9
Output:

10
Lab No. 06: Token Generations

Learning Objectives / Outcomes:


After completion of this Lab, students will be able:

 To understand the concept of Token Generations.

Lab Work:
Program:

11
Output:

12
Lab No. 07: Symbol Table

Learning Objectives / Outcomes:


After completion of this Lab, students will be able:

 To understand the concept of Symbol Table .

Lab Work:
ALGORITHM:

1. Start the Program.

2. Get the input from the user with the terminating symbol ‘$’.

3. Allocate memory for the variable by dynamic memory allocation function.

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.

6. The steps are repeated till”$”is reached.

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.

8. Stop the program.

13
Code:

#include<stdio.h>

#include<conio.h>

#include<malloc.h>

#include<string.h>

#include<math.h>

#include<ctype.h>

void main()

int i=0,j=0,x=0,n,flag=0; void *p,*add[15];

char ch,srch,b[15],d[15],c;

//clrscr();

printf("expression terminated by $:");

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++;

printf("the symbol is to be searched\n");

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)

printf("symbol not found\n");

//getch();

16
Output:

17
Lab No. 08: Three Address code

Learning Objectives / Outcomes:


After completion of this Lab, students will be able:

 To understand the concept of 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

Learning Objectives / Outcomes:


After completion of this Lab, students will be able:

 To understand the concept of 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

Learning Objectives / Outcomes:


After completion of this Lab, students will be able:

 To understand the concept of 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

Learning Objectives / Outcomes:


After completion of this Lab, students will be able:

 To understand the concept of 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

Learning Objectives / Outcomes:


After completion of this Lab, students will be able:

 To understand the concept of Flow Graph.

Lab Work:
Step-03:

The flow graph is-

25
To gain better understanding about these Miscellaneous Problems,

26
Lab No. 13: Data Structure for Symbol Table

Learning Objectives / Outcomes:


After completion of this Lab, students will be able:

 To understand the concept of 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

Learning Objectives / Outcomes:

Lab Work:

29
Lab No. 15: Practice

Learning Objectives / Outcomes:

Lab Work:

30
Lab No. 16: Quiz

Learning Objectives / Outcomes:

Lab Work:

31

You might also like