error in finding out the lexems and no of lines of a text file in C
Posted
by mekasperasky
on Stack Overflow
See other posts from Stack Overflow
or by mekasperasky
Published on 2010-04-27T10:21:12Z
Indexed on
2010/04/27
10:23 UTC
Read the original article
Hit count: 292
c
#include<stdio.h>
#include<ctype.h>
#include<string.h>
int main()
{
int i=0,j,k,lines_count[2]={1,1},operand_count[2]={0},operator_count[2]={0},uoperator_count[2]={0},control_count[2]={0,0},cl[13]={0},variable_dec[2]={0,0},l,p[2]={0},ct,variable_used[2]={0,0},constant_count[2],s[2]={0},t[2]={0};
char a,b[100],c[100];
char d[100]={0};
j=30;
FILE *fp1[2],*fp2;
fp1[0]=fopen("program1.txt","r");
fp1[1]=fopen("program2.txt","r"); //the source file is opened in read only mode which will passed through the lexer
fp2=fopen("ccv1ouput.txt","wb");
//now lets remove all the white spaces and store the rest of the words in a file
if(fp1[0]==NULL)
{
perror("failed to open program1.txt");
//return EXIT_FAILURE;
}
if(fp1[1]==NULL)
{
perror("failed to open program2.txt");
//return EXIT_FAILURE;
}
i=0;
k=0;
ct=0;
while(ct!=2)
{
while(!feof(fp1[ct]))
{
a=fgetc(fp1[ct]);
if(a!=' '&&a!='\n')
{
if (!isalpha(a) && !isdigit(a))
{
switch(a)
{
case '+':{
i=0;
cl[0]=1;
operator_count[ct]=operator_count[ct]+1;break;}
case '-':{ cl[1]=1;
operator_count[ct]=operator_count[ct]+1;i=0;break;}
case '*':{ cl[2]=1;
operator_count[ct]=operator_count[ct]+1;i=0;break;}
case '/':{ cl[3]=1;
operator_count[ct]=operator_count[ct]+1;i=0;break;}
case '=':{a=fgetc(fp1[ct]);
if (a=='='){cl[4]=1;
operator_count[ct]=operator_count[ct]+1;
operand_count[ct]=operand_count[ct]+1;}
else
{ cl[5]=1;
operator_count[ct]=operator_count[ct]+1;
operand_count[ct]=operand_count[ct]+1;
ungetc(1,fp1[ct]);
}
break;}
case '%':{ cl[6]=1;
operator_count[ct]=operator_count[ct]+1;i=0;break;}
case '<':{
a=fgetc(fp1[ct]);
if (a=='=')
{cl[7]=1;
operator_count[ct]=operator_count[ct]+1;}
else
{ cl[8]=1;
operator_count[ct]=operator_count[ct]+1;
ungetc(1,fp1[ct]);
}
break;
}
case '>':{ ;
a=fgetc(fp1[ct]);
if (a=='='){cl[9]=1;
operator_count[ct]=operator_count[ct]+1;}
else
{ cl[10]=1;
operator_count[ct]=operator_count[ct]+1;
ungetc(1,fp1[ct]);
}
break;}
case '&':{ cl[11]=1;
a=fgetc(fp1[ct]);
operator_count[ct]=operator_count[ct]+1;
operand_count[ct]=operand_count[ct]+1;
variable_used[ct]=variable_used[ct]-1;
break;
}
case '|':{ cl[12]=1;
a=fgetc(fp1[ct]);
operator_count[ct]=operator_count[ct]+1;
operand_count[ct]=operand_count[ct]+1;
variable_used[ct]=variable_used[ct]-1;
break;
}
case '#':{ while(a!='\n')
{
a=fgetc(fp1[ct]);
}
}
}
}
else
{
d[i]=a;
i=i+1;
k=k+1;
}
}
else
{
//printf("%s \n",d);
if((strcmp(d,"if")==0)){
memset ( d, 0, 100 );
i=0;
control_count[ct]=control_count[ct]+1;
}
else if(strcmp(d,"then")==0){
i=0;memset ( d, 0, 100 );control_count[ct]=control_count[ct]+1;}
else if(strcmp(d,"else")==0){
i=0;memset ( d, 0, 100 );control_count[ct]=control_count[ct]+1;}
else if(strcmp(d,"while")==0){
i=0;memset ( d, 0, 100 );control_count[ct]=control_count[ct]+1;}
else if(strcmp(d,"int")==0){
while(a != '\n')
{
a=fgetc(fp1[ct]);
if (isalpha(a) )
variable_dec[ct]=variable_dec[ct]+1;
}
memset ( d, 0, 100 );
lines_count[ct]=lines_count[ct]+1;
}
else if(strcmp(d,"char")==0){while(a != '\n')
{ a=fgetc(fp1[ct]);
if (isalpha(a) )
variable_dec[ct]=variable_dec[ct]+1;
}
memset ( d, 0, 100 );
lines_count[ct]=lines_count[ct]+1;
}
else if(strcmp(d,"float")==0){while(a != '\n')
{ a=fgetc(fp1[ct]);
if (isalpha(a) )
variable_dec[ct]=variable_dec[ct]+1;
}
memset ( d, 0, 100 );
lines_count[ct]=lines_count[ct]+1;
}
else if(strcmp(d,"printf")==0){while(a!='\n')
a=fgetc(fp1[ct]);
memset(d,0,100);
}
else if(strcmp(d,"scanf")==0){while(a!='\n')
a=fgetc(fp1[ct]);
memset(d,0,100);}
else if (isdigit(d[i-1]))
{
memset ( d, 0, 100 );
i=0;
constant_count[ct]=constant_count[ct]+1;
operand_count[ct]=operand_count[ct]+1;
}
else if (isalpha(d[i-1]) && strcmp(d,"int")!=0 && strcmp(d,"char")!=0 && strcmp(d,"float")!=0 && (strcmp(d,"if")!=0) && strcmp(d,"then")!=0 && strcmp(d,"else")!=0 && strcmp(d,"while")!=0 && strcmp(d,"printf")!=0 && strcmp(d,"scanf")!=0)
{
memset ( d, 0, 100 );
i=0;
operand_count[ct]=operand_count[ct]+1;
}
else if(a=='\n')
{
lines_count[ct]=lines_count[ct]+1;
memset ( d, 0, 100 );
}
}
}
fclose(fp1[ct]);
operand_count[ct]=operand_count[ct]-5;
variable_used[0]=operand_count[0]-constant_count[0];
variable_used[1]=operand_count[1]-constant_count[1];
for(j=0;j<12;j++)
uoperator_count[ct]=uoperator_count[ct]+cl[j];
fprintf(fp2,"\n statistics of program %d",ct+1);
fprintf(fp2,"\n the no of lines ---> %d",lines_count[ct]);
fprintf(fp2,"\n the no of operands --->%d",operand_count[ct]);
fprintf(fp2,"\n the no of operator --->%d",operator_count[ct]);
fprintf(fp2,"\n the no of control statments --->%d",control_count[ct]);
fprintf(fp2,"\n the no of unique operators --->%d",uoperator_count[ct]);
fprintf(fp2,"\n the no of variables declared--->%d",variable_dec[ct]);
fprintf(fp2,"\n the no of variables used--->%d",variable_used[ct]);
fprintf(fp2,"\n ---------------------------------");
fprintf(fp2,"\n \t \t \t");
ct=ct+1;
}
t[0]=lines_count[0]+control_count[0]+uoperator_count[0];
t[1]=lines_count[1]+control_count[1]+uoperator_count[1];
s[0]=operator_count[0]+operand_count[0]+variable_dec[0]+variable_used[0];
s[1]=operator_count[1]+operand_count[1]+variable_dec[1]+variable_used[1];
fprintf(fp2,"\n the time complexity of program 1 is %d",t[0]);
fprintf(fp2,"\n the time complexity of program 2 is %d",t[1]);
fprintf(fp2,"\n the space complexity of program 1 is %d",s[0]);
fprintf(fp2,"\n the space complexity of program 2 is %d",s[1]);
if((t[0]>t[1]) && (s[0] >s[1]))
fprintf(fp2,"\n the efficiency of program 2 is greater than program 1");
else if(t[0]<t[1] && s[0] < s[1])
fprintf(fp2,"\n the efficiency of program 1 is greater than program 2 " );
else if (t[0]+s[0] > t[1]+s[1])
fprintf(fp2,"\n the efficiency of program 1 is greater than program 2");
else if (t[0]+s[0] < t[1]+s[1])
fprintf(fp2,"\n the efficiency of program 2 is greater than program 1");
else if (t[0]+s[0] == t[1]+s[1])
fprintf(fp2,"\n the efficiency of program 1 is equal to that of program 2");
fclose(fp2);
return 0;
}
this code basically compares two c codes and finds out the no. of variables declared , used , no. of control statements , no. of lines and no. of unique operators , and operands , so as to find out the time complexity and space complexity of of the two programs given in the text file program1.txt and program2.txt ...
Lets say program1.txt is this
#include<stdio.h>
#include<math.h>
int main ()
{
FILE *fp;
fp=fopen("output.txt","w");
long double t,y=0,x=0,e=5,f=1,w=1;
for (t=0;t<10;t=t+0.01)
{
//if (isnan(y) || isinf(y))
//break;
fprintf(fp,"%ld\t%ld\n",y,x);
y = y + ((e*(1 - (x*x))*y) - x + f*cos(w*0.1))*0.1;
x = x + y*0.1;
}
fclose(fp);
return (0);
}
i havent indented it as its just a text file . But my output is totally faulty . Its not able to find the any of the ouput that i need . Where is the bug in this ? I am not able to figure out as the algorithm looks fine .
© Stack Overflow or respective owner