#include
#include
//
void error()
{
x09printf("error!n");
x09exit(1);
}
char judge(char x,char y)
{
x09int col;x09x09//分析表中的y所在的列号
x09switch (y)
x09{
x09case '+':
x09x09col=0;
x09x09break;
x09case '*':
x09x09col=1;
x09x09break;
x09case 'i':
x09x09col=2;
x09x09break;
x09case '(':
x09x09col=3;
x09x09break;
x09case ')':
x09x09col=4;
x09x09break;
x09case '#':
x09x09col=5;
x09x09break;
x09default:
x09x09printf("y isn't in the analysis List!");
x09}
x09switch(x)
x09{
x09case '+':
x09x09return ConstList[0][col];
x09case '*':
x09x09return ConstList[1][col];
x09case 'i':
x09x09return ConstList[2][col];
x09case '(':
x09x09return ConstList[3][col];
x09case ')':
x09x09return ConstList[4][col];
x09case '#':
x09x09return ConstList[5][col];
x09default:
x09x09printf(" x isn't in the analysis List!");
x09}
x09return '$';//错误信息
x09
}
char POP(struct stack *s)
{
x09char ch;
x09struct linkList *p;
x09if(0==s->num)
x09{
x09x09printf("pop error!n");
x09x09return 0;
x09}
x09else
x09{
x09x09s->num--;
x09x09ch=s->top->data;
x09x09p=s->top;
x09x09s->top=s->top->next;
x09x09free(p);
x09x09return ch;
x09x09
x09}
}
void PUSH(struct stack *s,char ch)
{
x09struct linkList *p;
x09if(NULL==s->top)
x09{
x09x09s->top=(struct linkList *)malloc (sizeof(struct linkList ));
x09x09s->top->data=ch;
x09x09s->top->next=NULL;
x09x09s->num=1;
x09}
x09else
x09{
x09x09p=(struct linkList *)malloc (sizeof(struct linkList ));
x09x09p->data=ch;
x09x09p->next=s->top;
x09x09s->top=p;
x09x09s->num++;
x09}
}
char head(struct stack *s)x09x09x09//取栈顶元素
{
x09if (s->num>0)
x09{
x09x09return s->top->data;
x09}
x09else
x09x09return 0;
}
void enStack_Queue(struct stack *s,char ch)x09x09//按入队方式入栈
{
x09struct linkList *p;
x09if (NULL==s->top)
x09{
x09x09s->top=(struct linkList *)malloc (sizeof(struct linkList ));
x09x09s->top->data=ch;
x09x09s->top->next=NULL;
x09x09s->num=1;
x09x09
x09}
x09else
x09{
x09x09p=s->top;
x09x09while (NULL!=p->next)
x09x09{
x09x09x09p=p->next;
x09x09}
x09x09p->next=(struct linkList *)malloc (sizeof(struct linkList ));
x09x09p->next->data=ch;
x09x09p->next->next=NULL;
x09x09s->num++;
x09x09
x09}
}
char getStack(struct stack *s,int j)
{
x09struct linkList *p;
x09int c;
x09int i=s->num-j;
x09if (s->num>=j)
x09{
x09x09p=s->top;
x09x09for (c=0;cnext;
x09x09}
x09x09return p->data;
x09}
x09else
x09x09return 0;
}
int isVT(char ch)x09x09x09//是否是终极符
{
x09int i;
x09for(i=0;inum;i>0;i--)
x09{
x09x09p=s->top;
x09x09for (c=0;cnext;
x09x09}
x09x09printf("%c",p->data);
x09}
x09printf("t");
x09
}
void showInput(struct stack *s)
{
x09struct linkList *p;
x09int i,c;
x09for (i=0;inum;i++)
x09{
x09x09p=s->top;
x09x09for (c=0;cnext;
x09x09}
x09x09printf("%c",p->data);
x09}
}
void main()
{
x09char ch;
x09char a,Q;
x09int k,j,pop_c,i,flag=0;
x09int relation=0;
x09struct stack *s_in=(struct stack *)malloc (sizeof(struct stack ));
x09struct stack *s_an=(struct stack *)malloc (sizeof(struct stack ));
x09s_an->top=NULL;
x09s_in->top=NULL;
x09printf("please input the string:");
x09
x09do
x09{x09
x09x09ch=getchar();
x09x09enStack_Queue(s_in,ch);
x09}while ('#'!=ch);
x09k=1;
x09PUSH(s_an,'#');
x09printf("栈t当前符号t剩余输入符t优先关系t移进或规约n");
x09do
x09{
x09x09a=POP(s_in);x09x09//当前输入符读入ax09x09
x09x09do
x09x09{
x09x09x09
x09x09x09showAnalysis(s_an);
x09x09x09printf("%ctt",a);
x09x09x09showInput(s_in);
x09x09x09if (isVT(getStack(s_an,k)))
x09x09x09{
x09x09x09x09j=k;
x09x09x09}
x09x09x09else
x09x09x09x09j=k-1;
x09x09x09if ('>'==judge(getStack(s_an,j),a))x09x09//s[j]>a
x09x09x09{
x09x09x09x09printf("tt%c",'>');
x09x09x09x09printf("tt规约n");
x09x09x09x09do
x09x09x09x09{
x09x09x09x09x09Q=getStack(s_an,j);
x09x09x09x09x09if (isVT(getStack(s_an,j-1)))
x09x09x09x09x09{
x09x09x09x09x09x09j--;
x09x09x09x09x09}
x09x09x09x09x09else
x09x09x09x09x09x09j=j-2;
x09x09x09x09} while (!('