Sei sulla pagina 1di 26

Compiler Design Lab Manual (2013 Regulation)

1. Implementation of Symbol Table

Aim:
To write a C program to implement the symbol table.
Algorithm:
 Get the input file ip1.c that stores the declaration of variables.
 Read the character one by one from the file, and identify the data type of variables, variable
name.
 Display the result for each variable with data type, size and variable name.
Program:
#include <stdio.h>
#include <conio.h>
void main()
{
char c[25],typ[10],ch[15],var[10];
int siz,i,j=0,n;
int *p;
FILE *fp;
clrscr();
fp=fopen("ip1.c","r");
printf("\nDatatype\tSize\tIdentifiers");
printf("\n------------------------------");
while(!feof(fp))
{
fscanf(fp,"%s",c);
if((strcmp(c,"int")==0)||(strcmp(c,"float")==0)||(strcmp(c,"char")==0))
{
strcpy(typ,c);
if(strcmp(typ,"int")==0)
siz=sizeof(int);
else if(strcmp(typ,"float")==0)
siz=sizeof(float);
else if(strcmp(typ,"char")==0)
siz=sizeof(char);
}
else
{
strcpy(ch,c);
n=strlen(ch);
for(i=0;i<n;i++)
{
if(ch[i]!=',' && ch[i]!=';')
{
var[j]=ch[i];
j++;
}
else
{
var[j]='\0';
printf("\n%s\t\t%d\t%s\t",typ,siz,var);
j=0;
}
}
}
}
fclose(fp);
getch();
}
Input:
Ip1.c
int a,b,c;
float total;
char name;
Output:
Datatype Size Identifiers
------------------------------
int 2 a
int 2 b
int 2 c
float 4 total
char 1 name

Result:

Thus the program for Implementation of symbol table is executed and the result is verified.

2. Implementation of lexical Analyzer in C


