百思不得其解,请高手解
这是一个LR分析器的程序,前面构造了一个文法,后面的void analyse()是用来产生规约配对的函数,函数编译没有任何错误,但是当我输入 i+i 这个规约串的时候,没有输出规约规则,我调试了一下,问题出在 红色那一行,s=0,state[stop]=0时 action[state[stop]].action=’'而不等于's',所以导致规则无法被识别进去,但是我看逻辑上没有错啊,蓝色那一行确实为's'啊,所以,求高手帮忙解决啊,感激不尽大家可粘贴到6.0中编译一下看下效果,这样能快速了解程序,也请各位高手能快速解决,拜谢了
#include <iostream>
using namespace std;
#include <string>
#include <stdlib.h>
#include <iomanip>
#define length 20
struct grammar
{
char left[64];
char right[16][64];
int n;
}gra[length];
void Initgra()
{
int i,j;
strcpy(gra[1].left,"E`");
strcpy(gra[2].left,"E");
strcpy(gra[3].left,"E");
strcpy(gra[4].left,"T");
strcpy(gra[5].left,"T");
strcpy(gra[6].left,"F");
strcpy(gra[7].left,"F");
strcpy(gra[1].right[0],"E");
strcpy(gra[2].right[0],"E");
strcpy(gra[2].right[1],"+");
strcpy(gra[2].right[2],"T");
strcpy(gra[3].right[0],"T");
strcpy(gra[4].right[0],"T");
strcpy(gra[4].right[1],"*");
strcpy(gra[4].right[2],"F");
strcpy(gra[5].right[0],"F");
strcpy(gra[6].right[0],"(");
strcpy(gra[6].right[1],"E");
strcpy(gra[6].right[2],")");
strcpy(gra[7].right[0],"i");
gra[1].n=1;
gra[2].n=3;
gra[3].n=1;
gra[4].n=3;
gra[5].n=1;
gra[6].n=3;
gra[7].n=1;
cout<<"文法:"<<endl;
for(i=1;i<8;i++)
{
cout<<gra.left<<"->";
for(j=0;j<gra.n;j++)
{
cout<<gra.right[j];
}
cout<<endl;
}
}
struct actionl
{
int num;
char action;
}action[16][16];
void Initaction()
{
int i,j;
for(i=0;i <length;i++)
{
for(j=0;j <length;j++)
{
action[j].action='0';
action[j].num=0;
}
}
action[0][0].action='s';
action[0][0].num=5;
action[0][3].action='s';
action[0][3].num=4;
action[1][1].action='s';
action[1][1].num=6;
action[1][5].action='a';
action[1][5].num=0;
action[2][1].action='r';
action[2][1].num=2;
action[2][2].action='s';
action[2][2].num=7;
action[2][4].action='r';
action[2][4].num=2;
action[2][5].action='r';
action[2][5].num=2;
action[3][1].action='r';
action[3][1].num=4;
action[3][2].action='r';
action[3][2].num=4;
action[3][4].action='r';
action[3][4].num=4;
action[3][5].action='r';
action[3][5].num=4;
action[4][0].action='s';
action[4][0].num=5;
action[4][3].action='s';
action[4][3].num=4;
action[5][1].action='r';
action[5][1].num=6;
action[5][2].action='r';
action[5][2].num=6;
action[5][4].action='r';
action[5][4].num=6;
action[5][5].action='r';
action[5][5].num=6;
action[6][0].action='s';
action[6][0].num=5;
action[6][3].action='s';
action[6][3].num=4;
action[7][0].action='s';
action[7][0].num=5;
action[7][3].action='s';
action[7][3].num=4;
action[8][1].action='s';
action[8][1].num=6;
action[8][4].action='s';
action[8][4].num=11;
action[9][1].action='r';
action[9][1].num=1;
action[9][2].action='s';
action[9][2].num=7;
action[9][4].action='r';
action[9][4].num=1;
action[9][5].action='r';
action[9][5].num=1;
action[10][1].action='r';
action[10][1].num=3;
action[10][2].action='r';
action[10][2].num=3;
action[10][4].action='r';
action[10][4].num=3;
action[10][5].action='r';
action[10][5].num=3;
action[11][1].action='r';
action[11][1].num=5;
action[11][2].action='r';
action[11][2].num=5;
action[11][4].action='r';
action[11][4].num=5;
action[11][5].action='r';
action[11][5].num=5;
cout<<" action表"<<endl;
cout<<" "<<" i"<<" +"<<" *"<<" ("<<" )"<<" #"<<endl;
for(i=0;i <12;i++)
{
cout<<setw(2)<<i;
for(j=0;j <6;j++)
{
cout<<setw(4) <<action[j].action
<<action[j].num ;
}
cout<<endl;
}
}
struct Goto
{
int to;
}go[16][16];
void GotoInit()
{
int i,j;
for(i=0;i <length;i++)
{
for(j=0;j <length;j++)
{
go[j].to=0;
}
}
go[0][0].to=1;
go[0][1].to=2;
go[0][2].to=3;
go[4][0].to=8;
go[4][1].to=2;
go[4][2].to=3;
go[6][1].to=9;
go[6][2].to=3;
go[7][2].to=10;
cout<<" goto表"<<endl;
cout<<" "<<" S"<<" A"<<" B"<<endl;
for(i=0;i <12;i++)
{
cout<<setw(2)<<i;
for(j=0;j <3;j++)
{
cout<<setw(4)<<go[j].to;
}
cout<<endl;
}
}
int action_col(char st)
{
if(st=='i') return 0;
else if(st=='+') return 1;
else if(st=='*') return 2;
else if(st=='(') return 3;
else if(st==')') return 4;
else if(st=='#') return 5;
else return -1;
}
int go_col(char str[64])
{
if(strcmp(str,"E")==0) return 0;
else if(strcmp(str,"T")==0) return 1;
else if(strcmp(str,"F")==0) return 2;
else return -1;
}
struct stack
{
char syb[64];
}stack[length];
int state[length];
int atop,stop;
void analyse()
{
char str[1024];
int m,j,len,s,t;
cout<<"请输入字符串:"<<endl;
while(cin>>str)
{
state[0]=0;
strcpy(stack[0].syb,"#");
strcat(str,"#");
atop=0;
stop=0;
len=strlen(str);
m=0;
while(m<len)
{
s=action_col(str[m]);
if(s==-1)
{
cout<<"error:"<<endl;
}
if(action[state[stop]][s].action=='s')
{
state[stop+1]=action[state[stop]].num;
stop++;
stack[atop].syb[0]=str[m];
atop++;
m++;
}
if(action[state[stop]].action=='r')
{
cout<<gra[action[state[stop]].num].left<<"->";
for(j=0;j<gra[action[state[stop]].num].n;j++)
{
cout<<gra[action[state[stop]].num].right[j];
}
cout<<endl;
t=gra[action[state[stop]].num].n;
s=go_col(gra[action[state[stop]].num].left);
atop -= t;
stop -= t;
state[stop+1]= go[state[stop]].to;
stop++;
strcpy(stack[atop+1].syb,gra[action[state[stop]].num].left);
atop++;
}
else if(action[state[stop]].action=='a')
{
cout<<"success! finish!"<<endl;
return;
}
}
}
}
void main()
{
Initgra();
Initaction();
GotoInit();
analyse();
system("pause");
[ 本帖最后由 上善若飞 于 2010-12-8 12:25 编辑 ]