欢迎来到天天文库
浏览记录
ID:58831467
大小:51.00 KB
页数:6页
时间:2020-09-24
《编译原理课程设计期末实验报告编译原理中重要的有机联系的文法词法语法,作一个具体的了解和掌握.doc》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库。
1、编译原理课程设计[课程设计目的]使学生能通过课程设计,对编译原理中重要的、有机联系的文法、词法、语法,作一个具体的了解和掌握。[课程设计要求]要求对一个简单的数值表达式进行文法、词法、语法分析,并结合实验,写出课程设计报告。[课程设计时数]8学时。[课程设计内容]对一个简单的数值表达式进行文法、词法、语法分析。该表达式只含双目运算符:+、-、*、/ ,1位的数值和括号。一:词法部分实验1,词法源程序代码:#include#include#include2、h>charprog[80],token[8];charch;intsyn,p,m=0,n,row,sum=0;char*rwtab[6]={"begin","if","then","while","do","end"};voidscaner(){for(n=0;n<8;n++)token[n]=NULL;ch=prog[p++];while(ch==''){ch=prog[p];p++;}if((ch>='a'&&ch<='z')3、4、(ch>='A'&&ch<='Z')){m=0;while((ch>='05、'&&ch<='9')6、7、(ch>='a'&&ch<='z')8、9、(ch>='A'&&ch<='Z')){token[m++]=ch;ch=prog[p++];}token[m++]=' ';p--;syn=10;for(n=0;n<6;n++)if(strcmp(token,rwtab[n])==0){syn=n+1;break;}}elseif((ch>='0'&&ch<='9')){{sum=0;while((ch>='0'&&ch<='9')){sum=sum*10+ch-'0';ch=prog[p10、++];}}p--;syn=11;if(sum>32767)syn=-1;}elseswitch(ch){case'<':m=0;token[m++]=ch;ch=prog[p++];if(ch=='>'){syn=21;token[m++]=ch;}elseif(ch=='='){syn=22;token[m++]=ch;}else{syn=23;p--;}break;case'>':m=0;token[m++]=ch;ch=prog[p++];if(ch=='='){syn=24;token[m++]=c11、h;}else{syn=20;p--;}break;case':':m=0;token[m++]=ch;ch=prog[p++];if(ch=='='){syn=18;token[m++]=ch;}else{syn=17;p--;}break;case'*':syn=13;token[0]=ch;break;case'/':syn=14;token[0]=ch;break;case'+':syn=15;token[0]=ch;break;case'-':syn=16;token[0]=ch;break;cas12、e'=':syn=25;token[0]=ch;break;case';':syn=26;token[0]=ch;break;case'(':syn=27;token[0]=ch;break;case')':syn=28;token[0]=ch;break;case'#':syn=0;token[0]=ch;break;case'':syn=-2;break;default:syn=-1;break;}}voidmain(){p=0;row=1;cout<<"Pleaseinputstring:"<13、dl;do{cin.get(ch);prog[p++]=ch;}while(ch!='#');p=0;do{scaner();switch(syn){case11:cout<<"("<14、n!=0);}2,词法实验结果验证:(1)给定源程序beginx:=10;ifx>2thenx:=3*x+1/2;end#输出结果如下2,源程序(包括上式未有的while/,do以及判断错误语句),结果图如上:beginx<=$;whilea<2dob<>10-x;end#
2、h>charprog[80],token[8];charch;intsyn,p,m=0,n,row,sum=0;char*rwtab[6]={"begin","if","then","while","do","end"};voidscaner(){for(n=0;n<8;n++)token[n]=NULL;ch=prog[p++];while(ch==''){ch=prog[p];p++;}if((ch>='a'&&ch<='z')
3、
4、(ch>='A'&&ch<='Z')){m=0;while((ch>='0
5、'&&ch<='9')
6、
7、(ch>='a'&&ch<='z')
8、
9、(ch>='A'&&ch<='Z')){token[m++]=ch;ch=prog[p++];}token[m++]=' ';p--;syn=10;for(n=0;n<6;n++)if(strcmp(token,rwtab[n])==0){syn=n+1;break;}}elseif((ch>='0'&&ch<='9')){{sum=0;while((ch>='0'&&ch<='9')){sum=sum*10+ch-'0';ch=prog[p
10、++];}}p--;syn=11;if(sum>32767)syn=-1;}elseswitch(ch){case'<':m=0;token[m++]=ch;ch=prog[p++];if(ch=='>'){syn=21;token[m++]=ch;}elseif(ch=='='){syn=22;token[m++]=ch;}else{syn=23;p--;}break;case'>':m=0;token[m++]=ch;ch=prog[p++];if(ch=='='){syn=24;token[m++]=c
11、h;}else{syn=20;p--;}break;case':':m=0;token[m++]=ch;ch=prog[p++];if(ch=='='){syn=18;token[m++]=ch;}else{syn=17;p--;}break;case'*':syn=13;token[0]=ch;break;case'/':syn=14;token[0]=ch;break;case'+':syn=15;token[0]=ch;break;case'-':syn=16;token[0]=ch;break;cas
12、e'=':syn=25;token[0]=ch;break;case';':syn=26;token[0]=ch;break;case'(':syn=27;token[0]=ch;break;case')':syn=28;token[0]=ch;break;case'#':syn=0;token[0]=ch;break;case'':syn=-2;break;default:syn=-1;break;}}voidmain(){p=0;row=1;cout<<"Pleaseinputstring:"<13、dl;do{cin.get(ch);prog[p++]=ch;}while(ch!='#');p=0;do{scaner();switch(syn){case11:cout<<"("<14、n!=0);}2,词法实验结果验证:(1)给定源程序beginx:=10;ifx>2thenx:=3*x+1/2;end#输出结果如下2,源程序(包括上式未有的while/,do以及判断错误语句),结果图如上:beginx<=$;whilea<2dob<>10-x;end#
13、dl;do{cin.get(ch);prog[p++]=ch;}while(ch!='#');p=0;do{scaner();switch(syn){case11:cout<<"("<14、n!=0);}2,词法实验结果验证:(1)给定源程序beginx:=10;ifx>2thenx:=3*x+1/2;end#输出结果如下2,源程序(包括上式未有的while/,do以及判断错误语句),结果图如上:beginx<=$;whilea<2dob<>10-x;end#
14、n!=0);}2,词法实验结果验证:(1)给定源程序beginx:=10;ifx>2thenx:=3*x+1/2;end#输出结果如下2,源程序(包括上式未有的while/,do以及判断错误语句),结果图如上:beginx<=$;whilea<2dob<>10-x;end#
此文档下载收益归作者所有