您好,欢迎访问三七文档
当前位置:首页 > 商业/管理/HR > 质量控制/管理 > 《C语言词法分析器》开发文档及源代码
1《C语言词法分析器》开发文档PoweredBy萌萌的玉雪一、实验题目编制并调试C词法分析程序。二、实验目的全面深入理解高级语言程序设计知识,掌握应用技巧,提高应用与分析能力。三、主要函数voidload()voidchar_search(char*word)voidmain()voidintb_search(char*word)voidscan()voidc_search(char*word)voidinta_search(char*word)voidcc_search(char*word)四、设计1.主函数voidmain()2.初始化函数voidload()绘制程序界面调用初始化函数:voidload()实现文件的建立调用主扫描函数:voidscan()实现文件的扫描分析完成后指引用户查看相关文件,直到用户输入退出命令函数结束通过文件指针建立相关文件函数结束23.保留字及标识符判断函数voidchar_search(char*word)4.整数类型判断函数voidinta_search(char*word)5.浮点类型判断函数voidintb_search(char*word)6.字符串常量判断函数voidcc_search(char*word)7.字符常量判断函数voidc_search(char*word)同4、5函数图8.主扫描函数voidscan()接收数据是否为保留字是:写入输出文件否:查标识符表找到:写入输出文件没找到:写入表文件写入输出文件接收数据查表找到:写入输出文件没找到:写入整数常量表文件写入输出文件接收数据查表找到:写入输出文件没找到:写入整数常量表文件写入输出文件函数开始3五、关键代码#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[]={,+,-,*,/,%,,,==,=,=,!=,!,&&,||,,,~,|,^,&,=,?:,-,++,--,.,+=,-=,*=,/=};读入源文件中的一个单词宏定义判断忽略无效字符忽略字符串调用保留字及标识符函数voidchar_search进行处理字符常量调用字符常量判断函数voidc_search进行处理字符串常量调用字符串常量判断函数voidcc_search进行处理整数常量调用整数类型判断函数voidinta_search进行处理浮点数常量调用浮点类型判断函数voidintb_search进行处理注释限制符写入注释文件查运算符、分隔符表并写入输出文件4/*运算符表*/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;5}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){6printf(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;7intvalue=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';8csi=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)9{foutput=fopen(temp_c40,a);fprintf(foutput,%s\n,word);fclose(foutput);temp_c40++;f
本文标题:《C语言词法分析器》开发文档及源代码
链接地址:https://www.777doc.com/doc-3375452 .html