《编译原理-词法分析程序报告 .doc》由会员分享,可在线阅读,更多相关《编译原理-词法分析程序报告 .doc(11页珍藏版)》请在得力文库 - 分享文档赚钱的网站上搜索。
1、 编译原理课程设计课程设计题目: 词法分析器 作者所在学院: 作者所在专业: 作者所在班级: 作 者 学 号: 作 者 姓 名 : 指导教师姓名: 完 成 时 间 : 2009 年 6 月 18 日1.课程设计任务书课题名称编译原理课程设计完成时间2010.7.14指导教师杨焱职称讲师学生姓名李旭昆班级总体设计要求总体设计要求: 每个学生按照课程设计要求,在规定的时间内独立完成。题目: 编译程序构造涉及内容:编译器的结构,词法分析、语法分析工作内容及时间进度安排第一天:设计动员,布置课程设计任务,查阅资料,制定方案,进行程序方案设计。第一天:编写和调试程序第三天:编写和调试程序,第四天:编写和
2、调试程序,整理,提交调研报告,撰写设计报告。第五天:验收,提交设计报告,评定成绩。课程设计成果1、调研报告一份,课程设计报告书一份2、源程序清单一份3、成果使用说明书一份2.调试PL/0编译程序习题第七题正确的程序为: var a,b,c; begin read(a,b); c:=100; if a0 then begin b:=b+1; write(b) end; write(a,b,c); end. 第八题(1) 扩充条件语句的语法图为: If条件then语句else语句EBNF的语法描述为:条件语句if条件then语句else语句 (2) 扩充repeat语句的语法图为: resent语
3、句Until条件1EBNF的语法描述为:repeat循环语句 repeat语句;语句until条件3.课程设计目的:结合课堂上学习的理论知识,通过C+实现词法分析器,更加深入的掌握词法分析;同时也可以更加了解词法分析的原理。4.课程设计内容:词法分析器模块划分:字母处理程序、数字处理程序、其他处理程序、主程序模块调用关系图:主程序字母处理程序数字处理程序其他处理程序模块流程图:否是判断文件是否为空初始化设置开始结束读取文件字符判断字符类型输出:非法字符字符处理程序输出相应字符是否有结束标志结束5.程序源代码:#include #include #include #include #includ
4、e / #define NULL 0FILE *fp;char ch;char *keyword34=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,printf,union,unsigned,void,volatile,while,main;char *operatornum6=+,-,*,/,+,-;char *c
5、omparison8=,=,=,!=;char *interpunction8=,;,:=,.,(,),;char *biaoshifu6=%,$,&,_,#;/特殊标识符char *zhushifu3=/,/*,*/;/注释符char *luoji3=&,|,!;/逻辑运算符/bool search(char searchstr,int wordtype)int i;switch (wordtype)case 1:for(i=0;i=33;i+) if(strcmp(keywordi,searchstr)=0) return(true);break;case 2: for(i=0;i=5;i
6、+) if(strcmp(operatornumi,searchstr)=0) return(true); break; case 3: for(i=0;i=7;i+) if(strcmp(comparisoni,searchstr)=0) return(true); break; case 4: for(i=0;i=7;i+) if(strcmp(interpunctioni,searchstr)=0) return(true); break;case 5: for(i=0;i=5;i+) if(strcmp(biaoshifui,searchstr)=0) return(true); br
7、eak;case 6: for(i=0;i=2;i+) if(strcmp(zhushifui,searchstr)=0) return(true); break;case 7: for(i=0;i=2;i+) if(strcmp(luojii,searchstr)=0) return(true); break;return(false);/char letterprocess (char ch)/字母处理函数int i=-1;char letter20;while (isalnum(ch)!=0) letter+i=ch; ch=fgetc(fp);letteri+1=0;if (searc
8、h(letter,1) printf(n,letter); else printf(n,letter);return(ch);/char numberprocess(char ch)/数字处理程序int i=-1;char num20;while (isdigit(ch)!=0) num+i=ch; ch=fgetc(fp);if(isalpha(ch)!=0) while(isspace(ch)=0) num+i=ch; ch=fgetc(fp); numi+1=0; printf(错误!非法标识符:%sn,num); goto u;numi+1=0; printf(n,num); u: r
9、eturn(ch);/char otherprocess(char ch)int i=-1;char other20;if (isspace(ch)!=0) ch=fgetc(fp); goto u;while (isspace(ch)=0)&(isalnum(ch)=0) other+i=ch; ch=fgetc(fp);otheri+1=0;if (search(other,2) printf(n,other);else if (search(other,3) printf(n,other);elseif (search(other,4) printf(n,other);elseif (s
10、earch(other,5) printf(n,other);elseif (search(other,6) printf(n,other);elseif (search(other,7) printf(n,other);else printf(错误!非法字符:%sn,other);u: return (ch);/void main ()char str,c;printf(*词法分析器*n);if (fp=fopen(源程序.txt,r)=NULL) printf(源程序无法打开!n);else str =fgetc(fp); while (str!=EOF) /文件结束标志 if (isal
11、pha(str)!=0) str=letterprocess(str); else if (isdigit(str)!=0) str=numberprocess(str); else str=otherprocess(str); ; printf(词法分析结束,谢谢使用!n); printf(点任意键退出!n); c=getch();6.程序测试数据和结果:(1)数据 var a,b,c:integer ;x:char ;beginfor i:=b * 2 to 100 do begin x:= (a+b) * (c+d) - (a+b+c) ; T:=T+8 ; endend(2)结果7.设计体会:本次课设使我对编写c语言程序有了更熟练得掌握,对相关控制函数有了更深的了解,对词法分析也有了更透彻的理解。在编写词法分析程序之初,不知如何开始,所以参考了其它人编好的源代码。此程序的特色对于用户来说就是直观,方便,易于掌握与使用。只是关键字,运算符等的定义范围有限,不够完全。8.使用说明书:将需要分析的源代码粘贴到程序根目录下的文本文档中,然后保存,运行程序即可。