收藏 分销(赏)

编译原理实验1扫描器的设计与实现.doc

上传人:精**** 文档编号:4128710 上传时间:2024-07-31 格式:DOC 页数:25 大小:76.54KB
下载 相关 举报
编译原理实验1扫描器的设计与实现.doc_第1页
第1页 / 共25页
编译原理实验1扫描器的设计与实现.doc_第2页
第2页 / 共25页
编译原理实验1扫描器的设计与实现.doc_第3页
第3页 / 共25页
编译原理实验1扫描器的设计与实现.doc_第4页
第4页 / 共25页
编译原理实验1扫描器的设计与实现.doc_第5页
第5页 / 共25页
点击查看更多>>
资源描述

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 = pir*r; printf(%f,s);预测结果:(513,)(

2、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,)(100,f)(509,”)(512,)(100,s)(503,

3、)(501,;)(507,)1.4. 实验内容及步骤1、输入已给的文本格式的扫描程序Lexical.c文件,然后编译运行,检查修改错误。2、编译成功后,提示输入C语言测试程序,用回车键查看输出的单词流,即单词符号及其属性.3、比较自己分析的结果和屏幕上的输出结果.1.5. 实验小结1、得到的经验。通过本次实验,我了解了如何设计、编制并调试词法分析程序,加深了对词法分析原理的理解.同时,在编制和调试程序时要全面考虑,避免漏掉或者错误定义定界符等,造成不必要的错误。2、遇到的主要问题。忘记对空格、回车符等定界符进行设置等。3、改进方案。(1)char *keywords15 = ”void,”ma

4、in”,”if”,then,break,”int”,char,”float”,”include”,”for,while”,printf,”scanf,define”,”double;(2)case 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;casen:flag = 251;break;(4)else if(flag = 250|flag =

5、251)修改前源代码:#includestdio。h”#include”stdlib。hinclude”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”,break”,int”,”char”,”float”,”include,for”,”

6、while,printf,scanf”;number = 0;status = 0;j = 0;ch = programi+; /* To handle the lettle space ands tab/*handle letters/if (ch = a) & (ch = z )while ((ch = a) & (ch = z )wordsj+=ch;ch=programi+;i-;wordsj+ = 0;for (k = 0; k = 0 ) & (ch )wordsj+ = ch;wordsj = 0;ch= programi+;if (ch = =)wordsj+ = ch;wor

7、dsj = 0;flag = 403;elsei-;flag = 404;break;case:if (ch = = a) & (ch = z ))while (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;

8、case 3: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;brea

9、k;case 12:flag = 13;status = 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 )wordsj+ = ch;wordsj = 0;ch= programi+;if (ch = =)wordsj+ = ch;wordsj = 0;flag = 403;elsei

10、-;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+ = ch;wordsj= 0;ch = programi+;if (ch = =)wordsj+ = ch;wordsj= 0;flag= 407;elsei-;flag = 408;break;case+:if (ch = +)wordsj+ = ch;wordsj

11、 = 0;ch= programi+;if (ch = =)wordsj+ = ch;wordsj = 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;wordsj = 0;flag = 413;elsei-;flag =

12、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= programi+;if (ch = =)wordsj+ = ch;wordsj = 0;flag= 417;elsei-;flag = 418;break;case;:wordsj = ch;wordsj+1 = 0;flag = 501;b

13、reak;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::wordsj

14、 = ch;wordsj+1 = 0;flag = 508;break;case: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#:wordsj = ch;wordsj+1 = 0;flag = 513;bre

15、ak;case:wordsj = #;flag = 0;break;case。:wordsj = ch;wordsj+1 = 0;flag = 514;break;case :wordsj = ;flag = 250;break;casen:flag = 251;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 (flag = 200)printf(%2d,4d),flag,number);else if (flag = -1)printf(%d,error),flag);else if(flag = 250|flag = 251)elseprintf(”(%2d,%4s),flag,words);while (flag != 0);system(pause”);25

展开阅读全文
部分上传会员的收益排行 01、路***(¥15400+),02、曲****(¥15300+),
03、wei****016(¥13200+),04、大***流(¥12600+),
05、Fis****915(¥4200+),06、h****i(¥4100+),
07、Q**(¥3400+),08、自******点(¥2400+),
09、h*****x(¥1400+),10、c****e(¥1100+),
11、be*****ha(¥800+),12、13********8(¥800+)。
相似文档                                   自信AI助手自信AI助手
搜索标签

当前位置:首页 > 包罗万象 > 大杂烩

移动网页_全站_页脚广告1

关于我们      便捷服务       自信AI       AI导航        获赠5币

©2010-2024 宁波自信网络信息技术有限公司  版权所有

客服电话:4008-655-100  投诉/维权电话:4009-655-100

gongan.png浙公网安备33021202000488号   

icp.png浙ICP备2021020529号-1  |  浙B2-20240490  

关注我们 :gzh.png    weibo.png    LOFTER.png 

客服