




版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、C语言词法分析器开发文档Powered By萌萌的玉雪、实验题目编制并调试c词法分析程序。、实验目的全面深入理解高级语言程序设计知识,掌握应用技巧,提高应用与分析能力。、主要函数void load ()void char search (ehar *word)void mai n ()void in tb seareh(ehar *word)void sea n ()void e seareh(ehar *word)void in ta seareh(ehar *word)void ee seareh(ehar *word)四、设计void mai n ()1主函数2.初始化函数 void l
2、oad ()3保留字及标识符判断函数void char_search(char *word)4.整数类型判断函数void in ta_search(char *word)5.浮点类型判断函数void in tb_search(char *word)6.字符串常量判断函数void cc_search(char *word)7.字符常量判断函数void c_search(char *word)同4、5函数图8.主扫描函数void scan ()函数开始五、关键代码#i nclude #in elude #in elude char*keyO=,”auto,break,case,char,co ns
3、t,co nti nue,default,do,double,else,e num,extern,float,for,goto,if ,i nt,lo ng,register,return,short,sig ned,sizeof,static,struct,switch,typedef,_Complex,_lmagi nary, un io n,u nsig ned,void,volatile,while;/*保留字表*/char *key1=”;/*分隔符表*/char*key2=+ - * / % = = = != ! & |=* = /= ;II II| A & = ? _ + + _
4、+J | J)J 1)J J/* 运算符表 */int xx035,xx110,xx231;int temp_key3=0,temp_c40=0,temp_c41=0,temp_c42=0,temp_c43=0;初始化函数 */void load()int mm;for (mm=0;mm=34;mm+) xx0mm=0;for (mm=0;mm=9;mm+) xx1mm=0;for (mm=0;mm=30;mm+) xx2mm=0;FILE *floading;if (floading=fopen(key0.txt,w)=NULL) printf(Error! Cant create file
5、 : key0.txt); return;fclose (floading);/* 建立保留字表文件: key0.txt*/if (floading=fopen(key1.txt,w)=NULL) printf(Error! Cant create file : key1.txt); return;/* 建立分隔符表文件: key1.txt*/if (floading=fopen(key2.txt,w)=NULL)printf(Error! Cant create file : key2.txt); return;fclose(floading);/* 建立运算符表文件: key2.txt*/
6、if (floading=fopen(key3.txt,w)=NULL)printf(Error! Cant create file : key3.txt); return;fclose (floading);/* 建立标识符表文件: key3.txt*/if (floading=fopen(c40.txt,w)=NULL)printf(Error! Cant create file : c40.txt); return;fclose (floading);/* 建立整数类型常量表文件: c40.txt*/if (floading=fopen(c41.txt,w)=NULL)printf(Er
7、ror! Cant create file : c41.txt); return;fclose (floading);/* 建立浮点类型常量表文件: c41.txt*/if (floading=fopen(c42.txt,w)=NULL)printf(Error! Cant create file : c42.txt); return;fclose (floading);/* 建立字符类型常量表文件: c42.txt*/if (floading=fopen(c43.txt,w)=NULL)printf(Error! Cant create file : c43.txt); return;fcl
8、ose (floading);/* 建立字符串类型常量表文件: c43.txt*/if (floading=fopen(defination.txt,w)=NULL) printf(Error! Cant create file : defination.txt); return;fclose (floading);/* 建立注释文件: defination.txt*/if (floading=fopen(output.txt,w)=NULL)printf(Error! Cant create file : output.txt); return;fclose (floading);/* 建立
9、内部码文件: output.txt*/if (floading=fopen(temp_key1,w)=NULL) printf(Error! Cant create file : temp_key1); return;fclose (floading);/* 建立保留字临时表文件: temp_key1*/if (floading=fopen(temp_key3,w)=NULL)printf(Error! Cant create file : temp_key3); return;fclose (floading);/* 建立标识符临时文件: temp_key3*/if (floading=fo
10、pen(temp_c40,w)=NULL)printf(Error! Cant create file : temp_c40); return;fclose (floading);/* 建立整数类型常量临时文件: temp_c40*/ if (floading=fopen(temp_c41,w)=NULL) printf(Error! Cant create file : temp_c41); return;fclose (floading);/* 建立浮点类型常量临时文件: temp_c41*/if (floading=fopen(temp_c42,w)=NULL)printf(Error!
11、 Cant create file : temp_c42); return;fclose (floading);/* 建立字符类型常量临时文件: temp_c42*/ if (floading=fopen(temp_c43,w)=NULL) printf(Error! Cant create file : temp_c43); return;fclose (floading);/* 建立字符串类型常量临时文件: temp_c43*/ /* 保留字及标识符判断函数 */void char_search(char *word)int m,line=0,csi=0;int value=0;int v
12、alue2=0;char c,cs100;FILE *foutput,*finput;for (m=1;m*Mlndlno4oso_o4MP0M-Ltoldlu9-=ss%=p%=517=ndln04=lu_d4FB-cxlo芸=)u doTlndlnMlndlno4oso_o4 MP0M-Ltoldlu9-=ss%=p%=517=ndln04=lu_d4M=e=-=ndlno=)u doTlndln+Ltoldlu9Mlndlno4oso_o4MPOM-=ss%=ndlno4=lu_d4-(= L 芸 ldlu9=)u doTlndln so 宀Mlndlno4oso_o4LL_ Ob一 s
13、。一 s。宀-anduu=Qo6T。 Q+土 so一 s。 (-ubl!.。)=IIM (do 山I!.。) 2ZM Mlnduu=Qo6T。-(=-芸 ldlu9=)u doTlndu 匸 宀Mlndlno4oso_o4 e0M-=ss%AW$2=ndln04=lu_d4 FB-cxlo芸=)u doTlndln +ldlu9Mlndlno4oso_o4MPOM-=ss%=ndlno4=lu_d4芸 ldlu9=)u doTlndln ouue 芸 ldlu9 二一 OHCXI nro lu- o$u= -u 一O.LS。lu- -00 匸 soelp+Eldlu9Mlndlno4oso_o
14、4MPOM-=ss%=ndlno4=lu_d4-(=&芸 ldlu9=)u doTlndlnouucxltolduJ9 二一OHCXI nro lu-o$u= -u 一O.LS。lu-00 匸 soelpQElp 】nduu=*ndlnor山 i 匚 (POM*elp)lpees。po*Mlndlno4oso_o4JEOMO芸 ldlu9-=ss% 乍 p% 乍$2一寸=ndln04=lu_d4FB-cxlo芸=)u doTlndlnMlndlno4oso_o4JEOMO芸 ldlu9-=ss% 乍 p% 乍$2一寸=ndln04=lu_d4 M=e=-=ndlno=)u doTlndln+l
15、dlu9Mlndlno4oso_o4MPOM-=ss%=ndlno4=lu_d4-(=-芸 ldlu9=)u doTlndln so 宀Mlndlno4oso_o4 xp0M(Du=-=ss%=p%=$2=ndln04=lu_d4 M=e=-=ndlno=)u doTlndln (UHCXI n_e七-anduu=oso_o4 宀-andu_4)oo6To宀-ucxl nrooupOM-so)dluols)七 匸+u =o上 so_andu_4)oo61To宀-UCXI nrooupOM-so)dluols)七 匸+u =O上 s。_ Ob一 S。一 s。宀-anduu=Qo6T。Q+土 so
16、一 s。(-ubl!.。)=IIM(do 山I!.。) 2ZMMlnduu=Qo6T。-(=-&芸 ldlu9=)u doTlndu 匸宀Mlndln04s0_04 eOM-=ss%AI40=ndlno4=lu_d4FB-cxlCXI芸=)u doTlndln*Mlndlno4oso_o4 MPOM7toldlu9-=ss%=p%10=ndlno4=lu_d4FB-cxlCXI芸=)u doTlndlnMlndlno4oso_o4 MPOM7toldlu9-=ss%=p%10=ndlno4=lu_d4M=e=-=ndlno=)u doTlndln+Eldlu9Mlndlno4oso_o4MPO
17、M-=ss%=ndlno4=lu_d4-(=&芸 ldlu9=)u doTlndln so宀Mlndlno4oso_o4 xpOM(Du=-=ss%=p%10=ndlno4=lu_d4M=e=-=ndlno=)u doTlndln(UHCXI n_e七 -anduu=oso_o4void scan()int count;char chin;FILE *fin;FILE *fout;char filename50;char temp100;char target3=;printf( 请输入文件名: );scanf(%s,filename);if (fin=fopen(filename,r)=NU
18、LL)printf(Error! Cant open file : %sn,filename); return;chin=fgetc(fin);while (chin!=EOF)/* 对文件包含、宏定义进行处理 */if (chin=#)while (chin!=) chin=fgetc(fin);/*chin=fgetc(fin);*/* 对空格符、水平制表符进行处理 */else if (chin= )|(chin=t)J/* 对回车符进行处理 */else if (chin=n)J/* 对单引号内的字符常量进行处理 */else if (chin=target0)if (xx19=0)f
19、out=fopen(key1.txt,a); fprintf(fout,1t9tt%cn,target0); fclose(fout);xx19=1;temp0=chin;chin=fgetc(fin);temp1=chin;chin=fgetc(fin);if (chin!=target0) temp2=chin; chin=fgetc(fin); temp3=chin; temp4=0;else temp2=chin; temp3=0; c_search(temp);/* 对双引号内的字符串常量进行处理*/else if (chin=)int i=0;tempi+=;chin=fgetc(
20、fin);while (chin!=) tempi+=chin; chin=fgetc(fin);tempi=;tempi+1=0;cc_search(temp);/* 对保留字、标识符进行处理 */else if (chin=A)&(chin=a)&(chin=A)&(chin=a)&(chin=0)&(chin=0)&(chin=0)&(chin=9)|(chin=.) if (chin=.)dotcount+;if (dotcount=2)break;tempi+=chin;chin=fgetc(fin);tempi=0;if (dotcount=1)intb_search(temp);
21、elseinta_search(temp);if (chin!=EOF)fseek (fin,-1L,SEEK_CUR);/* 对注释进行处理 */else if (chin=/)chin=fgetc(fin);if (chin=)fout=fopen(output.txt,a);fprintf(fout,2t30tt/=n);fclose(fout);else if (chin!=*)fout=fopen(output.txt,a);fprintf(fout,2t4tt/n);fclose(fout); fseek(fin,-1L,SEEK_CUR);else if (chin=*)coun
22、t=0;chin=fgetc(fin);fout=fopen(defination.txt,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,%cn,chin);elsefprintf(fout,%c,chin); chin=fgetc(fin);/* 对运算符、分隔符进行处理 */elsein
23、t time=0;int firstblood=0; temp0=chin; chin=fgetc(fin); if (chin!=EOF) temp1=chin; temp2=0;for (time=1;time=30;time+) if (strcmp(temp,key2time)=0) firstblood=1;if (xx2time=0)fout=fopen(key2.txt,a);fprintf(fout,2t%dtt%sn,time,temp);fclose(fout);xx2time=1;fout=fopen(output.txt,a);fprintf(fout,2t%dtt%s
24、n,time,temp); fclose(fout);break;if (firstblood!=1)fseek(fin,-1L,SEEK_CUR);temp1=0;for (time=1;time=9;time+)if (strcmp(temp,key1time)=0)if (xx1time=0)fout=fopen(key1.txt,a); fprintf(fout,1t%dtt%sn,time,temp); fclose(fout);xx1time=1;fout=fopen(output.txt,a);fprintf(fout,1t%dtt%sn,time,temp);fclose(fo
25、ut);break;for (time=1;time 查看保留字表文件1 - 查看分隔符表文件2 - 查看运算符表文件3 - 查看标识符表文件4 - 查看整数类型常量表5 - 查看浮点类型常量表6 - 查看字符类型常量表7 - 查看字符串类型常量表8 - 查看注释文件9 - 查看内部码文件Q - 退出*n);*n);*n);*n);*n);*n);*n);*n);*n);*n);*n);*n);*n);*n);*n);*n);printf(*n);printf(n);load();scan();printf(n);printf( 分析完成! n);getchar();printf(n);pri
26、ntf( 请输入命令: );command=getchar();while (command!=Q)&(command!=q) switch (command) case 0:printf(H*n);printf(n);fread=fopen(key0.txt,r);case 1:case 2:charin=fgetc(fread);while (charin!=EOF)putchar(charin); charin=fgetc(fread);printf(n);printf(printf(n);printf( 请输入命令: ); break;printf(*n);printf(n);frea
27、d=fopen(key1.txt,r);charin=fgetc(fread);while (charin!=EOF)putchar(charin); charin=fgetc(fread);printf(n);printf(*n);H*printf(n);printf( 请输入命令: ); break;printf(*n);H*printf(n);fread=fopen(key2.txt,r);charin=fgetc(fread);while (charin!=EOF)putchar(charin);charin=fgetc(fread);printf(n);printf(*n);H*pr
28、intf(n);printf( 请输入命令: ); break;*n);printf(*n);printf(n);fread=fopen(key3.txt,r);charin=fgetc(fread);while (charin!=EOF)putchar(charin);charin=fgetc(fread);printf(n);printf(*n);printf(n);printf( 请输入命令: );case 4:break;printf(*n);printf(n);fread=fopen(c40.txt,r);charin=fgetc(fread);while (charin!=EOF)putchar(charin);charin=fgetc(fread);printf(n);printf(*n);printf(n);printf( 请输入命令: );break
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 解析卷北师大版8年级数学上册期中试题含完整答案详解【夺冠】
- 押题宝典高校教师资格证之《高等教育心理学》考试题库有完整答案详解
- 2025年度国际组织外籍工作人员劳动合同
- 2025成都公寓租赁及物业维修服务合同
- 2025版商业房产买卖与租赁保证金退还合同范本
- 2025版环保产业投资合同协议范本
- 2025房基地买卖合同范本(含合同解除条款)
- 2025版外墙保温材料性能检测与施工劳务分包合同范本
- 2025版土地征收补偿协议范本
- 广东省惠州市惠东县2024-2025学年八年级下学期期末考试英语试卷(含笔试答案无听力)
- 2025-2030年中国化学纤维市场发展格局及投资前景规划研究报告
- 维护保养及售后服务说明
- 大学启示录:如何读大学(上海交通大学)学习通测试及答案
- 《静脉输液指南》课件
- AI技术赋能体育课堂教学评价
- 耳穴压豆课件完整版
- 母乳喂养知识培训-课件
- DBJ41T 256-2021 河南省海绵城市设计标准 河南省工程建设标准(住建厅版)
- 独家授权合同模板
- 南京外国语2024-2025九年级上学期第一次月考物理试卷及答案
- 绿色火车站建设与可持续发展
评论
0/150
提交评论