这是我用C语言编写的能识别C语言的关键字之类的一个词法分析器,需要有一个程序文件,然后在程序的第一行把#define Filaname “文件名”的那个文件名换成要识别的文件的准确路径,之后就运行就可以了,这个程序是在TurboC 2.0和本网站提供的TC for windows 3.1中调试成功的,如有问题请及时联系我,谢谢!(自认为不足之处就是没有调用函数,一个main弄到底,还有就是没有注释,看不懂的话千万不要硬撑啊!)
#define Filename "1.txt"
#include<string.h>
#include<stdio.h>
main()
{char keyword[32][9]={"auto","break","case","char","const","continue","default","double","do","else","enum","extern","float","for","goto","if","int","long","register","return","short","signed","sizeof","static","struct","switch","typedef","union","unsigned","void","volatile","while"};
char sing[40][5]={"->","++","--","<<",">>","<=",">=","==","!=","&&","||","?:","+=","-=","*=","/=","%=",">>=","<<=","&=","|=","^=","=",".","!","~","-","*","&'","/","%","+","<",">","^","|'","?",":",",",";"};
char *s;
char temp[10][9],ci[9];
char *go,*now;
int i,j,n,k,out,tag1=0,tag2=0,tag3=0;
FILE *p;
if((p=fopen(Filename,"r"))==NULL)
{printf("Can't open the file!");
exit(0);
}
while(!feof(p)) /*while*/
{fgets(s,100,p);
now=s;
go=now;
while((*now)!='\0') /*startwhile*/
{if((*now)==' '||(*now)=='\n')
{now=now+1;
go=now;
continue;
}
if((*now)=='/'&&(*(now+1))=='*') /*startif*/
{printf("Sign: /*\n");
go=go+2;
printf("Instruction: ");
for(;!((*go)=='*'&&(*(go+1))=='/');go++)
printf("%c",*go);
printf("\n");
printf("Sign: */\n");
go=go+2;
now=go;
continue;
} /*endif1 endwhile*/
else if(((*now)=='(')||((*now)==')')||((*now)=='[')||((*now)==']')||((*now)=='{')||((*now)=='}'))
{printf("Sign: %c\n",*now);
tag1++;
now++;
go=now;
continue;
}
else if((*now)=='\'')
{printf("Sign: ");
printf("%c\n",*now);
tag2++;
now++;
go=now;
continue;
}
else if((*now)=='"')
{printf("Sign: ");
printf("%c\n",*now);
tag3++;
now++;
go=now;
continue;
}
else if(((*now)!='"')&&tag3%2==1)
{if((*now)!='%')
{printf("Const Strng: ");
for(;((*go)!='"')&&((*go)!='%');go++)
printf("%c",*go);
printf("\n");
}
else
{printf("Sign: ");
do
{printf("%c",*go);
go++;
}while(((*go)!='"')&&((*go)!='%'));
printf("\n");
}
now=go;
continue;
}
else if((*now)=='&')
{printf("Sign: %c\n",*now);
go++;
now=go;
continue;
}
else if((*now)=='#')
{printf("Headfile: ");
for(;(*go)!=' '&&(*go)!='\n'&&(*go)!='\0';go++)
printf("%c",*go);
printf("\n");
now=go;
continue;
} /*endif2 endwhile*/
else if(((*now)>='a'&&(*now)<='z')||((*now)>='A'&&(*now)<='Z')||((*now)=='_'))
{i=0;
for(n=0;n<32;n++)
{if((*go)==keyword[n][0])
{for(k=0;k<strlen(keyword[n]);k++)
temp[i][k]=keyword[n][k];
for(k=strlen(keyword[n]);k<10;k++)
temp[i][k]='\0';
i++;
}
}
j=i;
if(i==0)
{printf("Identifier: ");
for(;(*go)!=' '&&(*go)!='\n'&&(*go)!='\0';go++)
{if(((*go)>='a'&&(*go)<='z')||((*go)>='A'&&(*go)<='Z')||((*go)=='_')||((*go)>='0'&&(*go)<='9'))
printf("%c",*go);
else break;
}
printf("\n");
now=go;
continue;
}
for(k=0;k<j;k++)
{for(i=0;i<strlen(temp[k]);i++)
{ci[i]=(*go);
go=go+1;
}
for(i=strlen(temp[k]);i<9;i++)
ci[i]='\0';
if((strcmp(ci,temp[k])==0&&(*go)==' ')||(strcmp(ci,temp[k])==0&&(*go)=='\0')||(strcmp(ci,temp[k])==0&&(*go)=='\n'))
{printf("Keyword: ");
for(out=0;out<strlen(temp[k]);out++)
printf("%c",temp[k][out]);
printf("\n");
now=go;
break;
}
else
{go=now;
continue;
}
}
if(k>=j)
{go=now;
printf("Identifier: ");
for(;(*go)!=' '&&(*go)!='\n'&&(*go)!='\0';go++)
{if(((*go)>='a'&&(*go)<='z')||((*go)>='A'&&(*go)<='Z')||((*go)>='0'&&(*go)<='9'))
printf("%c",*go);
else break;
}
printf("\n");
now=go;
continue;
}
} /*endif3 endwhile*/
else if((*now)>='0'&&(*now)<='9')
{printf("Const: ");
for(;(*go)>='0'&&(*go)<='9';go++)
printf("%c",*go);
printf("\n");
now=go;
continue;
} /*endif4 endwhile*/
else
{i=0;
for(n=0;n<40;n++)
{if((*go)==sing[n][0])
{for(k=0;k<strlen(sing[n]);k++)
temp[i][k]=sing[n][k];
for(k=strlen(sing[n]);k<10;k++)
temp[i][k]='\0';
i++;
}
}
j=i;
if(i==0)
{now=now+1;
go=now;
continue;
}
for(k=0;k<j;k++)
{for(i=0;i<strlen(temp[k]);i++)
{ci[i]=(*go);
go=go+1;
}
for(i=strlen(temp[k]);i<9;i++)
ci[i]='\0';
if(strcmp(ci,temp[k])==0)
{printf("Sign: ");
for(out=0;out<strlen(temp[k]);out++)
printf("%c",temp[k][out]);
printf("\n");
now=go;
break;
}
else
{go=now;
continue;
}
}
if(k>=j)
{go=now;
printf("Identifier: ");
for(;(*go)!=' '&&(*go)!='\n'&&(*go)!='\0';go++)
printf("%c",*go);
printf("\n");
now=go;
continue;
}
} /*ednif5 endwhile*/
} /*endwhile*/
} /*while*/
fclose(p);
printf("\n");
if(tag1%2==1)
{printf("Error:\n");
printf("Maybe wrong match in \"()\",\"[]\" or \"{}\"!\n");
}
if((tag2%2==1)||(tag3%2==1))
{printf("Error:\n");
printf("Maybe wrong match in \" ' \" or \" \" \"!\n");
}
printf("\nPress Enter key to exit...");
getchar();
}
评论