这是个简单的某个文法的LL(1),可对 i+i*i# 这类匹配
为什么我输入有'i'的时候就回出现什么内存不可读
#include<string.h>
#include<stdlib.h>
#include<iostream.h>
char a[]={'E','e','T','t','F','A','M'};
char b[]={'i','+','-','*','/','(',')','#'};
char *f[7][8]={{"Te","0","0","0","0","Te","0","0"},{"0","ATe","ATe","0","0","0","k","k"},
{"Ft","0","0","0","0","Ft","0","0"},{"0","k","k","MFt","MFt","0","k","k"},
{"i","0","0","0","0","(E)","0","0"},{"0","+","-","0","0","0","0","0"},
{"0","0","0","*","/","0","0","0"}};
struct stack{
char s[20];
int t;};
typedef struct stack *pseqstack;
pseqstack create(void)
{ pseqstack pastack;
pastack=(pseqstack)malloc(sizeof(struct stack));
if(pastack==NULL)
cout<<"out of space"<<endl;
else pastack->t=-1;
return(pastack);
}
void push(pseqstack pastack,char x)
{ pastack->t=pastack->t+1;
int i=0;
i=pastack->t;
pastack->s[i]=x;
}
void pop(pseqstack pastack)
{if(pastack->t==-1)
cout<<"underflow"<<endl;
else
pastack->t=pastack->t-1;
}
char top(pseqstack pastack)
{return(pastack->s[pastack->t]);
}
void macth(pseqstack pastack,char *p)
{ int j=0,m=0,n=0,q=0;
char tp;
char temp[10];
for(;;)
{tp=top(pastack);
if(tp=='#')
{if(p[j]=='#')
cout<<"macth"<<endl;
else
cout<<"error"<<endl;
break;
}
if(tp=='i'||tp=='+'||tp=='-'||tp=='*'||tp=='/'||tp=='('||tp==')')
{
if(tp==p[j])
{pop(pastack);
j++;
continue;}
else
{cout<<"error"<<endl;
break;
}
}
for(m=0;m<7;m++)
{if(a[m]==tp)
break;}
for(n=0;n<8;n++)
{if(b[n]==p[j])
break;}
strcpy(temp,f[m][n]);
if(strcmp(temp,"k")==0)
{pop(pastack);
continue;
}
if(strcmp(temp,"0")==0)
{cout<<"error"<<endl;
break;
}
else
{for(n=0;temp[n]!='\0';n++)
{
q=q+1;
}
for(n=q-1;n>=0;n--)
{
push(pastack,temp[n]);
}
}
}
}
void main()
{ char ru[10];
char j;
pseqstack pk=create();
j='#';
push(pk,j);
j='E';
push(pk,j);
cout<<"shu ru zifu chuan: ";
cin>>ru;
macth(pk,ru);
}
[此贴子已经被作者于2007-3-20 19:22:55编辑过]