有人能帮我改一改这个算符优先的语法分析程序吗?
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct Graph //文法
{
char str[5];
}Graph;
struct tag_out //输出
{
int num; //步骤
char s[10]; //栈
char Rel; //优先关系
char Tok; //当前字符
char Rest[10]; //剩余输入串
int Action; //动作,0代表移进,1代表归约
}out[50];
void getch();
void recoghead();
void recogtail();
char Ter[6]={'+','*','(',')','i','#'};
char Nter[3]={'E','T','F'};
char a[6][6]={{'>','<','<','>','<','>'},{'>','>','<','>','<','>'},
{'<','<','<','=','<',' '},{'>','>',' ','>',' ','>'},
{'>','>',' ','>',' ','>'},{'<','<','<',' ','<',' '}}; //优先关系矩阵
Graph graph1[8]={{"E"},{"E+T"},{"E+E"},{"T+T"},{"F+F"},{"T+F"},{"E+F"},{"T"}};
Graph graph2[8]={{"T"},{"T*F"},{"T*T"},{"E*E"},{"F*F"},{"E*T"},{"E*F"},{"F"}};
Graph graph3[5]={{"F"},{"(E)"},{"(T)"},{"(F)"},{"i"}};
char s[10];
char line[10];//存放输入串
char lpp[5];
static int top;
char ch;
char token; //存放下一输入符,也即out中的Tok
static int cc;
static int tail; //栈顶第一个终结符,也即lpp的尾
int m=0;
int flag2;
char Res[10];
int sum;
/*过程
主函数
recogtail()
recoghead()
match(lpp,n)
push
pop*/
void main()
{
//清屏
printf("G[s]:E->E+T|T\n"); /* 输出文法 */
printf(" T->T*F|F\n");
printf(" F->(E)|i\n");
printf("please input a string:\n");
getch();
top=0;
s[top]='#';//栈顶指针为'#'
printf("NUM S Rel Tok Rest Action\n");
recogtail();
}
void recogtail()
{
int i;
int flag1;
token=ch; //把下一字符,即输出中的当前字符,读入到token
out[m].Tok=token;
for(i=0;i<10;i++)
{
Res[i]=line[cc];
cc=cc+1;
}
sum=i;
//输出第一步分析
out[m].num=m;
printf("%d",out[m].num);//步骤
s[top+1]='\0';
strcpy(out[m].s,s);//栈
printf("<");//优先关系
printf("%c",out[m].Tok);//当前字符
Res[sum]='\0';
strcpy(out[m].Rest,Res);//剩余输入串
printf("0");//动作
//最大的循环
do
{
m=m+1;
getch();
token=ch;
for(i=0;i<6;i++)
{
if(s[top]==Ter[i])
tail=top;
else
tail=top-1;
}
i=-1;
do
{
i++;
}while(s[tail]!=Ter[i]);
flag1=i;
i=-1;
do
{
i++;
}while(token!=Ter[i]);
flag2=i;
if (a[flag1][flag2]=='<'||a[flag1][flag2]=='=')
{
top=top+1;
s[top]=token;
out[m].Rel=a[flag1][flag2];
out[m].Action=0;//push token,移进
//输出下一步分析
out[m].num=m;
printf("%d",out[m].num);//步骤
s[top+1]='\0';
strcpy(out[m].s,s);//栈
printf("%c",out[m].Rel);//优先关系
printf("%c",out[m].Tok);//当前字符
Res[sum]='\0';
strcpy(out[m].Rest,Res);//剩余输入串
printf("%d",out[m].Action);//动作
}
if (a[flag1][flag2]==' ')
printf("there is no relation between %c and %c\n",s[tail],token); //出错
if (a[flag1][flag2]=='>')
{
out[m].Rel=a[flag1][flag2];
out[m].Action=1;
recoghead(); //输入指针回退一个字符 ,规约
}
}while(top!=1);//继续取数,
if(top==1&&s[top]=='E')
printf("Success!\n");
else
printf("Error!\n");
}
void recoghead() //寻找lpp的头lower
{
int i;
int lower,upper;
int flag3,flag4,flag5;
char n;
//输出下一步分析
out[m].num=m;
printf("%d",out[m].num);//步骤
s[top+1]='\0';
strcpy(out[m].s,s);//栈
printf("%c",out[m].Rel);//优先关系
printf("%c",out[m].Tok);//当前字符
Res[sum]='\0';
strcpy(out[m].Rest,Res);//剩余输入串
printf("%d",out[m].Action);//动作
m=m+1;
lower=tail;
do
{
upper=lower;
for(i=0;i<6;i++)
{
if(s[lower-1]==Ter[i])
lower=lower-1;
else
lower=lower-2;
}
i=-1;
do
{
i++;
}while(s[lower]!=Ter[i]);
flag3=i;
do
{
i++;
}while(s[upper]!=Ter[i]);
flag4=i;
}while(a[flag3][flag4]=='>'||a[flag3][flag4]=='='); //直到s[lower]<s[upper];
for(i=0;i<10;i++)
{
lpp[i]=s[lower+1+i];
} //lpp='s[lower+1]....s[top]';
//call match(lpp,n);
for(i=1;i<8;i++)
{
if((strcmp(lpp,graph1[i].str))==0)//若匹配,把产生式的左部放入n
n=graph1[0].str;
else if((strcmp(lpp,graph2[i].str))==0)
n=graph2[0].str;
else if((strcmp(lpp,graph3[i].str))==0)
n=graph3[0].str;
else n=' ';
}
if(n==' ')//出错
{
printf("NO Match!\n");
//记住出错时刻和出错位置
}
else
{
top=lower;//pop lpp
top=top+1;//push n
s[top]=n;//n肯定是非终结符
i=-1;
do
{
i++;
}while(s[top-1]!=Ter[i]);//top-1才是终结符
flag5=i;
if(a[flag5][flag2]=='<'||a[flag5][flag2]=='=')
out[m].Action=0;
else if(a[flag5][flag2]=='>')
out[m].Action=1;
else
printf("No Match!");//出错
//输出一步分析
out[m].num=m;
printf("%d",out[m].num);//步骤
s[top+1]='\0';
strcpy(out[m].s,s);//栈
printf("%c",out[m].Tok);//当前字符
Res[sum]='\0';
strcpy(out[m].Rest,Res);//剩余输入串
printf("%d",out[m].Action);//动作
}
}
void getch()
{
int i=0;
do
{
scanf("%c",&line[i]);
i++;
}while(line[i-1]!='\n');
ch=line[cc];
cc=cc+1;
}