Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
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.
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.
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.
Aim:
To write a program to write a three address code and quadruples for the given statements.
Algorithm:
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:
Quadraples
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:
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
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:
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.
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
1. FOR LOOP
2. WHILE LOOP
3. DO WHILE LOOP
4. EXIT
Enter the choice:1
Result is 120
1. FOR LOOP
2. WHILE LOOP
3. DO WHILE LOOP
4. EXIT
Enter the choice:2
Result is 120
1. FOR LOOP
2. WHILE LOOP
3. DO WHILE LOOP
4. EXIT
Enter the choice:3
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.
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
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.
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