基于算符优先分析方法的语法制导翻译程序设计_编译课程设计报告书(编辑修改稿)内容摘要:
FTT | PFPF | iEP |)( 说明: i 为整型常数或者为标识符表示整型变量;使用中↑用 **表示。 (2)优先关系表 设优先关系表如表 12所示。 表 12 优先关系表 + * ↑ i ( ) + * ↑ i ( ) 9 二、设计(实验)正文 1. 算符优先分析流程图 Y N N Y N Y includeiostream includestring using namespace std。 const int MAX=100。 char cost[8][8]={39。 39。 ,39。 +39。 ,39。 *39。 ,39。 !39。 ,39。 i39。 ,39。 (39。 ,39。 )39。 ,39。 39。 , 39。 +39。 ,39。 39。 ,39。 39。 ,39。 39。 ,39。 39。 ,39。 39。 ,39。 39。 ,39。 39。 , 39。 *39。 ,39。 39。 ,39。 39。 ,39。 39。 ,39。 39。 ,39。 39。 ,39。 39。 ,39。 39。 , 39。 !39。 ,39。 39。 ,39。 39。 ,39。 39。 ,39。 39。 ,39。 39。 ,39。 39。 ,39。 39。 , 开始 输出优先关系表 输入字符串 ‘ ’压栈 当前字符为‘ ’。 ’ 栈 中 为“ N” 不是句子 出栈 是句子 结束 栈顶终结符与当前字符比较 大于。 规约 下一个字符 入栈 10 39。 i39。 ,39。 39。 ,39。 39。 ,39。 39。 ,39。 39。 ,39。 39。 ,39。 39。 ,39。 39。 , 39。 (39。 ,39。 39。 ,39。 39。 ,39。 39。 ,39。 39。 ,39。 39。 ,39。 =39。 ,39。 39。 , 39。 )39。 ,39。 39。 ,39。 39。 ,39。 39。 ,39。 39。 ,39。 39。 ,39。 39。 ,39。 39。 , 39。 39。 ,39。 39。 ,39。 39。 ,39。 39。 ,39。 39。 ,39。 39。 ,39。 39。 ,39。 =39。 }。 //the priority realiton graph class stack { int top。 char array[MAX]。 public: stack(){top=0。 } char GetTop() { return array[top1]。 } int Getsize() { return top。 } void PushStack(char c) { if(topMAX) array[top++]=c。 else coutover flow!endl。 } char *PopStack(int length) { if(top=length) { char *temp=new char[MAX]。 for(int i=0。 ilength。 i++) { temp[i]=array[toplength+i]。 } top=toplength。 temp[length]=39。 \039。 return temp。 } else { return NULL。 } 11 } char GetChar(int pos) { if((pos=0)amp。 amp。 (posMAX)) return array[pos]。 else return 39。 \039。 } void DispStack() { for(int i=0。 itop。 i++) { if (GetChar(i)==39。 !39。 ) { cout**。 cout39。 \b39。 } else coutGetChar(i)。 } if (thistop=8)cout39。 \t39。 else cout39。 \t39。 39。 \t39。 } }。 void DispCost() { for(int i=0。 i8。 i++) { for(int j=0。 j8。 j++) { if(cost[i][j]==39。 !39。 ) cout**39。 \t39。 else coutcost[i][j]39。 \t39。 } coutendl。 } } bool IsVT(char c) { bool flag=false。 for(int i=0。 i8。 i++) { 12 if(c==cost[0][i]) { flag=true。 } } if (((c=39。 a39。 )amp。 amp。 (c=39。 z39。 )) || ((c=39。 039。 )amp。 amp。 (c=39。 939。 ))) flag=true。 return flag。 } bool Num_Char(char c) { if (((c=39。 a39。 )amp。 amp。 (c=39。 z39。 )) || ((c=39。 039。 )amp。 amp。 (c=39。 939。 ))) return true。 else return false。 } int SearchCost(char ch1,char ch2)//set the realiton according to the cost { //0==equal , 1==above, 1==low, 2==NULL int i,j。 if(Num_Char(ch1)) ch1=39。 i39。 if(Num_Char(ch2)) ch2=39。 i39。 switch(ch1) { case 39。 +39。 :i=1。 break。 case 39。 *39。 :i=2。 break。 case 39。 !39。 :i=3。 break。 case 39。 i39。 :i=4。 break。 case 39。 (39。 :i=5。 break。 case 39。 )39。 :i=6。 break。 case 39。 39。 :i=7。 break。 } switch(ch2) { case 39。 +39。 :j=1。 break。 case 39。 *39。 :j=2。 break。 case 39。 !39。 :j=3。 break。 case 39。 i39。 :j=4。 break。 case 39。 (39。 :j=5。 break。 case 39。 )39。 :j=6。 break。 case 39。 39。 :j=7。 break。 } switch(cost[i][j]) 13 { case 39。 39。 :return 1。 break。 case 39。 39。 :return 1。 break。 case 39。 =39。 :return 0。 break。 default:return 2。 break。 } } void main() { char string[MAX]。 //for the sentence to be ananlyse char *ch=new char [MAX]。 //the mergeing string stack s。 //for the merge stack (39。 39。 )。 //init the stack char a。 //store the letter need to be judge int strp。 //point to the letter of the string int statop。 //point to the top of the stack int statopvt。 //point to the top VT cout*****优先关系表 :********endl。 DispCost()。 cout*********************************************************endl。 cout请输入一个字符串 (以 39。 39。 结束 ): endl。 (string,MAX)。 coutThe stack:39。 \t39。 The actionendl。 a=string[0]。 //init the pointers strp=0。 statop=()1。 statopvt=statop。 while(a!=39。 39。 ) //The analysing progress is as following: { //if the letter39。 s priority is litter than the top start merge a=string[strp]。 // get the letter if((a==39。 *39。 )amp。 amp。 (string[strp+1]==39。 *39。 )) { a=39。 !39。 strp++。 } if(IsVT((statop))) statopvt=statop。 else statopvt=statop1。 14 //if the letter39。 s priority is greater than the top the push it while(IsVT(a)amp。 amp。 (SearchCost((statopvt),a)==1))//the topvt is greater than the letter the can not push then merge { ()。 //show the stack before merging int highpos=statopvt,lowpos=highpos1。 //calculate the leng。基于算符优先分析方法的语法制导翻译程序设计_编译课程设计报告书(编辑修改稿)
阅读剩余 0%
本站所有文章资讯、展示的图片素材等内容均为注册用户上传(部分报媒/平媒内容转载自网络合作媒体),仅供学习参考。
用户通过本站上传、发布的任何内容的知识产权归属用户或原始著作权人所有。如有侵犯您的版权,请联系我们反馈本站将在三个工作日内改正。