资源描述:
《语义分析代码(模仿语法制导翻译)》由会员上传分享,免费在线阅读,更多相关内容在学术论文-天天文库。
1、#include#include#includeusingnamespacestd;stackstate;stacksymbol;stackval;charsen[50];charsym[12][6]={//符号表{'s','e','e','s','e','e'},{'e','s','e','e','e','a'},{'r','r','s','r','r','r'},{'r','r','r','r','r','r'},{'s','e','e','s','e','e'},{'r','r','r','r'
2、,'r','r'},{'s','e','e','s','e','e'},{'s','e','e','s','e','e'},{'e','s','e','e','s','e'},{'r','r','s','r','r','r'},{'r','r','r','r','r','r'},{'r','r','r','r','r','r'}};charsnum[12][6]={//数字表{5,1,1,4,2,1},{3,6,5,3,2,0},{2,2,7,2,2,2},{4,4,4,4,4,4},{5,1,1,4,2,1},{6,6,6,6,6,6},{5,1,1,4,2,1},{5,1,1,4
3、,2,1},{3,6,5,3,11,4},{1,1,7,1,1,1},{3,3,3,3,3,3},{5,5,5,5,5,5}};intgo2[12][3]={//goto表{1,2,3},{0,0,0},{0,0,0},{0,0,0},{8,2,3},{0,0,0},{0,9,3},{0,0,10},{0,0,0},{0,0,0},{0,0,0},{0,0,0}};voidaction(inti,char*&a,char&how,int&num,char&A,int&b)//action函数[i,a]{intj,r;switch(*a){case'i'://case'd':j=0;b
4、reak;case'+':j=1;break;case'*':j=2;break;case'(':j=3;break;case')':j=4;break;case'#':j=5;break;default:j=-1;break;}if(j!=-1){how=sym[i][j];num=snum[i][j];if(how=='r'){switch(num){case1:A='E',b=3;cout<<"按E->E1+T规约tE.val=E1.val+T.valt"<5、l.pop();val.push(r);cout<<"tt"<T规约tE.val=T.valtt"<T1*F规约tT.val=T1.val*F.valt"<6、<F规约tT.val=F.valtt"<(E)规约tF.val=E.valtt"<d
7、igit规约tF.val=digit.lexvalt"<