版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、精选优质文档-倾情为你奉上实验1: 扫描器的设计与实现1.1. 目的和要求1 加深对词法分析理论的理解,培养动手实践的能力。2 学会从以字符串表示的源程序中识别出具有独立意义的基本语法单位,同时指出它们的属性的方法和技术。1.2. 实验环境Windows XP + WinTC1911.3. 实验准备1、先将课本3.5节词法分析程序的生成认真的学习一遍,理解词法分析程序的构成过程。2、写出C语言测试程序,分析可能得到的结果。测试程序:#define pi 3.14void main() double r = 2.5; double s = 0.0; s = pi*r*r; printf(&quo
2、t;%f",s);预测结果:(513,#)(14,define)(100,pi)(200,3)(514,.)(200,14)(1,void)(2,main)(502,()(503,)(506,)(15,double)(100,r)(402,=)(200,2)(514,.)(200,5)(501,;)(15,double)(100,s)(402,=)(200,0)(514,.)(200,0)(501,;)(100,s)(402,=)(100,pi)(416,*)(100,r)(416,*)(100,r)(501,;)(12,printf)(502,()(509,")(511,
3、%)(100,f)(509,")(512,)(100,s)(503,)(501,;)(507,)1.4. 实验内容及步骤1、输入已给的文本格式的扫描程序Lexical.c文件,然后编译运行,检查修改错误。2、编译成功后,提示输入C语言测试程序,用回车键查看输出的单词流,即单词符号及其属性。3、比较自己分析的结果和屏幕上的输出结果。1.5. 实验小结1、得到的经验。通过本次实验,我了解了如何设计、编制并调试词法分析程序,加深了对词法分析原理的理解。同时,在编制和调试程序时要全面考虑,避免漏掉或者错误定义定界符等,造成不必要的错误。2、遇到的主要问题。忘记对空格、回车符等定界符进行设置等
4、。3、改进方案。(1)char *keywords15 = "void","main","if","then","break","int","char","float","include","for","while","printf","scanf","define","double"(2)case
5、 13:flag = 14;status = 1;break;case 14:flag = 15;status = 1;break;(3)case'.':wordsj = ch;wordsj+1 = '0'flag = 514;break;case' ':wordsj = ' 'flag = 250;break;case'n':flag = 251;break;(4)else if(flag = 250|flag = 251)修改前源代码:#include"stdio.h"#include&qu
6、ot;stdlib.h"#include"string.h"int i,j,k,flag,number,status;/*status which is use to judge the string is keywords or not!*/char ch;char words10 = " "char program500;int Scan(char program)char *keywords13 = "void","main","if","then",&quo
7、t;break","int","char","float","include","for","while","printf","scanf"number = 0;status = 0;j = 0;ch = programi+; /* To handle the lettle space ands tab*/*handle letters*/if (ch >= 'a') && (ch <
8、;= 'z' )while (ch >= 'a') && (ch <= 'z' )wordsj+=ch;ch=programi+;i-;wordsj+ = '0'for (k = 0; k < 13; k+)if (strcmp (words,keywordsk) = 0)switch(k)case 0:flag = 1;status = 1;break;case 1:flag = 2;status = 1;break;case 2:flag = 3;status = 1;break;case 3
9、:flag = 4;status = 1;break;case 4:flag = 5;status = 1;break;case 5:flag = 6;status = 1;break;case 6:flag = 7;status = 1;break;case 7:flag = 8;status = 1;break;case 8:flag = 9;status = 1;break;case 9:flag = 10;status = 1;break;case 10:flag = 11;status = 1;break;case 11:flag = 12;status = 1;break;case
10、 12:flag = 13;status = 1;break;if (status = 0)flag = 100;/*handle digits*/else if (ch >= '0') && (ch <= '9')number = 0;while (ch >= '0' ) && (ch <= '9' )number = number*10+(ch-'0');ch= programi+;flag = 200;i-;/*opereation and edge h
11、andle*/else switch (ch)case '=':if (ch = '=')wordsj+ = ch;wordsj= '0'ch= programi+;if (ch = '=')wordsj+ = ch;wordsj= '0'flag = 401;elsei-;flag = 402;break;case'>':if (ch = '>')wordsj+ = ch;wordsj = '0'ch= programi+;if (ch = '=
12、')wordsj+ = ch;wordsj = '0'flag = 403;elsei-;flag = 404;break;case'<':if (ch = '<')wordsj+ = ch;wordsj = '0'ch= programi+;if (ch = '=')wordsj+ = ch;wordsj= '0'flag = 405;elsei-;flag = 406;break;case'!':if (ch = '!')wordsj+ = c
13、h;wordsj= '0'ch = programi+;if (ch = '=')wordsj+ = ch;wordsj= '0'flag= 407;elsei-;flag = 408;break;case'+':if (ch = '+')wordsj+ = ch;wordsj = '0'ch= programi+;if (ch = '=')wordsj+ = ch;wordsj = '0'flag= 409;else if (ch = '+')wor
14、dsj+ = ch;wordsj = '0'flag= 410;elsei-;flag= 411;break;case'-':if (ch = '-')wordsj+ = ch;wordsj = '0'ch= programi+;if (ch = '=')wordsj+ = ch;wordsj = '0'flag = 412;else if( ch = '-')wordsj+ = ch;wordsj = '0'flag = 413;elsei-;flag = 414;
15、break;case'*':if (ch = '*')wordsj+ = ch;wordsj = '0'ch= programi+;if (ch = '=')wordsj+ = ch;wordsj = '0'flag = 415;elsei-;flag = 416;break;case'/':if (ch = '/')wordsj+ = ch;wordsj = '0'ch= programi+;if (ch = '=')wordsj+ = ch;wor
16、dsj = '0'flag= 417;elsei-;flag = 418;break;case'':wordsj = ch;wordsj+1 = '0'flag = 501;break;case'(':wordsj = ch;wordsj+1 = '0'flag = 502;break;case')':wordsj = ch;wordsj+1 = '0'flag = 503;break;case'':wordsj = ch;wordsj+1 = '0'
17、flag = 504;break;case'':wordsj = ch;wordsj+1 = '0'flag = 505;break;case'':wordsj = ch;wordsj+1 = '0'flag= 506;break;case'':wordsj = ch;wordsj+1 = '0'flag = 507;break;case':':wordsj = ch;wordsj+1 = '0'flag = 508;break;case'"'
18、;:wordsj = ch;wordsj+1 = '0'flag = 509;break;case'%':if (ch = '%')wordsj+ = ch;wordsj = '0'ch = programi+;if (ch = '=')wordsj+ = ch;wordsj = '0'flag= 510;elsei-;flag = 511;break;case',':wordsj = ch;wordsj+1 = '0'flag = 512;break;case
19、9;#':wordsj = ch;wordsj+1 = '0'flag = 513;break;case'':wordsj = '#'flag = 0;break;default:flag = -1;break;return flag;main()i=0;printf("please input a program end with ");doch = getchar();programi+ = ch;while(ch != '');i = 0;doflag = Scan(program);if (fl
20、ag = 200)printf("(%2d,%4d)",flag,number);else if (flag = -1)printf("(%d,error)",flag);elseprintf("(%2d,%4s)",flag,words);while (flag != 0);system("pause");修改后源代码:#include"stdio.h"#include"stdlib.h"#include"string.h"int i,j,k,flag,
21、number,status;/*status which is use to judge the string is keywords or not!*/char ch;char words10 = " "char program500;int Scan(char program)char *keywords15 = "void","main","if","then","break","int","char","float&q
22、uot;,"include","for","while","printf","scanf","define","double"number = 0;status = 0;j = 0;ch = programi+; /* To handle the lettle space ands tab*/*handle letters*/if (ch >= 'a') && (ch <= 'z' )while
23、(ch >= 'a') && (ch <= 'z' )wordsj+=ch;ch=programi+;i-;wordsj+ = '0'for (k = 0; k < 15; k+)if (strcmp (words,keywordsk) = 0)switch(k)case 0:flag = 1;status = 1;break;case 1:flag = 2;status = 1;break;case 2:flag = 3;status = 1;break;case 3:flag = 4;status = 1;b
24、reak;case 4:flag = 5;status = 1;break;case 5:flag = 6;status = 1;break;case 6:flag = 7;status = 1;break;case 7:flag = 8;status = 1;break;case 8:flag = 9;status = 1;break;case 9:flag = 10;status = 1;break;case 10:flag = 11;status = 1;break;case 11:flag = 12;status = 1;break;case 12:flag = 13;status =
25、 1;break;case 13:flag = 14;status = 1;break;case 14:flag = 15;status = 1;break;if (status = 0)flag = 100;/*handle digits*/else if (ch >= '0') && (ch <= '9')number = 0;while (ch >= '0' ) && (ch <= '9' )number = number*10+(ch-'0');ch=
26、 programi+;flag = 200;i-;/*opereation and edge handle*/else switch (ch)case '=':if (ch = '=')wordsj+ = ch;wordsj= '0'ch= programi+;if (ch = '=')wordsj+ = ch;wordsj= '0'flag = 401;elsei-;flag = 402;break;case'>':if (ch = '>')wordsj+ = ch;w
27、ordsj = '0'ch= programi+;if (ch = '=')wordsj+ = ch;wordsj = '0'flag = 403;elsei-;flag = 404;break;case'<':if (ch = '<')wordsj+ = ch;wordsj = '0'ch= programi+;if (ch = '=')wordsj+ = ch;wordsj= '0'flag = 405;elsei-;flag = 406;break;
28、case'!':if (ch = '!')wordsj+ = ch;wordsj= '0'ch = programi+;if (ch = '=')wordsj+ = ch;wordsj= '0'flag= 407;elsei-;flag = 408;break;case'+':if (ch = '+')wordsj+ = ch;wordsj = '0'ch= programi+;if (ch = '=')wordsj+ = ch;wordsj =
29、39;0'flag= 409;else if (ch = '+')wordsj+ = ch;wordsj = '0'flag= 410;elsei-;flag= 411;break;case'-':if (ch = '-')wordsj+ = ch;wordsj = '0'ch= programi+;if (ch = '=')wordsj+ = ch;wordsj = '0'flag = 412;else if( ch = '-')wordsj+ = ch;w
30、ordsj = '0'flag = 413;elsei-;flag = 414;break;case'*':if (ch = '*')wordsj+ = ch;wordsj = '0'ch= programi+;if (ch = '=')wordsj+ = ch;wordsj = '0'flag = 415;elsei-;flag = 416;break;case'/':if (ch = '/')wordsj+ = ch;wordsj = '0'ch=
31、 programi+;if (ch = '=')wordsj+ = ch;wordsj = '0'flag= 417;elsei-;flag = 418;break;case'':wordsj = ch;wordsj+1 = '0'flag = 501;break;case'(':wordsj = ch;wordsj+1 = '0'flag = 502;break;case')':wordsj = ch;wordsj+1 = '0'flag = 503;break;case'':wordsj = ch;wordsj+1 = '0'flag = 504;break;case'':wordsj = ch;wordsj+1 = '0'flag = 505;break;case'':wordsj = ch;wordsj+1 = '0'flag= 506;break;case'':wordsj = ch;wordsj+1 = '0'flag = 507;break;case'
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 校企合作实习协议书范例
- 外研版初一英语教案编写要点
- 基督教历史一窥
- 芒果订购合同范例
- 外墙装修涂饰劳务分包协议
- 外研版九年级英语下册教学案例
- 小升初图形题目详解与练习
- 大语文观塑造全新教学模式
- 大蒜种植的启示与实践
- 清算版权许可协议
- 《道路勘测设计》课件-4-4 路基排水设计
- 红河学院《运动营养学》2022-2023学年第一学期期末试卷
- 偏瘫的早期康复治疗
- 烘焙师聘用合同范例
- 萱草花音乐课件
- 2024安全员知识考试题(全优)
- 国家开放大学《当代中国政治制度》期末复习题
- 2024新版行政复议法知识培训课件
- 石家庄市2025届高三教学质量摸底检测英语试题
- 专项15-分式方程的应用-八大题型
- 冠心病课件完整版本
评论
0/150
提交评论