C语言词法分析器_源代码_风君版.txt小时候觉得父亲不简单,后来觉得自己不简单,再后来觉得自己孩子不简单。越是想知道自己是不是忘记的时候,反而记得越清楚。#includestdio.h#includestring.h#includestdlib.hchar*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,_Complex,_Imaginary,union,unsigned,void,volatile,while};/*保留字表*/char*key1[]={,(,),[,],{,},,,;,'};/*分隔符表*/char*key2[]={,+,-,*,/,%,,,==,=,=,!=,!,&&,||,,,~,|,^,&,=,?:,-,++,--,.,+=,-=,*=,/=};/*运算符表*/intxx0[35],xx1[10],xx2[31];inttemp_key3=0,temp_c40=0,temp_c41=0,temp_c42=0,temp_c43=0;/*******初始化函数*******/voidload(){intmm;for(mm=0;mm=34;mm++){xx0[mm]=0;}for(mm=0;mm=9;mm++){xx1[mm]=0;}for(mm=0;mm=30;mm++){xx2[mm]=0;}FILE*floading;if((floading=fopen(key0.txt,w))==NULL){printf(Error!Can'tcreatefile:key0.txt);return;}fclose(floading);/*建立保留字表文件:key0.txt*/if((floading=fopen(key1.txt,w))==NULL){printf(Error!Can'tcreatefile:key1.txt);return;}/*建立分隔符表文件:key1.txt*/if((floading=fopen(key2.txt,w))==NULL){printf(Error!Can'tcreatefile:key2.txt);return;}fclose(floading);/*建立运算符表文件:key2.txt*/if((floading=fopen(key3.txt,w))==NULL){printf(Error!Can'tcreatefile:key3.txt);return;}fclose(floading);/*建立标识符表文件:key3.txt*/if((floading=fopen(c40.txt,w))==NULL){printf(Error!Can'tcreatefile:c40.txt);return;}fclose(floading);/*建立整数类型常量表文件:c40.txt*/if((floading=fopen(c41.txt,w))==NULL){printf(Error!Can'tcreatefile:c41.txt);return;}fclose(floading);/*建立浮点类型常量表文件:c41.txt*/if((floading=fopen(c42.txt,w))==NULL){printf(Error!Can'tcreatefile:c42.txt);return;}fclose(floading);/*建立字符类型常量表文件:c42.txt*/if((floading=fopen(c43.txt,w))==NULL){printf(Error!Can'tcreatefile:c43.txt);return;}fclose(floading);/*建立字符串类型常量表文件:c43.txt*/if((floading=fopen(defination.txt,w))==NULL){printf(Error!Can'tcreatefile:defination.txt);return;}fclose(floading);/*建立注释文件:defination.txt*/if((floading=fopen(output.txt,w))==NULL){printf(Error!Can'tcreatefile:output.txt);return;}fclose(floading);/*建立内部码文件:output.txt*/if((floading=fopen(temp_key1,w))==NULL){printf(Error!Can'tcreatefile:temp_key1);return;}fclose(floading);/*建立保留字临时表文件:temp_key1*/if((floading=fopen(temp_key3,w))==NULL){printf(Error!Can'tcreatefile:temp_key3);return;}fclose(floading);/*建立标识符临时文件:temp_key3*/if((floading=fopen(temp_c40,w))==NULL){printf(Error!Can'tcreatefile:temp_c40);return;}fclose(floading);/*建立整数类型常量临时文件:temp_c40*/if((floading=fopen(temp_c41,w))==NULL){printf(Error!Can'tcreatefile:temp_c41);return;}fclose(floading);/*建立浮点类型常量临时文件:temp_c41*/if((floading=fopen(temp_c42,w))==NULL){printf(Error!Can'tcreatefile:temp_c42);return;}fclose(floading);/*建立字符类型常量临时文件:temp_c42*/if((floading=fopen(temp_c43,w))==NULL){printf(Error!Can'tcreatefile:temp_c43);return;}fclose(floading);/*建立字符串类型常量临时文件:temp_c43*/}/*******保留字及标识符判断函数*******/voidchar_search(char*word){intm,line=0,csi=0;intvalue=0;intvalue2=0;charc,cs[100];FILE*foutput,*finput;for(m=1;m=34;m++){if(strcmp(word,key0[m])==0){value=1;break;}}if(value==1){if(xx0[m]==0){foutput=fopen(key0.txt,a);fprintf(foutput,0\t%d\t\t%s\n,m,word);fclose(foutput);xx0[m]=1;}foutput=fopen(output.txt,a);fprintf(foutput,0\t%d\t\t%s\n,m,word);fclose(foutput);}else{if(temp_key3==0){foutput=fopen(temp_key3,a);fprintf(foutput,%s\n,word);fclose(foutput);temp_key3++;foutput=fopen(key3.txt,a);fprintf(foutput,3\t1\t\t%s\n,word);fclose(foutput);}finput=fopen(temp_key3,r);c=fgetc(finput);while(c!=EOF){while(c!='\n'){cs[csi++]=c;c=fgetc(finput);}cs[csi]='\0';csi=0;line++;if((strcmp(cs,word))==0){value2=1;break;}else{value2=0;c=fgetc(finput);}}fclose(finput);if(value2==1){foutput=fopen(output.txt,a);fprintf(foutput,3\t%d\t\t%s\n,line,word);fclose(foutput);}else{foutput=fopen(temp_key3,a);fprintf(foutput,%s\n,word);fclose(foutput);temp_key3++;foutput=fopen(output.txt,a);fprintf(foutput,3\t%d\t\t%s\n,temp_key3,word);fclose(foutput);foutput=fopen(key3.txt,a);fprintf(foutput,3\t%d\t\t%s\n,temp_key3,word);fclose(foutput);}}}/*******整数类型判断函数*******/voidinta_search(char*word){FILE*foutput,*finput;charc;charcs[100];intcsi=0;intline=0;intvalue2=0;if(temp_c40==0){foutput=fopen(temp_c40,a);fprintf(foutput,%s\n,word);fclose(foutput);temp_c40++;foutput=fopen(c40.txt,a);fprintf(foutput,4\t0\t1\t%s\n,word);fclose(foutput);}finput=fopen(temp_c40,r);c=fgetc(finput);while(c!=EOF){while(c!='\n'){cs[csi++]=c;c=fgetc(finput);}cs[csi]='\0';csi=0;line++;if(strcmp(cs,word)==0){value2=1;break;}c=fgetc(finput);}fclose(finput);if(value2==1){foutput=fopen(output.txt,a);fprintf(foutput,4\t0\t%d\t%s\n,line,word);fclose(foutput);}else{foutput=fopen(temp_c40,a);fprintf(foutput,%s\n,word);fclose(foutput);temp_c40++;foutput=fopen(output.txt,a);fprintf(foutput,4\t0\t%d\t%s\n,temp_c40,word);fclose(foutput);foutput=fopen(c40.txt,a);fprintf(foutput,4\t0\t%d\t%s\n,temp_c40,word);fclose(foutput);}}/*******浮点类型判断函数*******/voidintb_search(char*word){FILE*foutput,*finput;charc;charcs[100];intcsi=0;intline=0;intvalue2=0;if(temp_c41==0){foutput=fopen(temp_c41,a);fprintf(foutput,%s\n,word);fclose(foutput);temp_c41++;foutpu