




版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
编译原理C语言词法分析器编译原理C语言词法分析器编译原理C语言词法分析器xxx公司编译原理C语言词法分析器文件编号:文件日期:修订次数:第1.0次更改批准审核制定方案设计,管理制度编译原理C语言词法分析器一、实验题目编制并调试C词法分析程序。txt源代码:main(){intsum=0,it=1;/*Variabledeclaration*/if(sum==1)it++;elseit=it+2;}设计其词法分析程序,能识别出所有的关键字、标识符、常数、运算符(包括复合运算符,如++)、界符;能过滤掉源程序中的注释、空格、制表符、换行符;并且能够对一些词法规则的错误进行必要的处理,如:标识符只能由字母、数字和下划线组成,且第一个字符必须为字母或下划线。实验要求:要给出所分析语言的词法说明,相应的状态转换图,单词的种别编码方案,词法分析程序的主要算法思想等。二、实验目的1、理解词法分析在编译程序中的作用;2、掌握词法分析程序的实现方法和技术;3、加深对有穷自动机模型的理解。三、主要函数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()绘制程序界面绘制程序界面调用初始化函数:voidload()实现文件的建立调用初始化函数:voidload()实现文件的建立调用主扫描函数:voidscan()实现文件的扫描调用主扫描函数:voidscan()实现文件的扫描分析完成后指引用户查看相关文件,直到用户输入退出命令分析完成后指引用户查看相关文件,直到用户输入退出命令函数结束函数结束2.初始化函数voidload()通过文件指针建立相关文件通过文件指针建立相关文件函数结束函数结束3.保留字及标识符判断函数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()函数开始函数开始读入源文件中的一个单词读入源文件中的一个单词判断宏定义判断宏定义忽略忽略无效字符无效字符忽略忽略调用保留字及标识符函数voidchar_search进行处理调用保留字及标识符函数voidchar_search进行处理字符串字符常量字符常量调用字符常量判断函数voidc_search调用字符常量判断函数voidc_search进行处理调用字符串常量判断函数voidcc_search进行处理字符串常量调用字符串常量判断函数voidcc_search进行处理字符串常量整数常量整数常量调用整数类型判断函数voidinta_search进行处理调用整数类型判断函数voidinta_search进行处理调用浮点类型判断函数voidintb_search进行处理浮点数常量调用浮点类型判断函数voidintb_search进行处理浮点数常量注释注释写入注释文件写入注释文件查运算符、分隔符表并写入输出文件限制符查运算符、分隔符表并写入输出文件限制符五、关键代码#include<>#include<>#include<>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","_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("","w"))==NULL) { printf("Error!Can'tcreatefile:"); return; } fclose(floading); /*建立保留字表文件:*/ if((floading=fopen("","w"))==NULL) { printf("Error!Can'tcreatefile:"); return; } /*建立分隔符表文件:*/ if((floading=fopen("","w"))==NULL) { printf("Error!Can'tcreatefile:"); return; } fclose(floading); /*建立运算符表文件:*/ if((floading=fopen("","w"))==NULL) { printf("Error!Can'tcreatefile:"); return; } fclose(floading); /*建立标识符表文件:*/ if((floading=fopen("","w"))==NULL) { printf("Error!Can'tcreatefile:"); return; } fclose(floading); /*建立整数类型常量表文件:*/ if((floading=fopen("","w"))==NULL) { printf("Error!Can'tcreatefile:"); return; } fclose(floading); /*建立浮点类型常量表文件:*/ if((floading=fopen("","w"))==NULL) { printf("Error!Can'tcreatefile:"); return; } fclose(floading); /*建立字符类型常量表文件:*/ if((floading=fopen("","w"))==NULL) { printf("Error!Can'tcreatefile:"); return; } fclose(floading); /*建立字符串类型常量表文件:*/if((floading=fopen("","w"))==NULL) { printf("Error!Can'tcreatefile:"); return; } fclose(floading); /*建立注释文件:*/ if((floading=fopen("","w"))==NULL) { printf("Error!Can'tcreatefile:"); return; } fclose(floading); /*建立内部码文件:*/ 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("","a"); fprintf(foutput,"0\t%d\t\t%s\n",m,word); fclose(foutput); xx0[m]=1; } foutput=fopen("","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("","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("","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("","a"); fprintf(foutput,"3\t%d\t\t%s\n",temp_key3,word); fclose(foutput); foutput=fopen("","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("","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("","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("","a"); fprintf(foutput,"4\t0\t%d\t%s\n",temp_c40,word); fclose(foutput); foutput=fopen("","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++; foutput=fopen("","a"); fprintf(foutput,"4\t1\t1\t%s\n",word); fclose(foutput); } finput=fopen("temp_c41","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("","a"); fprintf(foutput,"4\t1\t%d\t%s\n",line,word); fclose(foutput); } else { foutput=fopen("temp_c41","a"); fprintf(foutput,"%s\n",word); fclose(foutput); temp_c41++; foutput=fopen("","a"); fprintf(foutput,"4\t1\t%d\t%s\n",temp_c41,word); fclose(foutput); foutput=fopen("","a"); fprintf(foutput,"4\t1\t%d\t%s\n",temp_c41,word); fclose(foutput); }}/*******字符串常量判断函数*******/voidcc_search(char*word){ FILE*foutput,*finput; charc; charcs[100]; intcsi=0; intline=0; intvalue2=0; if(temp_c43==0) { foutput=fopen("temp_c43","a"); fprintf(foutput,"%s\n",word); fclose(foutput); temp_c43++; foutput=fopen("","a"); fprintf(foutput,"4\t3\t1\t%s\n",word); fclose(foutput); } finput=fopen("temp_c43","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("","a"); fprintf(foutput,"4\t3\t%d\t%s\n",line,word); fclose(foutput); } else { foutput=fopen("temp_c43","a"); fprintf(foutput,"%s\n",word); fclose(foutput); temp_c43++; foutput=fopen("","a"); fprintf(foutput,"4\t3\t%d\t%s\n",temp_c43,word); fclose(foutput); foutput=fopen("","a"); fprintf(foutput,"4\t3\t%d\t%s\n",temp_c43,word); fclose(foutput); }}/*******字符常量判断函数*******/voidc_search(char*word){ FILE*foutput,*finput; charc; charcs[100]; intcsi=0; intline=0; intvalue2=0; if(temp_c42==0) { foutput=fopen("temp_c42","a"); fprintf(foutput,"%s\n",word); fclose(foutput); temp_c42++; foutput=fopen("","a"); fprintf(foutput,"4\t2\t1\t%s\n",word); fclose(foutput); } finput=fopen("temp_c42","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("","a"); fprintf(foutput,"4\t2\t%d\t%s\n",line,word); fclose(foutput); } else { foutput=fopen("temp_c42","a"); fprintf(foutput,"%s\n",word); fclose(foutput); temp_c42++; foutput=fopen("","a"); fprintf(foutput,"4\t2\t%d\t%s\n",temp_c42,word); fclose(foutput); foutput=fopen("","a"); fprintf(foutput,"4\t2\t%d\t%s\n",temp_c42,word); fclose(foutput); }}/*******主扫描函数*******/voidscan(){ intcount; charchin; FILE*fin; FILE*fout; charfilename[50]; chartemp[100]; chartarget[3]="'"; printf("请输入文件名:"); scanf("%s",filename); if((fin=fopen(filename,"r"))==NULL) { printf("Error!Can'topenfile:%s\n",filename); return; } chin=fgetc(fin); while(chin!=EOF) { /*对文件包含、宏定义进行处理*/ if(chin=='#') { while(chin!='>') chin=fgetc(fin); /*chin=fgetc(fin);*/ } /*对空格符、水平制表符进行处理*/ elseif((chin=='')||(chin=='\t')) { ; } /*对回车符进行处理*/ elseif(chin=='\n') { ; } /*对单引号内的字符常量进行处理*/ elseif(chin==target[0]) { if(xx1[9]==0) { fout=fopen("","a"); fprintf(fout,"1\t9\t\t%c\n",target[0]); fclose(fout); xx1[9]=1; } temp[0]=chin; chin=fgetc(fin); temp[1]=chin; chin=fgetc(fin); if(chin!=target[0]) { temp[2]=chin; chin=fgetc(fin); temp[3]=chin; temp[4]='\0'; } else { temp[2]=chin; temp[3]='\0'; } c_search(temp); } /*对双引号内的字符串常量进行处理*/ elseif(chin=='"') { inti=0; temp[i++]='"'; chin=fgetc(fin); while(chin!='"') { temp[i++]=chin; chin=fgetc(fin); } temp[i]='"'; temp[i+1]='\0'; cc_search(temp); } /*对保留字、标识符进行处理*/ elseif(((chin>='A')&&(chin<='Z'))||((chin>='a')&&(chin<='z'))||(chin=='_')) { inti=0; while(((chin>='A')&&(chin<='Z'))||((chin>='a')&&(chin<='z'))||(chin=='_')||((chin>='0')&&(chin<='9'))) { temp[i++]=chin; chin=fgetc(fin); } temp[i]='\0'; char_search(temp); if(chin!=EOF) fseek(fin,-1L,SEEK_CUR); } /*对整型、浮点型数据进行处理*/ elseif((chin>='0')&&(chin<='9')) { intdotcount=0; inti=0; while(((chin>='0')&&(chin<='9'))||(chin=='.')) { if(chin=='.') dotcount++; if(dotcount==2) break; temp[i++]=chin; chin=fgetc(fin); } temp[i]='\0'; if(dotcount==1) intb_search(temp); else inta_search(temp); if(chin!=EOF) fseek(fin,-1L,SEEK_CUR); } /*对注释进行处理*/ elseif(chin=='/') { chin=fgetc(fin); if(chin=='=') { fout=fopen("","a"); fprintf(fout,"2\t30\t\t/=\n"); fclose(fout); } elseif(chin!='*') { fout=fopen("","a"); fprintf(fout,"2\t4\t\t/\n"); fclose(fout); fseek(fin,-1L,SEEK_CUR); } elseif(chin=='*') { count=0; chin=fgetc(fin); fout=fopen("","a"); fprintf(fout,"/*"); while(count!=2) { count=0; while(chin!='*') { fprintf(fout,"%c",chin); chin=fgetc(fin); } count++; fprintf(fout,"%c",chin); chin=fgetc(fin); if(chin=='/') { count++; fprintf(fout,"%c\n",chin); } else { fprintf(fout,"%c",chin); chin=fgetc(fin); } } } } /*对运算符、分隔符进行处理*/ else { inttime=0; intfirstblood=0; temp[0]=chin; chin=fgetc(fin); if(chin!=EOF) { temp[1]=chin; temp[2]='\0'; for(time=1;time<=30;time++) { if(strcmp(temp,key2[time])==0) { firstblood=1; if(xx2[time]==0) { fout=fopen("","a"); fprintf(fout,"2\t%d\t\t%s\n",time,temp); fclose(fout); xx2[time]=1; } fout=fopen("","a"); fprintf(fout,"2\t%d\t\t%s\n",time,temp); fclose(fout); break; } } if(firstblood!=1) { fseek(fin,-1L,SEEK_CUR); temp[1]='\0'; for(time=1;time<=9;time++) { if(strcmp(temp,key1[time])==0) { if(xx1[time]==0) { fout=fopen("","a"); fprintf(fout,"1\t%d\t\t%s\n",time,temp); fclose(fout); xx1[time]=1; } fout=fopen("","a"); fprintf(fout,"1\t%d\t\t%s\n",time,temp); fclose(fout); break; } } for(time=1;time<=30;time++) { if(strcmp(temp,key2[time])==0) { if(xx2[time]==0) { fout=fopen("","a"); fprintf(fout,"2\t%d\t\t%s\n",time,temp); fclose(fout); xx2[time]=1; } fout=fopen("","a"); fprintf(fout,"2\t%d\t\t%s\n",time,temp); fclose(fout); break; } } } } } chin=fgetc(fin); } fout=fopen("","a"); fprintf(fout,"1\t6\t\t}\n"); fclose(fout);}/*******Main函数*******/voidmain(){ FILE*fread; charcharin; charcommand='Q'; printf("\n"); printf("********************C语言词法分析工具********************\n"); printf("**\n"); printf("**\n"); printf("*命令如下:*\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-->退出*\n"); printf("***************************************************************\n"); printf("\n"); load(); scan(); printf("\n"); printf("分析完成!\n"); getchar(); printf("\n"); printf("请输入命令:"); command=getchar(); while((command!='Q')&&(command!='q')) { switch(command) { case'0': { printf("*************************\n"); printf("\n"); fread=fopen("","r"); charin=fgetc(fread); while(charin!=EOF) { putchar(charin); charin=fgetc(fread); } printf("\n"); printf("*************************\n"); printf("\n"); printf("请输入命令:"); break; } case'1': { printf("*************************\n"); printf("\n"); fread=fopen("","r"); charin=fgetc(fread); while(charin!=EOF) { putchar(charin); charin=fgetc(fread); } printf("\n"); printf("*************************\n"); printf("\n"); printf("请输入命令:"); break; } case'2': { printf("*************************\n"); printf("\n"); fread=fopen("","r"); charin=fgetc(fread); while(charin!=EOF) { putchar(charin); charin=fgetc(fread); } printf("\n"); printf("*************************\n"); printf("\n"); printf("请输入命令:"); break; } case'3': { printf("*************************\n"); printf("\n"); fread=fopen("","r"); charin=fgetc(fread); while(charin!=EOF) { putchar(charin); charin=fgetc(fread); } printf("\n"); printf("*************************\n"); printf("\n"); printf("请输入命令:"); break; } case'4': {
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 语言的美与表达试题及答案
- 专升本思政与经济发展试题及答案
- 2025年2个女儿离婚协议书模板
- 二零二五年度临聘员工劳动合同模板制作与解析
- 二零二五年度企业内部廉洁自律规范执行协议
- 二零二五年度房屋建筑漏水责任赔偿与维修协议
- 2025年度橱柜行业电商平台合作合同
- 二零二五年度旅游行业员工转正协议书范本
- 2025年度艺术画廊墙布采购合同书
- 二零二五年度农机租赁与农业信息化平台合作协议
- 炼油化工企业设备管理制度汇编
- 智能理赔人工智能在保险业的应用
- 一方出地一方出资合作建房合同样本正规范本(通用版)
- 教辅材料采购投标方案(技术标)
- 气防站的安全管理制度
- 《幸福花儿开心上》教学设计(内蒙古市级优课)x-三年级音乐教案
- 设备仓库管理制度
- 铁路调车基本技能
- 《故乡》课后习题参考答案
- 城市轨道交通站台屏蔽门系统完整全套教学课件
- 部编2023版道德与法治六年级下册活动园问题及答案
评论
0/150
提交评论