词法分析程序生成实验

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

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

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

资源描述

无符号数的词法分析程序一、实验课程的性质、目的和任务1.培养学生初步掌握编译原理实验的技能。2.验证所学理论、巩固所学知识并加深理解。3.对学生进行实验研究的基本训练。二、实验内容掌握词法分析的基本思想,并用高级语言编写无符号数的词法分析程序。三、实验要求从键盘上输入一串字符(包括字母、数字等),最后以“;”结束,编写程序识别出其中的无符号数。四、无符号数语法规则无符号数→无符号实数│无符号整数无符号实数→无符号整数.数字串[E比例因子]│无符号整数E比例因子比例因子→有符号整数有符号整数→[+│-]无符号整数无符号整数→数字串数字串→数字{数字}数字→0123......9五、实验流程图开始0=w,p,j1=e数字否出错数值=〉dw*10+d=w取下一字符数字否是‘.’否是‘E’否退一字符‘整型’=〉CJ1取下一字符数字否出错数值=〉dw*10+d=wj+1=j取下一字符数字否是‘E’否取下一字符是‘-’否是‘+’否数字否数值=〉dp*10+d=p取下一字符数字否退一字符‘实型’=CJ1W*10C*P-J=CJ2出口-1=e取下一字符出错YNNNNYNYYNNYYNNYNYYNYYY六、实验源代码#includestdio.h#includestdlib.h#includeerrno.h#includectype.h#includemath.h#defineN50charUnsignedNumber[N];/*用来储存提取出来的无符号数字符串*//*识别已经提取出来的无符号数字符串*/voidReadUnsignedNumber(){intw=0;intp=0;intj=0;inti=0;shorte=1;shortd=0;doubleDataValue;while(isdigit(UnsignedNumber[i])){d=UnsignedNumber[i]-48;w=w*10+d;i++;}if(UnsignedNumber[i]=='.'){i++;while(isdigit(UnsignedNumber[i])){d=UnsignedNumber[i]-48;w=w*10+d;j++;i++;}if(UnsignedNumber[i]=='\0'){DataValue=w*pow(10,e*p-j);printf(Thevalueof%sis%f\n,UnsignedNumber,DataValue);}elseif(UnsignedNumber[i]=='e'||UnsignedNumber[i]=='E'){i++;if(UnsignedNumber[i]=='-'){e=-1;i++;}elseif(UnsignedNumber[i]=='+'){i++;}do{d=UnsignedNumber[i]-48;p=p*10+d;i++;}while(isdigit(UnsignedNumber[i]));DataValue=w*pow(10,e*p-j);printf(Thevalueof%sis%f\n,UnsignedNumber,DataValue);}}elseif(UnsignedNumber[i]=='e'||UnsignedNumber[i]=='E'){i++;if(UnsignedNumber[i]=='-'){e=-1;i++;}elseif(UnsignedNumber[i]=='+'){i++;}do{d=UnsignedNumber[i]-48;p=p*10+d;i++;}while(isdigit(UnsignedNumber[i]));DataValue=w*pow(10,e*p-j);printf(Thevalueof%sis%f\n,UnsignedNumber,DataValue);}else{DataValue=w;printf(Thevalueof%sis%f\n,UnsignedNumber,DataValue);}if(errno==ERANGE){printf(Overflowconditionoccurred!\n);}}intmain(){charch;inti;intflag=0;printf(Pleaseinputthedatawith';'toend!\n);ch=getchar();/*扫描输入的字符串,从中提取无符号数字符串*/while(ch!=';'){i=0;while(!isdigit(ch)){if(ch==';'){break;}ch=getchar();}while(isdigit(ch)){UnsignedNumber[i]=ch;i++;ch=getchar();}if(ch=='.'){UnsignedNumber[i]=ch;i++;ch=getchar();while(isdigit(ch)){UnsignedNumber[i]=ch;i++;ch=getchar();}if(ch=='e'||ch=='E'){UnsignedNumber[i]=ch;i++;ch=getchar();if(!isdigit(ch)){if(ch=='+'||ch=='-'){UnsignedNumber[i]=ch;i++;ch=getchar();if(!isdigit(ch)){i--;UnsignedNumber[i]='\0';}else{do{UnsignedNumber[i]=ch;i++;ch=getchar();}while(isdigit(ch));if(ch=='.'){printf(Thedatayouinputiswrong!\n);exit(1);}else{UnsignedNumber[i]='\0';}}}else{i--;UnsignedNumber[i]='\0';}}else{do{UnsignedNumber[i]=ch;i++;ch=getchar();}while(isdigit(ch));if(ch=='.'){printf(Thedatayouinputiswrong!\n);exit(1);}else{UnsignedNumber[i]='\0';}}}else{UnsignedNumber[i]='\0';}}elseif(ch=='e'||ch=='E'){UnsignedNumber[i]=ch;i++;ch=getchar();if(!isdigit(ch)){if(ch=='+'||ch=='-'){UnsignedNumber[i]=ch;i++;ch=getchar();if(!isdigit(ch)){i--;UnsignedNumber[i]='\0';}else{do{UnsignedNumber[i]=ch;i++;ch=getchar();}while(isdigit(ch));if(ch=='.'){printf(Thedatayouinputiswrong!\n);exit(1);}else{UnsignedNumber[i]='\0';}}}else{UnsignedNumber[i]='\0';}}else{do{UnsignedNumber[i]=ch;i++;ch=getchar();}while(isdigit(ch));if(ch=='.'){printf(Thedatayouinputiswrong!\n);exit(1);}else{UnsignedNumber[i]='\0';}}}else{UnsignedNumber[i]='\0';}if(i!=0){ReadUnsignedNumber();flag=1;}if(flag==0){printf(Sorry!Thereisnounsignednumberinyourdata!\n);}}return0;}七、实验结果

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

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

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

×
保存成功