#include<stdlib.h>
#include<stdio.h>
#include<string.h>
void init()
{
char *key[]={" ","auto","break","case","char","const","continue","default","do","double",
"else","enum","extern","float","for","goto","if","int","long","register",
"return","short","signed","sizeof","static","struct","switch","typedef",
"union","unsigned","void","volatile","while"};
char *limit[]={" ","(",")","[","]","->",".","!","++","--","&","~",
"*","/","%","+","-","<<",">>","<","<=",">",">=","==","!=","&&","||",
"=","+=","-=","*=","/=",",",";","{","}","#","_","'"};
FILE *fp;
int i;
char c;
fp=fopen("d://k.txt","w");
for(i=1;i<=32;i++)
fprintf(fp,"%s/n",key[i]);
fclose(fp);
fp=fopen("d://l.txt","w");
for(i=1;i<=38;i++)
fprintf(fp,"%s/n",limit[i]);
c='"';
fprintf(fp,"%c/n",c);
fclose(fp);
fp=fopen("d://i.txt","w");
fclose(fp);
fp=fopen("d://c.txt","w");
fclose(fp);
fp=fopen("d://output.txt","w");
fclose(fp);
}
char * dtb(char *buf)
{
int temp[20];
char *binary;
int value=0,i=0,j;
for(i=0;buf[i]!='/0';i++)
value=value*10+(buf[i]-48);
if(value==0)
{
binary=malloc(2*sizeof(char));
binary[0]='0';
binary[1]='/0';
return(binary);
}
i=0;
while(value!=0)
{
temp[i++]=value%2;
value/=2;
}
temp[i]='/0';
binary=malloc((i+1)*sizeof(char));
for(j=0;j<=i-1;j++)
binary[j]=(char)(temp[i-j-1]+48);
binary[i]='/0';
return(binary);
}
int find(char *buf,int type,int command)
{
int number=0;
FILE *fp;
char c;
char temp[30];
int i=0;
switch(type)
{
case 1: fp=fopen("d://k.txt","r");break;
case 2: fp=fopen("d://i.txt","r");break;
case 3: fp=fopen("d://c.txt","r");break;
case 4: fp=fopen("d://l.txt","r");
}
c=fgetc(fp);
while(c!=EOF)
{
while(c!='/n')
{
temp[i++]=c;
c=fgetc(fp);
}
temp[i]='/0';
i=0;
number++;
if(strcmp(temp,buf)==0)
{
fclose(fp);
return(number);
}
else
c=fgetc(fp);
}
if(command==1)
{
fclose(fp);
return(0);
}
switch(type)
{
case 1: fp=fopen("d://k.txt","a");break;
case 2: fp=fopen("d://i.txt","a");break;
case 3: fp=fopen("d://c.txt","a");break;
case 4: fp=fopen("d://l.txt","a");
}
fprintf(fp,"%s/n",buf);
fclose(fp);
return(number+1);
}
void cs_manage(char *buffer)
{
FILE *fp;
char *pointer;
int result;
pointer=dtb(buffer);
result=find(pointer,3,2);
fp=fopen("d://output.txt","a");
fprintf(fp,"%s/t/t/t3/t/t/t%d/n",buffer,result);
fclose(fp);
}
void ch_manage(char *buffer)
{
FILE *fp;
int result;
result=find(buffer,1,1);
fp=fopen("d://output.txt","a");
if(result!=0)
fprintf(fp,"%s/t/t/t1/t/t/t%d/n",buffer,result);
else
{
result=find(buffer,2,2);
fprintf(fp,"%s/t/t/t2/t/t/t%d/n",buffer,result);
}
fclose(fp);
}
void er_manage(char error,int lineno)
{
printf("/nerror: %c ,line %d",error,lineno);
}
void scanner()
{
FILE *fpin,*fpout;
char filename[20];
char ch;
int i=0,line=1;
int count,result,errorno=0;
char array[30];
char *word;
printf("/nthe file name:");
scanf("%s",filename);
if((fpin=fopen(filename,"r"))==NULL)
{
printf("cannot open file");
return;
}
ch=fgetc(fpin);
while(ch!=EOF)
{
i=0;
if(((ch>='A')&&(ch<='Z'))||((ch>='a')&&(ch<='z'))||(ch=='_'))
{
while(((ch>='A')&&(ch<='Z'))||((ch>='a')&&(ch<='z'))||(ch=='_')||((ch>='0')&&(ch<='9')))
{
array[i++]=ch;
ch=fgetc(fpin);
}
word=(char *)malloc((i+1)*sizeof(char));
memcpy(word,array,i);
word[i]='/0';
ch_manage(word);
if(ch!=EOF)
fseek(fpin,-1L,SEEK_CUR);
}
else if(ch>='0'&&ch<='9')
{
while(ch>='0'&&ch<='9')
{
array[i++]=ch;
ch=fgetc(fpin);
}
word=(char *)malloc((i+1)*sizeof(char));
memcpy(word,array,i);
word[i]='/0';
cs_manage(word);
if(ch!=EOF)
fseek(fpin,-1L,SEEK_CUR);
}
else if((ch==' ')||(ch=='/t'))
;
else if(ch=='/n')
line++;
else if(ch=='/')
{
ch=fgetc(fpin);
if(ch=='=')
{
fpout=fopen("d://output.txt","a");
fprintf(fpout,"/=/t/t/t4/t/t/t32/n");
fclose(fpout);
}
else if(ch!='*')
{
fpout=fopen("d://output.txt","a");
fprintf(fpout,"//t/t/t4/t/t/t13/n");
fclose(fpout);
fseek(fpin,-1L,SEEK_CUR);
}
else if(ch=='*')
{
count=0;
ch=fgetc(fpin);
while(count!=2)
{
count=0;
while(ch!='*')
ch=fgetc(fpin);
count++;
ch=fgetc(fpin);
if(ch=='/')
count++;
else
ch=fgetc(fpin);
}
}
}
else if(ch=='"')
{
fpout=fopen("d://output.txt","a");
fprintf(fpout,"%c/t/t/t4/t/t/t37/n",ch);
ch=fgetc(fpin);
while(ch!='"')
ch=fgetc(fpin);
fprintf(fpout,"%c/t/t/t4/t/t/t37/n",ch);
fclose(fpout);
}
else
{
array[0]=ch;
ch=fgetc(fpin);
if(ch!=EOF)
{
array[1]=ch;
word=(char *)malloc(3*sizeof(char));
memcpy(word,array,2);
word[2]='/0';
result=find(word,4,1);
if(result==0)
{
word=(char *)malloc(2*sizeof(char));
memcpy(word,array,1);
word[1]='/0';
result=find(word,4,1);
if(result==0)
{
er_manage(array[0],line);
errorno++;
fseek(fpin,-1L,SEEK_CUR);
}
else
{
fpout=fopen("d://output.txt","a");
fprintf(fpout,"%s/t/t/t4/t/t/t%d/t/n",word,result);
fclose(fpout);
fseek(fpin,-1L,SEEK_CUR);
}
}
else
{
fpout=fopen("d://output.txt","a");
fprintf(fpout,"%s/t/t/t4/t/t/t%d/n",word,result);
fclose(fpout);
}
}
else
{
word=(char *)malloc(2*sizeof(char));
memcpy(word,array,1);
word[1]='/0';
result=find(word,4,1);
if(result==0)
er_manage(array[0],line);
else
{
fpout=fopen("d://output.txt","a");
fprintf(fpout,"%s/t/t/t4/t/t/t%d/n",word,result);
fclose(fpout);
}
}
}
ch=fgetc(fpin);
}
fclose(fpin);
printf("/nThere are %d error(s)./n",errorno);
}
main()
{
init();
scanner();
}