资源描述:
《c语言词法分析源代码》由会员上传分享,免费在线阅读,更多相关内容在行业资料-天天文库。
1、c语言词法分析源代码char*bl[]={"","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
2、","while"};//分隔符表char*fg[]={"","(",")","[","]","{","}",",",";","'","#","
3、"};//运算符表char*ys[]={"","+","-","*","/","%","<",">","==",">=","<=","!=","!","&&","
4、
5、","<<",">>","~","
6、","^","&","=","?:",".","->","++","--","+=","-=","*=","/=","%="};ints0[35],s1[12],s2[32];intcount_1=0,count_2=0,count_3=0,count
7、_4=0,count_5=0;voidInitialize(){inti;for(i=0;i<=34;i++){s0[i]=0;}for(i=0;i<=11;i++){s1[i]=0;}for(i=0;i<=31;i++){s2[i]=0;}FILE*fp;/*依次创建:保留字内部码文档“baoliuzi.txt”,分隔符内部码文档“fengefu.txt”,运算符内部码文档“yunsuanfu.txt”,标识符内部码文档“bs.txt”,整型常量内部码文档“zx.txt”,浮点型常量内部码文档“fd.txt”,字符型常量内部码文档“zf.txt”,字符串类型常量内部码文档“zc.txt”,注
8、释文档“zs.txt”,源代码内部码文档“code.txt”*/if(((fp=fopen("baoliuzi.txt","w"))==NULL)
9、
10、((fp=fopen("fengefu.txt","w"))==NULL)
11、
12、((fp=fopen("yunsuanfu.txt","w"))==NULL)
13、
14、((fp=fopen("bz.txt","w"))==NULL)
15、
16、((fp=fopen("zx.txt","w"))==NULL)
17、
18、((fp=fopen("fd.txt","w"))==NULL)
19、
20、((fp=fopen("zf.txt","w"))==NULL)
21、
22、((fp=fo
23、pen("zc.txt","w"))==NULL)
24、
25、((fp=fopen("zs.txt","w"))==NULL)
26、
27、((fp=fopen("code.txt","w"))==NULL)){printf("ERROR!Cannotcreatethefile!");exit(0);}fclose(fp);/*依次创建:标识符文档“count_1.txt”,整型常量文档“count_2.txt”,浮点型常量文档“count_3.txt”,字符型常量文档“count_4.txt”,字符串常量文档“count_5.txt”*/if(((fp=fopen("count_1.txt","w"))==N
28、ULL)
29、
30、((fp=fopen("count_2.txt","w"))==NULL)
31、
32、((fp=fopen("count_3.txt","w"))==NULL)
33、
34、((fp=fopen("count_4.txt","w"))==NULL)
35、
36、((fp=fopen("count_5.txt","w"))==NULL)){printf("ERROR!Cannotcreatethefile!");exit(0);}fclose(fp);}voidchar_code(char*word){intm,k=0,i=0;intvalue=0;intvalue2=0;charch,str[100];FI
37、LE*outputfile,*inputfile;for(m=1;m<=34;m++){if(strcmp(word,bl[m])==0){value=1;break;}}if(value==1){if(s0[m]==0){outputfile=fopen("baoliuzi.txt","a");fprintf(outputfile,"0t%dtt%s",m,word);fclose(o