有人能帮我改一改这个算符优先的语法分析程序吗?

iceandfire 2002-05-12 04:44:27
#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;
}

...全文
344 24 打赏 收藏 转发到动态 举报
写回复
用AI写文章
24 条回复
切换为时间正序
请发表友善的回复…
发表回复
unbounded 2002-07-13
  • 打赏
  • 举报
回复
代码已发送到你的邮箱~可以给分了吧
yinguoyi 2002-05-15
  • 打赏
  • 举报
回复
程序太乱了。没办法改。
carbon107 2002-05-15
  • 打赏
  • 举报
回复
我做过这样的程序明天给您答案
dy_kiss 2002-05-15
  • 打赏
  • 举报
回复
先收藏,再接着想办法解决吧~
iceandfire 2002-05-15
  • 打赏
  • 举报
回复
那个谁~~你不是很懂算符优先算法吧~~不要说的那么不客气嘛~~我是虚心请教的~~
eion 2002-05-15
  • 打赏
  • 举报
回复



把你的目的、解题思路、问题、自己的判断过程等都贴出来,你让别人给你看,什么都没有,怎么看?还不如给你重写得了!!!!!!!!!!!!!!!!!!!1



step_by_step 2002-05-15
  • 打赏
  • 举报
回复
自己好好看看数据结构方面的书吧
iceneve 2002-05-14
  • 打赏
  • 举报
回复
参见《数据结构》清华严蔚敏第二版关于栈的一节。
iceandfire 2002-05-14
  • 打赏
  • 举报
回复
这个算法要实现的功能是,比如输入i+i#

应该经过这些步骤:
NUM S REL TOK REST ACTION
1 # < i +i# 0
2 #i > + i# 1
3 #F < + i# 0
4 #F+ < i # 0
5 #F+i > # 1
6 #F+F > # 1
7 #F = #

上面的是在屏幕上显示的结果~

现在只要求实现如下文法的算符优先算法
E→ E+T∣T
T→ T*F∣F
F→ (E) ∣i

有高手解决吗?
sandwish2000 2002-05-14
  • 打赏
  • 举报
回复
我也是 !我在写算SLR(1)分析程序!
qq:22550404
awinder 2002-05-14
  • 打赏
  • 举报
回复
我也在写算符优先的语法分析程序
qq 6569541
hisailer 2002-05-14
  • 打赏
  • 举报
回复
怎么回事啊 ,我发现编译的时候有三个错误!!
zzhouj 2002-05-14
  • 打赏
  • 举报
回复
请问你这个程序需要实现的功能是什么呢?你不说的话,我们看起来很吃力的。
zzhouj 2002-05-14
  • 打赏
  • 举报
回复
你需要该哪里呢?是不是不能用,还是有些bug呢?
MSVCer 2002-05-13
  • 打赏
  • 举报
回复
太长了,每天忙的要死
iceandfire 2002-05-13
  • 打赏
  • 举报
回复
没有会编译原理的C高手吗?
superct 2002-05-12
  • 打赏
  • 举报
回复
你需要改什么?能不能把问题说清楚?
iceandfire 2002-05-12
  • 打赏
  • 举报
回复
谁能帮忙看看?100分赠送~~
wwwjacob 2002-05-12
  • 打赏
  • 举报
回复
也发给我一份吧!didadi163@163.com
3X
iceandfire 2002-05-12
  • 打赏
  • 举报
回复
我的邮箱是
iceandfire@163.com
加载更多回复(4)

69,373

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