Aim
To implement the lexical analyzer using C language
Algorithm
1) Start the program
2) Get the input file “a.txt” that contains the source language.
3) Read the input file and get the character one by one.
4) If the character is an operator or special symbols like +, -, *, /, <, >, {, }, (, ) then display
those symbols as a token.
5) If it is alphabet, check the next characters until it is an operator or special symbol then
display it.
6) Stop the program
Program
#include<stdio.h>
#include<conio.h>
void main()
{
char ch[50],token[50];
int i=0,j=0,k;
FILE *fp;
clrscr();
fp=fopen(“a.txt”,”r”);
for(k=0;k<=10;k++)
token[k]=’\0’;
printf(“\n Tokens are \n”);
printf(“\n ---------------\n”);
while(!feof(fp))
{
fscanf(fp,”%s”,ch);
while(i<strlen(ch))
{
if((ch[i]==40)|| (ch[i]==41)|| (ch[i]==42)|| (ch[i]==43)|| (ch[i]==45)||
(ch[i]==47)|| (ch[i]==59)|| (ch[i]==60)|| (ch[i]==61)||
(ch[i]==62)|| (ch[i]==123)|| (ch[i]==125))
{
printf(“\n\t%s”,token);
for(k=0;k<=10;k++)
token[k]=’\0’;
j=0;
printf(“\n\t%c”,ch[i]);
}
else
{
token[j]=ch[i];
j++;
}
i++;
}
i=0;
getch();
}

Input
a.txt
total=a+b;
while(total>a)
{
a=total;
}

Output
The tokens are
total
=
a
+
b
;
while
(
total
>
a
)
{
a
=
total
;
}

Result:
Thus the c program for implementing lexical analyzer is executed and verified successfully.

3. Lexical Analysis using LEX


Aim:
To write a lex program for lexical analyzer using lex tool in Linux.
Algorithm:
 Start the program
 Create a input file ip.c
 Set the rules for identifiers, numbers, keywords, operators, preprocessor directives, functions,
delimiters and strings
 Open the input file ip.c in read mode
 Identify the tokens and their types in the input file and display it
 Stop the program
Program:
lexical.lex
%{
%}
identifier [_a-zA-Z][_a-zA-Z0-9]*
%%
#.* {printf("\n %s is a preprocessor",yytext);}
void|int|float|double|char|do|while|for|switch|case|break|continue|return|goto {
printf("\n\t%s is a keyword",yytext);}
{identifier}\( {printf("\n Function %s",yytext);}
\; {printf("\n\t %s is a delimiter",yytext);}
\{ {printf("\n block begins %s",yytext);}
\} {printf("\n block ends %s",yytext);}
{identifier}(\[0-9\]*)? {printf("\n\t %s is an identifier",yytext);}
\".*\" {printf("\n\t %s is s string",yytext);}
[0-9]+ {printf("\n\t %s is a number",yytext);}
\+|\-|\*|\/|\% {printf("\n\t %s is an operator",yytext);}
= {printf("\n\t %s is a assignment operator",yytext);}
\<=|\>=|\==|\!= {printf("\n\t %s is a relationla operator",yytext);}
%%

main()
{
FILE *file;
file=fopen("ip.c","r");
yyin=file;
yylex();
}
int yywrap()
{
return 1;
}
Input:
ip.c
#include<stdio.h>
void main()
{
int a=5,b=56,c;
c=a+b;
printf("%d",c);
}
Output:
[itstaff@Telnetserv ~]$ lex lexical.lex
[itstaff@Telnetserv ~]$ cc lex.yy.c -ll
[itstaff@Telnetserv ~]$ ./a.out

#include<stdio.h> is a preprocessor
void is a keyword
Function main()
block begins {
int is a keyword
a is an identifier
= is a assignment operator
5 is a number,
b is an identifier
= is a assignment operator
56 is a number,
c is an identifier
; is a delimiter
c is an identifier
= is a assignment operator
a is an identifier
+ is an operator
b is an identifier
; is a delimiter
Function printf(
"%d" is s string,
c is an identifier)
; is a delimiter
block ends }
Result:
Thus the lexical analyzer program using LEX tool is successfully executed and the result is
verified.
4. Syntax Analysis using YACC
Aim:
To write a yacc program for an infix calculator.
Algorithm:
 Start the program
 Get the input
 Parse the given input
 Check whether the given input is a digit or not
 If it is a digit, perform the specified by the given operator, and display the result
 Else error
 Stop the execution
Program:
%{
#include<ctype.h>
%}
%token DIGIT
%%
line : exp '\n' {printf("The result is %d\n",$1);}
;
exp : term {$$=$1;}
| exp '+' term {$$=$1+$3;}
| exp '-' term {$$=$1-$3;}
;
term :factor {$$=$1;}
|term '*' factor {$$=$1*$3;}
|term '/' factor {$$=$1/$3;}
;
factor :'('exp')' {$$=$2;}
|DIGIT
;
%%
yylex( )
{
int c;
c=getchar();
if(isdigit(c))
{
yylval=c-'0';
return DIGIT;
}
return c;
}
Output:
[itstaff@Telnetserv ~]$ yacc calc.y
[itstaff@Telnetserv ~]$ cc y.tab.c -ly
[itstaff@Telnetserv ~]$ ./a.out
1+2;
The result is 3
[itstaff@Telnetserv ~]$ cc y.tab.c -ly
[itstaff@Telnetserv ~]$ ./a.out
(1+2)*3;
The result is 9
[itstaff@Telnetserv ~]$ cc y.tab.c -ly
[itstaff@Telnetserv ~]$ ./a.out
(1+2;
syntax error
[itstaff@Telnetserv ~]$ cc y.tab.c -ly
[itstaff@Telnetserv ~]$ ./a.out
1/0;
Floating point exception
Result:
Thus the infix calculator program using YACC is executed and the result was verified.

5. Three address Code Generation

Aim:
To write a program to write a three address code and quadruples for the given statements.
Algorithm:

 Get the input expression


 Construct the quadruples for the input expression that contains 4 parameters op,arg1, arg2
and result
 Generate the three address code for the quadruples

Program:
#include<stdio.h>
#include<conio.h>
void func(int);
struct
{
char op,arg1[3],arg2[3],result[3];
}table[50];
char str[15][3],s[3],t[3];
int cnt=0,tcnt=0,j;
void main()
{
char c;
int i=0;
clrscr();
printf("\n\n Enter the expression");
while((c=getch())!=13)
{
putch(c);
if(c=='-'||c=='+'||c=='/'||c=='*'||c=='=')
{
str[cnt++][i]='\0';
str[cnt][0]=c;
str[cnt++][1]='\0';
i=0;
}
else str[cnt][i++]=c;
}
cnt++;
while(cnt>3)
{
for(i=0;i<cnt;i++)
if(str[i][0]=='*'||str[i][0]=='/')
func(i);
for(i=0;i<cnt;i++)
if(str[i][0]=='+'||str[i][0]=='-')
func(i);
}
strcpy(table[tcnt].result,str[0]);
table[tcnt].op=str[1][0];
strcpy(table[tcnt].arg1,table[tcnt-1].result);
strcpy(table[tcnt++].arg2,"");
printf("\n Quadraples\n");
printf("\n\n Pos\tOp\tArg1\tArg2\tRes\n\n");
for(i=0;i<tcnt;i++)
printf("%d\t%c\t%s\t%s\t%s",i,table[i].op,table[i].arg1,table[i].arg2,table[i].result);
printf("\n\n 3 Address code \n");
for(i=0;i<tcnt-1;i++)
printf("%s=%s%c%s\n",table[i].result,table[i].arg1,table[i].op,table[i].arg2);
printf("%s%c%s\n",table[i].result,table[i].op,table[i].arg1);
getch();
}
void func(int i)
{
int j,k;
table[tcnt].op=str[i][0];
strcpy(table[tcnt].arg1,str[i-1]);
strcpy(table[tcnt].arg2,str[i+1]);
sprintf(table[tcnt].result,"t%d",tcnt);
tcnt++;
strcpy(str[i-1],table[tcnt-1].result);
for(j=0;j<2;j++)
{
for(k=i;k<cnt;k++)
strcpy(str[k],str[k+1]);
cnt--;
}
}
Input:

Enter The Expression:a=b*c+b*c-d


Output:

Quadraples

Pos Op Arg1 Arg2 Res

0 * b c t0
1 * b c t1
2 + t0 t1 t2
3 - t2 d t3
4 = t3 a

3 Address Code

t0=b*c
t1=b*c
t2=t0+t1
t3=t2-d
a=t3

Result:

Thus the program for three address code is successfully executed and the result is verified

6. Code Optimization
Aim:
To write a program to implement the code optimization (common sub expression
elimination)
Algorithm:
 Get the input expression
 Construct the quadruples for the input expression that contains 4 parameters op,arg1, arg2
and result
 Generate the three address code for the quadruples
 Identify the common sub expression in the three address code and remove them
 Finally, write the optimized code for the given expression

Program:
#include<stdio.h>
#include<conio.h>
void func(int);
struct
{
char op,arg1[3],arg2[3],result[3];
}table[50];
char str[15][3],s[3],t[3];
int cnt=0,tcnt=0,j;
void main()
{
char c;
int i=0;
clrscr();
printf("\n\n Enter the expression");
while((c=getch())!=13)
{
putch(c);
if(c=='-'||c=='+'||c=='/'||c=='*'||c=='=')
{
str[cnt++][i]='\0';
str[cnt][0]=c;
str[cnt++][1]='\0';
i=0;
}
else str[cnt][i++]=c;
}
cnt++;
while(cnt>3)
{
for(i=0;i<cnt;i++)
if(str[i][0]=='*'||str[i][0]=='/')
func(i);
for(i=0;i<cnt;i++)
if(str[i][0]=='+'||str[i][0]=='-')
func(i);
}
strcpy(table[tcnt].result,str[0]);
table[tcnt].op=str[1][0];
strcpy(table[tcnt].arg1,table[tcnt-1].result);
strcpy(table[tcnt++].arg2,"");
printf("\n Quadraples\n");
printf("\n\n Pos\tOp\tArg1\tArg2\tRes\n\n");
for(i=0;i<tcnt;i++)
printf("%d\t%c\t%s\t%s\t%s",i,table[i].op,table[i].arg1,table[i].arg2,table[i].result);
printf("\n\n 3 Address code \n");
for(i=0;i<tcnt-1;i++)
printf("%s=%s%c%s\n",table[i].result,table[i].arg1,table[i].op,table[i].arg2);
printf("%s%c%s\n",table[i].result,table[i].op,table[i].arg1);
for(i=0;i<tcnt-1;i++)
{
for(j=j+1;j<tcnt;j++)
{
if((table[i].op==table[j].op)&&(strcmp(table[i].arg1,table[j].arg1)==0)&&
(strcmp(table[i].arg2,table[j].arg2)==0))
{
strcpy(s,table[j].result);
strcpy(t,table[i].result);
strcpy(table[j].result,table[i].result);
break;
}
}
}
for(i=0;i<tcnt;i++)
{
if((strcmp(table[i].arg1,s))==0)
strcpy(table[i].arg1,t);
if((strcmp(table[i].arg2,s))==0)
strcpy(table[i].arg2,t);
}
printf("\n Optimized Code\n");
for(i=0;i<tcnt-1;i++)
if(strcmp(table[i].result,table[i+1].result)!=0)
printf("%s=%s%c%s\n",table[i].result,table[i].arg1,table[i].op,table[i].arg2);
printf("%s%c%s\n",table[i].result,table[i].op,table[i].arg1);
getch();
}
void func(int i)
{
int j,k;
table[tcnt].op=str[i][0];
strcpy(table[tcnt].arg1,str[i-1]);
strcpy(table[tcnt].arg2,str[i+1]);
sprintf(table[tcnt].result,"t%d",tcnt);
tcnt++;
strcpy(str[i-1],table[tcnt-1].result);
for(j=0;j<2;j++)
{
for(k=i;k<cnt;k++)
strcpy(str[k],str[k+1]);
cnt--;
}
}
Input:

Enter The Expression:a=b*c+b*c-d


Output:
Quadraples
Pos Op Arg1 Arg2 Res

0 * b c t0
1 * b c t1
2 + t0 t1 t2
3 - t2 d t3
4 = t3 a
3 Address Code

t0=b*c
t1=b*c
t2=t0+t1
t3=t2-d
a=t3
Optimized Code

t0=b*c
t2=t0+t0
t3=t2-d
a=t3
Result:

Thus the program for code optimization is successfully executed and the result is verified

7. Shift Reduce Parsers using Stack allocation


Aim:

To write a C program to implement Shift Reduce Parsers


Algorithm:
 Get the no.of productions and the input string
 Create a stack with ‘$’ symbol at the top of the stack
 Push the current input symbol to the stack
 If the stack that matches with the right side of the production then replace the RHS with
the corresponding non terminal
 Repeat Step 3 & 4 until the input string and stack that contains only ‘$’
 Display the result

Program:
#include<stdio.h>
#include<conio.h>
#include<string.h>
void getinput();
void parsing();
void push(char);
void pop();
char topofstack();
void display();
static int i=0;
int j,top=0,q,noofprod,r;
char ts,x,temp[10],temp1,t,istring[10],stack[10];
struct shift
{
char nonterm;
char RHS[10];
}table[10];
void main()
{
clrscr();
getinput();
printf("\n\t STACK \t INPUT \n");
parsing();
getch();
}
void parsing()
{
int k,flag,l;
char ts,cs;
istring[strlen(istring)]='$';
istring[strlen(istring)+1]='\0';
push('$');
display();
while(1)
{
ts=topofstack();
cs=istring[i];
if((ts=='$')&&(cs=='$'))
{
printf("\n Success");
return;
}
else if((ts=='$')&&(cs!='$'))
{
push(cs);
i=i+1;
display();
}
else if((islower(ts))&&(islower(cs)||cs=='+'||cs=='-'||cs=='*'||cs=='$'))
{
temp1=ts;
for(k=0;k<noofprod;k++)
{
if(temp1==table[k].RHS[0])
{
t=table[k].nonterm;
pop();
push(t);
display();
break;
}
}
}
else if(ts=='+'||ts=='-'||ts=='*')
{
push(cs);
i=i+1;
display();
}
else
{
for(r=1;r<strlen(stack);r++)
temp[r-1]=stack[r];
temp[r-1]='\0';
for(k=0;k<noofprod;k++)
{
if(strcmp(table[k].RHS,temp)==0)
{
t=table[k].nonterm;
for(q=0;q<strlen(temp);q++)
pop();
push(t);
display();
}
}
if(cs!='$')
{
push(cs);
i=i+1;
display();
}
else
{
pop();
display();
}
}
}
}
void getinput()
{
int j,k;
printf("\nOUTPUT");
printf("\nEnter the no of productions -> ");
scanf("%d",&noofprod);
for(k=0;k<noofprod;k++)
{
printf("\nEnter the non terminal -> ");
table[k].nonterm=getche();
printf("\nEnter the RHS -> ");
scanf("%s",table[k].RHS);
}
printf("\nEnter the input symbol -> ");
scanf("%s",istring);
}
void push(char x)
{
stack[top]=x;
top+=1;
}
void pop()
{
top-=1;
stack[top]='\0';
}
char topofstack()
{
return(stack[top-1]);
}
void display()
{
printf("\t%s\t\t",stack);
for(j=i;j<strlen(istring);j++)
printf("%c",istring[j]);
printf("\n");
}

Input:

Enter the no of productions -> 3

Enter the non terminal -> E


Enter the RHS -> E+E

Enter the non terminal -> E


Enter the RHS -> E-E

Enter the non terminal -> E


Enter the RHS -> a

Enter the input symbol -> a+a-a

Output:

STACK INPUT

$ a+a-a$
$a +a-a$
$E +a-a$
$E+ a-a$
$E+a -a$
$E+E -a$
$E -a$
$E- a$
$E-a $
$E-E $
$E $
$ $

Success
Result:

Thus the program for shift reduce parsers is successfully executed and the result is verified

8. Type Checking
Aim:
To write a program to implement Type Checking.

Algorithm:
1. Start the program
2. Perform type checking for the statement
int a,b;
int c=a+b;
3. Get the value of a & b.
4. Find out the type of value of a & b.
5. If the type of a is “int” & b is “int” then type error
If the type of a is “int” & b is “float” then type error
If the type of a is “float” & b is “int” then type error
If the type of a is “float” & b is “float” then no type error
6. Display the result.
7. Stop the program.

Program:
#include <stdio.h>
#include <conio.h>
#include <ctype.h>
#include <string.h>
#include <stdlib.h>
char *type(char [],int);
void main()
{
char a[10],b[10],mess[20],mess1[20];
int i,l;
clrscr();
printf("\n Statement");
printf("\n ---------");
printf("\n int a,b;\n\n int c=a+b;\n");
printf("\n Enter the value of a:");
scanf("%s",a);
l=strlen(a);
printf("\n a is :");
strcpy(mess,type(a,l));
printf("%s",mess);
printf("\n Enter the value of b:");
scanf("%s",b);
l=strlen(b);
printf("\n b is :");
strcpy(mess1,type(b,l));
printf("%s",mess1);
if(strcmp(mess,"int")==0 && strcmp(mess1,"int")==0)
printf("\n No Type Error");
else
printf("\n Type Error");
getch();
}
char *type(char x[],int m)
{
int i;
char mes[20];
for(i=0;i<m;i++)
{
if(isalpha(x[i]))
{
strcpy(mes,"AlphaNumeric");
goto x;
}
else if(x[i]=='.')
{
strcpy(mes,"float");
goto x;
}
}
strcpy(mes,"int");
x:
return mes;
}

Output

Statement
--------------
int a,b;
int c=a+b;
Enter the value of a:11
a is :int
Enter the value of b:1.2
b is :float
Type Error

Result:
Thus the program for implementing type checking is executed successfully and the result is
verified.

9. Global Data Flow & Control Flow Analysis


Aim:
To write a C++ program to implement global data flow control flow analysis for looping
statements.
Algorithm:
Step1: Start the program.
Step2: Declare the funtiond forloop(), whileloop() and dowhileloop().
Step3: Get the value of n.
Step4: Create a menu with
1. FOR LOOP
2. WHILE LOOP
3. DO WHILE LOOP
4. EXIT
Step5: Get the choice. If the choice is 1, then call the function forloop(n). This function which
calculates the factorial for n using for loop and displays the details about flow of execution of for loop.
i.e
a. Take initial value
b. Check the condition
c. If the condition is true, then execute the statements
d. Increment the value
e. Goto b
f. If the condition fails, then terminate from the for loop
Step6: If the choice is 2, then call the function whileloop(n). This function which calculates the
factorial for n using while loop and displays the details about flow of execution of while loop. i.e
a. Check the condition
b. If the condition is true, then execute the statements
c. Goto a
d. If the condition fails, then terminate from the while loop
Step7: If the choice is 3, then call the function dowhileloop(n). This function which calculates the
factorial for n using do while loop and displays the details about flow of execution of do while loop.
i.e
a. Execute the statements
b. Check the condition
c. If the condition is true, then goto a.
d. Otherwise, terminate from the do while loop
Step8: If the choice is 4, then exit the program.

Program:
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
void forloop(int);
void whileloop(int);
void dowhileloop(int);
void main()
{
int choice,n;
printf("Global data Flow Analysis - Looping statements - Factorial");
printf("\n ---------------------------------------------------------");
printf("\n Enter n:");
scanf("%d",&n);
while(1)
{
printf("\n 1. FOR LOOP \n 2. WHILE LOOP \n 3. DO WHILE LOOP \n 4. EXIT");
printf("\n Enter the choice:");
scanf("%d",&choice);
switch(choice)
{
case 1: forloop(n);
break;
case 2: whileloop(n);
break;
case 3: dowhileloop(n);
break;
case 4: exit(0);
}
}
getch();
}
void forloop(int n)
{
int i,fact=1;
printf("\n i value \t condition \t T/F \t statement execution \t increment\n");
printf("\n -------------------------------------------------------------------------\n");
for(i=1;i<=n;i++)
{
printf("\n %d \t\t %d<=%d \t\t TRUE",i,i,n);
fact=fact*i;
printf("\t\t Fact = %d",fact);
printf("\t i=%d",i+1);
}
printf("\n %d \t\t %d<=%d \t\t FAIL \t\t Stop Execution",i,i,n);
printf("\n\n Result is %d\n\n",fact);
}
void whileloop(int n)
{
int i=1,fact=1;
printf("\n i value \t condition \t T/F \t statement execution \t increment\n");
printf("\n -------------------------------------------------------------------------\n");
while(i<=n)
{
printf("\n %d \t\t %d<=%d \t\t TRUE",i,i,n);
fact=fact*i;
printf("\t\t Fact = %d",fact);
printf("\t i=%d",++i);
}
printf("\n %d \t\t %d<=%d \t\t FAIL \t\t Stop Execution",i,i,n);
printf("\n\n Result is %d\n\n",fact);
}
void dowhileloop(int n)
{
int i=1,fact=1;
printf("\n i value \t statement execution \t increment \t Condition Checking \n");
printf("\n -----------------------------------------------------------------------------\n");
do
{
printf("\n %d",i);
fact=fact*i;
printf("\t\t Fact = %d",fact);
i++;
printf("\t\t i=%d \t\t %d<=%d",i,i,n);
}while(i<=n);
printf("\n Condition %d<=%d FAIL. So Stop Execution",i,n);
printf("\n\n Result is %d\n\n",fact);
}

Output

Global data Flow Analysis - Looping statements - Factorial


--------------------------------------------------------------------------
Enter n:5

1. FOR LOOP
2. WHILE LOOP
3. DO WHILE LOOP
4. EXIT
Enter the choice:1

i value condition T/F statement execution increment


-----------------------------------------------------------------------------------
1 1<=5 TRUE Fact = 1 i=2
2 2<=5 TRUE Fact = 2 i=3
3 3<=5 TRUE Fact = 6 i=4
4 4<=5 TRUE Fact = 24 i=5
5 5<=5 TRUE Fact = 120 i=6
6 6<=5 FAIL Stop Execution

Result is 120

1. FOR LOOP
2. WHILE LOOP
3. DO WHILE LOOP
4. EXIT
Enter the choice:2

i value condition T/F statement execution increment


----------------------------------------------------------------------------------
1 1<=5 TRUE Fact = 1 i=2
2 2<=5 TRUE Fact = 2 i=3
3 3<=5 TRUE Fact = 6 i=4
4 4<=5 TRUE Fact = 24 i=5
5 5<=5 TRUE Fact = 120 i=6
6 6<=5 FAIL Stop Execution

Result is 120

1. FOR LOOP
2. WHILE LOOP
3. DO WHILE LOOP
4. EXIT
Enter the choice:3

i value statement execution increment Condition Checking


---------------------------------------------------------------------------------------
1 Fact = 1 i=2 2<=5
2 Fact = 2 i=3 3<=5
3 Fact = 6 i=4 4<=5
4 Fact = 24 i=5 5<=5
5 Fact = 120 i=6 6<=5
Condition 6<=5 FAIL. So Stop Execution

Result is 120

1. FOR LOOP
2. WHILE LOOP
3. DO WHILE LOOP
4. EXIT
Enter the choice:4

Result:
Thus the program for global data flow control flow analysis for looping statements is implemented
successfully and the result is verified.

10. Construction of DAG


Aim:
To write a C program to construct DAG(Directed Acyclic Graph).
Algorithm:
1. Start the program.
2. Get the number of nodes in the syntax tree for the following expression as input
a+a*(b-c)+(b-c)*d
3. For all the nodes, get the value of node number, node value, left child number and left child
value of that node, right child number and right child value of that node using structure.
4. Assign number for all the nodes from root to leaf level by level.
5. Find common sub expression in the given input by checking the node value, left child node
value and right child node value.
6. Mark the common sub expression node number as x & y and Mark the left child and right child
number as a & b.
7. Removing the common sub expression entries from the input and update the node value as
required.
8. Display the result.
9. Stop the program.

Program:
#include<stdio.h>
#include<conio.h>
struct DAG
{
int nodeno;
char nodeval;
int lchildno;
char lchildval;
int rchildno;
char rchildval;
}s[15];
void main()
{
int i,j,n,x,y,a,b;
clrscr();
printf("\n Enter the no.of nodes:");
scanf("%d",&n);
printf("\n Enter the node values, left child & Right child\n");
printf("\n NODE NO NODE VAL LCHILD NO LCHILD VAL RCHILD NO RCHILD VAL");
printf("\n -------------------------------------------------------------------------------------------------\n");
for(i=1;i<=n;i++)
{
scanf("%d%s%d%s%d%s",&s[i].nodeno,&s[i].nodeval,&s[i].lchildno,
&s[i].lchildval,&s[i].rchildno,&s[i].rchildval);
}
for(i=1;i<n;i++)
{
for(j=i+1;j<=n;j++)
{
if((s[i].nodeval==s[j].nodeval && s[i].lchildval==s[j].lchildval &&
s[i].rchildval==s[i].rchildval)&&
(s[i].lchildval!='0' && s[i].rchildval!='0'))
{
x=i;
y=j;
a=s[x].lchildno;
b=s[x].rchildno;
goto label;
}
}
}
label:
for(i=1;i<=n;i++)
{
if(s[i].lchildno==x)
s[i].lchildno=y;
if(s[i].rchildno==x)
s[i].rchildno=y;
}
printf("\n DAG construction");
printf("\n -----------------");
printf("\n NODE NO NODE VAL LCHILD NO LCHILD VAL RCHILD NO RCHILD VAL");
printf("\n -------------------------------------------------------------------------------------------------\n");
for(i=1;i<=n;i++)
{
if(s[i].nodeno!=x && s[i].nodeno!=a && s[i].nodeno!=b)
{
printf("\n%d\t%c\t%d\t%c\t%d\t%c",s[i].nodeno,s[i].nodeval,
s[i].lchildno,s[i].lchildval,s[i].rchildno,s[i].rchildval);
}
}
getch();
}

Input:
Syntax Tree for the given expression a+a*(b-c)+(b-c)*d

Enter the no.of nodes:13


Enter the node values, left child & Right child
NODE NO NODE VAL LCHILD NO LCHILD VAL RCHILD NO RCHILD VAL
-------------------------------------------------------------------------------------------------------
1 + 2 + 3 *
2 + 4 a 5 *
3 * 6 - 7 d
4 a 0 0 0 0
5 * 8 a 9 -
6 - 10 b 11 c
7 d 0 0 0 0
8 a 0 0 0 0
9 - 12 b 13 c
10 b 0 0 0 0
11 c 0 0 0 0
12 b 0 0 0 0
13 c 0 0 0 0

DAG construction
------------------------
NODE NO NODE VAL LCHILD NO LCHILD VAL RCHILD NO RCHILD VAL
-------------------------------------------------------------------------------------------------------
1 + 2 + 3 *
2 + 4 a 5 *
3 * 9 - 7 d
4 a 0 0 0 0
5 * 8 a 9 -
7 d 0 0 0 0
8 a 0 0 0 0
9 - 12 b 13 c
12 b 0 0 0 0
13 c 0 0 0 0

Result:
Thus the program for constructing DAG(Directed Acyclic Graph) is executed
successfully and the result is verified.

11. Target Code Generation


Aim:
To write a C program for target code generation
Algorithm:
 Start the program.
 Create an input file input.txt that contains the optimized three address code (Quadruples)
 For every instruction (in the form of x= y op z) perform the following:
(i) Move the content of y to register Ri
(ii) Generate the following instruction OP z, Ri
(iii) Move the content of Ri to the variable x
 Display the result
 Stop the program

Program

#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<stdlib.h>
struct quad
{
char op[5];
char arg1[10];
char arg2[10];
char result[10];
}quad;
void main()
{
FILE *scr;
int regno=0;
scr=fopen("input.txt","r");
printf("\n Target Code");
printf("\n______\n");
while(!feof(scr))
{
fscanf(scr,"%s%s%s%s",quad.op,quad.arg1,quad.arg2,quad.result);
if(strcmp(quad.op,"=")==0)
printf("\nMOV %s,%s",quad.arg1,quad.arg2);
else
{
printf("\n MOV %s,R%d",quad.arg1,regno);
if(strcmp(quad.op,"+")==0)
printf("\n ADD %s,R%d",quad.arg2,regno);
if(strcmp(quad.op,"-")==0)
printf("\n SUB %s,R%d",quad.arg2,regno);
if(strcmp(quad.op,"*")==0)
printf("\n MUL %s,R%d",quad.arg2,regno);
if(strcmp(quad.op,"/")==0)
printf("\n DIV %s,R%d",quad.arg2,regno);
printf("\n MOV R%d,%s",regno,quad.result);
}
regno=regno+1;
}
getch();
}
Input:

input.txt

+ a b t1
* c t1 t2
= t2 d

Target Code
___________

MOV a,R0
ADD b,R0
MOV R0,t1
MOV c,R1
MUL t1,R1
MOV R1,t2
MOV t2,d

Result:

Thus the program for target code generation is successfully executed and the result is verified

Potrebbero piacerti anche