您好,欢迎访问三七文档
当前位置:首页 > 商业/管理/HR > 质量控制/管理 > 编译原理实验:语义分析实验
实验四:语义分析一.实验目的:通过上机实习,加深对语法制导翻译原理的理解,掌握将语法分析所识别的语法成分变换为中间代码的语义翻译方法二.实验要求:采用的是递归下降语法制导翻译法,对算术表达式,赋值语进行语义分析并生成四元式序列。1.实验的输入和输出分别为以下:输入是语法分析后提供的正确的单词串,输出为三地址指令形式的四元式序列。例如:对于语句串begina:=2+3*4;x:=(a+b)/cend#输出的三地址指令如下所示:(1)t1=3*4;(2)t2=2+t1;(3)a=t2;(4)t3=a+b;(5)t4=t3/c;(6)x=t4;2.实验算法的思想为:(1)设置语义分析过程这其中用到的几个重要的函数的功能和代码如下面所列出的:1.emit(char*result,char*ag1,char*op,char*ag2)该函数的功能是生成一个三地址语句送到四元式表中。四元式表的结构如下:Struct{Charresult[8];Charag1[8];Charop[8];Charag2[8];}quad[20];2char*newtemp()该函数的功能是回送一个新的变量名,临时变量的名称产生的顺序依次为T1,T2,……..Char*newtemp(){Char*p;Charm[8];P=(char*)malloc(8);K++;Iota(k,m,10);Strcpy(p+1,m);P[0]=’t’;Return(p);}(2)主程序的示意图如图所示:设置初值调用Scanner()函数调用lrparser函数()输出四元组结束(3)函数lrparser在原来语法分析的基础上插入相应的语义分析动作,将输入串翻译成四元式序列,在实验中我们仅仅所作的是对表达式和赋值语句进行翻译。三,实验程序源码代码如下:#includestdio.h#includestring.h#includectype.hchartoken[20];charprog[100];charch;char*rwtab[6]={begin,if,then,while,do,end};intsyn,p,m,n,sum;#defineSTACK_SIZE100intcompute(intx,charc,inty);intstack_num[STACK_SIZE];intnum_top=-1;boolnotfull_num(){if(num_top==STACK_SIZE)returnfalse;elsereturntrue;}voidpush_num(intvalue){if(notfull_num()){num_top++;stack_num[num_top]=value;}}voidpop_num(){num_top--;}intnum_top_value(){intm=stack_num[num_top];//printf(sing=====%d\n,m);returnm;}boolempty_num(){if(num_top==-1)returntrue;elsereturnfalse;}charstack_operation[STACK_SIZE];intoperation_top=-1;boolnotfull_operation(){if(operation_top==STACK_SIZE)returnfalse;elsereturntrue;}voidpush_operation(charc){if(notfull_operation())stack_operation[++operation_top]=c;}voidpop_operation(){operation_top--;}charoperation_top_value(){returnstack_operation[operation_top];}boolempty_operation(){if(operation_top==-1)returntrue;elsereturnfalse;}intcompute(intx,charc,inty){intm;if(c=='+')m=x+y;elseif(c=='-')m=x-y;elseif(c=='*')m=x*y;elseif(c=='/')m=x/y;returnm;}struct{intresult;intag1;charop;intag2;}quad[20];voidscanner();intfindf(charc);intfindg(charc);intfindf(charc){if(c=='^')return7;elseif(c=='*')return5;elseif(c=='/')return5;elseif(c=='+')return3;elseif(c=='-')return3;elseif(c=='$')return0;}intfindg(charc){if(c=='^')return6;elseif(c=='*')return4;elseif(c=='/')return4;elseif(c=='+')return2;elseif(c=='-')return2;elseif(c=='$')return0;}voidmain(){p=0;printf(pleaseinputthestring\n);intquadsign=0;do{scanf(%c,&ch);//输入所需要的文字prog[p]=ch;p++;}while(ch!='#');p=0;push_operation('$');//printf(OK\n);do{scanner();/*case11:printf((11,%d),sum);break;case-1:printf(error);break;default:printf((%d,%s),syn,token);break;*/if(syn==11){push_num(sum);//printf(sum==%d\n,sum);sum=0;}elseif(syn==13||syn==14||syn==15||syn==16||syn==100){if(findf(operation_top_value())=findg(token[0]))push_operation(token[0]);else{do{quad[quadsign].ag2=num_top_value();pop_num();//printf(%d\n,quad[quadsign].ag2);for(inti=0;inum_top;i++)//printf(stack==%d\n,stack_num[i]);//输出的为栈中的元素quad[quadsign].op=operation_top_value();pop_operation();quad[quadsign].ag1=num_top_value();pop_num();//printf(%d\n,quad[quadsign].ag1);quad[quadsign].result=compute(quad[quadsign].ag1,quad[quadsign].op,quad[quadsign].ag2);//compute(intx,charc,inty);push_num(quad[quadsign].result);quadsign++;}while(!empty_operation()&&findf(operation_top_value())findg(token[0]));push_operation(token[0]);}}}while(syn!=0);for(inti=0;iquadsign;i++)printf(%d=%d%c%d\n,quad[i].result,quad[i].ag1,quad[i].op,quad[i].ag2);}voidscanner(){for(n=0;n8;n++)token[n]=0;ch=prog[p++];while(isspace(ch))ch=prog[p++];m=0;//设置token数组为空if(isalpha(ch)){while(isalnum(ch)){token[m++]=ch;ch=prog[p++];}token[m++]='\0';p--;syn=10;for(n=0;n6;n++){if(strcmp(token,rwtab[n])==0){syn=n+1;break;}}}elseif(isdigit(ch)){while(isdigit(ch)){sum=sum*10+ch-'0';ch=prog[p++];}p--;syn=11;}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=20;p--;}break;case'':token[m++]=ch;ch=prog[p++];if(ch=='='){syn=24;token[m++]=ch;}else{syn=23;p--;}break;case':':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;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=100;token[0]=ch;break;default:syn=-1;}}
本文标题:编译原理实验:语义分析实验
链接地址:https://www.777doc.com/doc-4254603 .html