1、c语言词法分析#include#include#define nummax 15 /数的最大位数#define norw 31 /关键字个数char ch,chin; /获取字符int cc,ll,cx; /表示当前 ch字符的位置char line100; /读取行缓冲区char a20; /临时放一个字符char intnumnummax; /临时存放一个整数或浮点数char innumnummax; /临时存放一个常数char wordnorw20; /存放保留字char filename30; /存放文件名char id20; /存放保留字和关键字int num; /常数int err
2、or; /错误char *key0=“ “,“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“,“while“,“main“,“printf“,“scanf“;/保留
3、字数组char key6=' ','(',')','','','','',',','','.'/界符数组char *key1=“ “,“+“,“-“,“*“,“/“,“%“,“,“=“,“=“,“,“|“,“/*运算符表*/FILE *fin,*fout,*fas,*file,*key1_files,*key2_files,*key3_files,*key4_files,*key5_files,*key6_files,*key7_fil
4、es,*key8_files,*key0_files,*key9_files; /文件指针/程序运行出错,退出#define getchdo if(-1=getch() return -1#define getsymdo if(-1=getsym() break;int getch();/*读取一行字符*/int getsym();/*读取一个分词*/void charfile(char *word) /保留字和关键字判断函数int i=0,value=0;static int keynum=0;for(;i='a'a2=0;key1_file(a);getchdo;else
5、if(ch='/'|ch='*')if(ch='/')k=1;while(cc!=ll)ak=ch;k+;getchdo;ak=ch;ak+1=0;getchdo;elsek=1;while(ch!='/')ak=ch;k+;getchdo;ak=ch;ak+1=0;getchdo;key9_file(a);elsea1=0;key1_file(a);else if(ch=','|ch='('|ch=')'|ch=''|ch=''|ch='
6、39;)/界符key6_file(ch);getchdo;else if(ch='“')k=0;ak=ch;getchdo;while(ch!='“')k+;ak=ch;getchdo;ak+1=ch;ak+2=0;key7_file(a);getchdo;else if(ch=39)a0=ch;getchdo;a1=ch;getchdo;a2=ch;a3=0;key8_file(a);getchdo;else /其他字符getchdo;return 0;void init()if (key0_files=fopen(“key0.txt“,“w“)=NULL)/
7、建立保留字表printf(“Error! Can't create file : key0.txt“);return;if (key1_files=fopen(“key1.txt“,“w“)=NULL)/建立运算符表printf(“Error! Can't create file : key0.txt“);return;if (key2_files=fopen(“key2.txt“,“w“)=NULL)/建立分隔符表printf(“Error! Can't create file : key0.txt“);return;if (key3_files=fopen(“key
8、3.txt“,“w“)=NULL)/建立整数表printf(“Error! Can't create file : key0.txt“);return;if (key7_files=fopen(“key7.txt“,“w“)=NULL)/建立字符串常量表printf(“Error! Can't create file : key0.txt“);return;if (key4_files=fopen(“key4.txt“,“w“)=NULL)/建立关键字表printf(“Error! Can't create file : key0.txt“);return;if (ke
9、y5_files=fopen(“key5.txt“,“w“)=NULL)/建立浮点数表printf(“Error! Can't create file : key0.txt“);return;if (key6_files=fopen(“key6.txt“,“w“)=NULL)/建立界符表printf(“Error! Can't create file : key0.txt“);return;if (key8_files=fopen(“key8.txt“,“w“)=NULL)/建立字符常量表printf(“Error! Can't create file : key8.t
10、xt“);return;if (key9_files=fopen(“key9.txt“,“w“)=NULL)/建立注释表printf(“Error! Can't create file : key9.txt“);return;if(fas=fopen(“fas.txt“,“w“)=NULL)printf(“Error! Can't create file : key0.txt“);return;void fclosefile()fclose(key0_files);fclose(key1_files);fclose(key2_files);fclose(key3_files);
11、fclose(key4_files);fclose(key5_files);fclose(key6_files);fclose(key7_files);fclose(key8_files);fclose(key9_files);fclose(fas);void printffile()fin=fopen(“fas.txt“,“r“);while(fscanf(fin,“%c“,fclose(fin);int main()char abs='Q'printf(“* C语言词法分析工具 *n“);printf(“* *n“);printf(“* *n“);printf(“* 命令如
12、下: *n“);printf(“* 0 查看保留字表文件 *n“);printf(“* 1 查看分隔符表文件 *n“);printf(“* 2 查看运算符表文件 *n“);printf(“* 3 查看标识符表文件 *n“);printf(“* 4 查看整数类型常量表 *n“);printf(“* 5 查看浮点类型常量表 *n“);printf(“* 6 查看字符类型常量表 *n“);printf(“* 7 查看字符串类型常量表 *n“);printf(“* 8 查看注释表 *n“);printf(“* 9 查看内部码文件 *n“);printf(“* - *n“);printf(“* Q 退出
13、 *n“);printf(“*n“);printf(“请输入源文件名:“);scanf(“%s“,filename);fin=fopen(filename,“r“);if(fin)init();error=0;cc=ll=0;ch=' 'dogetsymdo;while(!feof(fin);elseprintf(“can't open file!“);printf(“分析完成nn“);printf(“请输入命令!n“);fclosefile();scanf(“%c“,while(abs!='Q'file=fopen(“key0.txt“,“r“);ch
14、in=fgetc(file);while(chin!=EOF)printf(“%c“,chin);chin=fgetc(file);printf(“n*n“);printf(“请输入命令n“);fclose(file);break;case '1':printf(“*nn“);file=fopen(“key6.txt“,“r“);chin=fgetc(file);while(chin!=EOF)printf(“%c“,chin);chin=fgetc(file);printf(“n*n“);printf(“请输入命令n“);fclose(file);break;case
15、9;2':printf(“*nn“);file=fopen(“key1.txt“,“r“);chin=fgetc(file);while(chin!=EOF)printf(“%c“,chin);chin=fgetc(file);printf(“n*n“);printf(“请输入命令n“);fclose(file);break;case '3':printf(“*nn“);file=fopen(“key4.txt“,“r“);chin=fgetc(file);while(chin!=EOF)printf(“%c“,chin);chin=fgetc(file);printf
16、(“n*n“);printf(“请输入命令n“);fclose(file);break;case '4':printf(“*nn“);file=fopen(“key3.txt“,“r“);chin=fgetc(file);while(chin!=EOF)printf(“%c“,chin);chin=fgetc(file);printf(“n*n“);printf(“请输入命令n“);fclose(file);break;case '5':printf(“*nn“);file=fopen(“key5.txt“,“r“);chin=fgetc(file);while
17、(chin!=EOF)printf(“%c“,chin);chin=fgetc(file);printf(“n*n“);printf(“请输入命令n“);fclose(file);break;case '6':printf(“*nn“);file=fopen(“key8.txt“,“r“);chin=fgetc(file);while(chin!=EOF)printf(“%c“,chin);chin=fgetc(file);printf(“n*n“);printf(“请输入命令n“);fclose(file);break;case '7':printf(“*nn
18、“);file=fopen(“key7.txt“,“r“);chin=fgetc(file);while(chin!=EOF)printf(“%c“,chin);chin=fgetc(file);printf(“n*n“);printf(“请输入命令n“);fclose(file);break;case '8':printf(“*nn“);file=fopen(“key9.txt“,“r“);chin=fgetc(file);while(chin!=EOF)printf(“%c“,chin);chin=fgetc(file);printf(“n*n“);printf(“请输入命令n“);fclose(file);break;case '9':printf(“*nn“);file=fopen(“fas.txt“,“r“);chin=fgetc(file);while(chin!=EOF)printf(“%c“,chin);chin=fgetc(file);printf(“n*n“);printf(“请输入命令n“);fclose(file);break;scanf(“%c“,return 0;