www.pudn.com > bycksj.rar > aaa.cpp
//(1)S->aAcBe //(2)A->b //(3)A->Ab //(4)B->d #include#include #include #define stack_size 20 #define stackincrement 10 int p=0,q=0,zh,sh,x; char Vn[3]={'S','A','B'}; //非终结符 char Vt[6]={'a','c','e','b','d','#'}; //终结符 typedef struct { char *bottom; char *top; int size; // char data[stack_size]; }stack; int Initstack(stack &s) //初始化栈 { s.bottom=(char *)malloc(stack_size*sizeof(char)); if(!s.bottom) exit(-1); s.top=s.bottom; s.size=stack_size; return(1); } int push(stack &s,char e) //入栈 { //int i; if(s.top-s.bottom>=s.size){ s.bottom=(char *)realloc(s.bottom,(stack_size+stackincrement)*sizeof(char)); if(!s.bottom) exit(-1); s.top=s.bottom+s.size; s.size+=stackincrement; } *s.top++=e; cout< =s.size){ s.bottom=(char *)realloc(s.bottom,(stack_size+stackincrement)*sizeof(char)); if(!s.bottom) exit(-1); s.top=s.bottom+s.size; s.size+=stackincrement; } *s.top++=e; cout< >st[0]; while(st[i]!=';') { i++; cin>>st[i]; } Initstack(s); Initstack(t); push(s,'#'); push2(t,0); cout<<"\t"; while(ok==1) { ch=st[index]; index++; cout< 0&&action(q,6,y)<10) { push(s,ch); push2(t,zh); // q=zh; } if(action(q,6,y)>10&&action(q,6,y)<20) { switch(zh) { case 11: for(i=0;i<5;i++) { pop(s,&y);pop2(t,&x); } push(s,'S'); pop2(t,&e); push2(t,e); go(e,3,'S'); //查询GOTO表 push2(t,sh); //替换后的数字 index--; break; case 12: pop(s,&y);pop2(t,&x); push(s,'A'); pop2(t,&e); push2(t,e); go(e,3,'A'); push2(t,sh); index--; break; case 13: pop(s,&y); pop(s,&y); pop2(t,&x); pop2(t,&x); push(s,'A'); pop2(t,&e); push2(t,e); go(e,3,'A'); push2(t,sh); index--; break; case 14: pop(s,&y); pop2(t,&x); push(s,'B'); pop2(t,&e); push2(t,e); go(e,3,'B'); push2(t,sh); index--; break; } } if(action(q,6,y)==20) { cout<<"分析成功"<