1、char snum126=/数字表5,1,1,4,2,1,3,6,5,3,2,0,2,2,7,2,2,2,4,4,4,4,4,4,6,6,6,6,6,6,3,6,5,3,11,4,1,1,7,1,1,1,3,3,3,3,3,3,5,5,5,5,5,5int go2123=/goto表1,2,3,0,0,0,8,2,3,0,9,3,0,0,10,0,0,0void action(int i,char *&a,char &how,int &num,char &A,int &b,int &k)/action函数i,a int j;/,r;char s1,s2,s3;char v; switch(*a
2、) case i: /case d j=0;break;+ j=1;* j=2;( j=3;) j=4;# j=5; default: j=-1; if(j!=-1) how=symij; num=snumij; if(how=) switch(num) case 1: A=E,b=3; coutE1+T规约endl; s1=symbol2.top(); symbol2.pop(); s2=symbol2.top();s3=symbol2.top(); symbol2.push(k+48);couttk=(v=(isdigit(s3)?t )s3s2;cout(v=(isdigit(s1)?s
3、1T1*F规约s1=symbol2.top(); case 4: F规约 case 5:F 按F-(E)规约 case 6:id规约 default: int go(int t,char A)/gotot,A switch(A) case return go2t0; return go2t1; return go2t2;void error(int i,int j,char *&a)/error显示函数 couterror switch(j) case 1:/期望输入id或左括号,但是碰到+,*,或$,就假设已经输入id了,转到状态5 /state.push(5); /symbol.push(
4、);/必须有这个,如果假设输入id的话,符号栈里必须有. coutsen; a=sen; state.push(0);/先输入0状态 while(*a!=0 b=0;num=0;how=;A= s=state.top(); q=*a; if(isalpha(*a)*a= action(s,a,how,num,A,b,k); if(how=)/移进 cout移进 symbol.push(*a); state.push(num);if (q!&q!symbol2.push(q); a+; else if(how=)/规约 for(int i=0;ib;i+) if(!state.empty() state.pop();symbol.empty() symbol.pop(); int t=state.top(); symbol.push(A); state.push(go(t,A);)/接受 cout成功接受 break; else error(s,num,a);/错误显示 输入有误,重新输入! break; return 0;