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

整理文档很辛苦,赏杯茶钱您下走!

免费阅读已结束,点击下载阅读编辑剩下 ...

阅读已结束,您可以下载文档离线阅读编辑

资源描述

1实验1:扫描器的设计与实现1.1.目的和要求1.加深对词法分析理论的理解,培养动手实践的能力。2.学会从以字符串表示的源程序中识别出具有独立意义的基本语法单位,同时指出它们的属性的方法和技术。1.2.实验环境WindowsXP+WinTC1911.3.实验准备1、先将课本3.5节词法分析程序的生成认真的学习一遍,理解词法分析程序的构成过程。2、写出C语言测试程序,分析可能得到的结果。测试程序:#definepi3.14voidmain(){doubler=2.5;doubles=0.0;s=pi*r*r;printf(%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,%)(100,f)(509,)(512,,)(100,s)(503,))(501,;)(507,})1.4.实验内容及步骤1、输入已给的文本格式的扫描程序Lexical.c文件,然后编译运行,检查修改错误。22、编译成功后,提示输入C语言测试程序,用回车键查看输出的单词流,即单词符号及其属性。3、比较自己分析的结果和屏幕上的输出结果。1.5.实验小结1、得到的经验。通过本次实验,我了解了如何设计、编制并调试词法分析程序,加深了对词法分析原理的理解。同时,在编制和调试程序时要全面考虑,避免漏掉或者错误定义定界符等,造成不必要的错误。2、遇到的主要问题。忘记对空格、回车符等定界符进行设置等。3、改进方案。3(1)char*keywords[15]={void,main,if,then,break,int,char,float,include,for,while,printf,scanf,define,double};(2)case13:{flag=14;status=1;break;}case14:{flag=15;status=1;break;}(3)case'.':{words[j]=ch;words[j+1]='\0';flag=514;break;}case'':{words[j]='';flag=250;break;}case'\n':{flag=251;break;}(4)elseif(flag==250||flag==251){}修改前源代码:#includestdio.h#includestdlib.h#includestring.hinti,j,k,flag,number,status;4/*statuswhichisusetojudgethestringiskeywordsornot!*/charch;charwords[10]={};charprogram[500];intScan(charprogram[]){char*keywords[13]={void,main,if,then,break,int,char,float,include,for,while,printf,scanf};number=0;status=0;j=0;ch=program[i++];/*Tohandlethelettlespaceandstab*//*handleletters*/if((ch='a')&&(ch='z')){while((ch='a')&&(ch='z')){words[j++]=ch;ch=program[i++];}i--;words[j++]='\0';for(k=0;k13;k++)if(strcmp(words,keywords[k])==0)switch(k){case0:{flag=1;status=1;break;}case1:{flag=2;status=1;5break;}case2:{flag=3;status=1;break;}case3:{flag=4;status=1;break;}case4:{flag=5;status=1;break;}case5:{flag=6;status=1;break;}case6:{flag=7;status=1;break;}case7:{flag=8;status=1;break;}case8:{flag=9;status=1;break;6}case9:{flag=10;status=1;break;}case10:{flag=11;status=1;break;}case11:{flag=12;status=1;break;}case12:{flag=13;status=1;break;}}if(status==0){flag=100;}}/*handledigits*/elseif((ch='0')&&(ch='9')){number=0;while((ch='0')&&(ch='9')){number=number*10+(ch-'0');ch=program[i++];}7flag=200;i--;}/*opereationandedgehandle*/elseswitch(ch){case'=':{if(ch=='=')words[j++]=ch;words[j]='\0';ch=program[i++];if(ch=='='){words[j++]=ch;words[j]='\0';flag=401;}else{i--;flag=402;}break;}case'':{if(ch=='')words[j++]=ch;words[j]='\0';ch=program[i++];if(ch=='='){words[j++]=ch;words[j]='\0';flag=403;}else8{i--;flag=404;}break;}case'':{if(ch=='')words[j++]=ch;words[j]='\0';ch=program[i++];if(ch=='='){words[j++]=ch;words[j]='\0';flag=405;}else{i--;flag=406;}break;}case'!':{if(ch=='!')words[j++]=ch;words[j]='\0';ch=program[i++];if(ch=='='){words[j++]=ch;words[j]='\0';flag=407;}else9{i--;flag=408;}break;}case'+':{if(ch=='+')words[j++]=ch;words[j]='\0';ch=program[i++];if(ch=='='){words[j++]=ch;words[j]='\0';flag=409;}elseif(ch=='+'){words[j++]=ch;words[j]='\0';flag=410;}else{i--;flag=411;}break;}case'-':{if(ch=='-')words[j++]=ch;words[j]='\0';ch=program[i++];if(ch=='=')10{words[j++]=ch;words[j]='\0';flag=412;}elseif(ch=='-'){words[j++]=ch;words[j]='\0';flag=413;}else{i--;flag=414;}break;}case'*':{if(ch=='*')words[j++]=ch;words[j]='\0';ch=program[i++];if(ch=='='){words[j++]=ch;words[j]='\0';flag=415;}else{i--;flag=416;}break;}11case'/':{if(ch=='/')words[j++]=ch;words[j]='\0';ch=program[i++];if(ch=='='){words[j++]=ch;words[j]='\0';flag=417;}else{i--;flag=418;}break;}case';':{words[j]=ch;words[j+1]='\0';flag=501;break;}case'(':{words[j]=ch;words[j+1]='\0';flag=502;break;}case')':{words[j]=ch;words[j+1]='\0';flag=503;break;}12case'[':{words[j]=ch;words[j+1]='\0';flag=504;break;}case']':{words[j]=ch;words[j+1]='\0';flag=505;break;}case'{':{words[j]=ch;words[j+1]='\0';flag=506;break;}case'}':{words[j]=ch;words[j+1]='\0';flag=507;break;}case':':{words[j]=ch;words[j+1]='\0';flag=508;break;}case'':{words[j]=ch;words[j+1]='\0';flag=509;break;}13case'%':{if(ch=='%')words[j++]=ch;words[j]='\0';ch=program[i++];if(ch=='='){words[j++]=ch;words[j]='\0';flag=510;}else{i--;flag=511;}break;}case',':{words[j]=ch;words[j+1]='\0';flag=512;break;}case'#':{words[j]=ch;words[j+1]='\0';flag=513;break;}case'@':{words[j]='#';flag=0;break;}default:{14flag=-1;break;}}returnflag;}main(){i=0;printf(pleaseinputaprogramendwith@);do{ch=getchar();program[i++]=ch;}while(ch!='@');i=0;do{flag=Scan(program);if(flag==200){printf((%2d,%4d),flag,number);}elseif(flag==-1){printf((%d,error),flag);}else{printf((%2d,%4s),flag,words);}}while(flag!=0);system(pause);}修改后源代码:#includestdio.h15#includestdlib.h#includestring.hinti,j,k,flag,number,status;/*statuswhichisusetojudgethestringiskeywordsornot!*/charch;charwords[10]={};charprogram[500];intScan(charprogram[]){char*keywords[15]={void,main,if,then,break,int,char,float,include,for,while,printf,scanf,define,double};number=0;status=0;j=0;ch=program[i++];/*Tohandlethelettles

1 / 26
下载文档,编辑使用

©2015-2020 m.777doc.com 三七文档.

备案号:鲁ICP备2024069028号-1 客服联系 QQ:2149211541

×
保存成功