LR(0)语法分析

时间:2021-11-04 04:45:06
 # include <stdio.h>
# include <string.h> //存储LR(0)分析表
struct node
{
char ch;
int num;
};
struct node table[]={ {'s',},{'t',},{'t',},{'s',},{'t',},{'t',},{'t',},{'t',},{'t',},
{'t',},{'s',},{'t',},{'t',},{'t',},{'a',},{'t',},{'t',},{'t',},
{'t',},{'r',},{'s',},{'t',},{'r',},{'r',},{'t',},{'t',},{'t',},
{'t',},{'r',},{'r',},{'t',},{'r',},{'r',},{'t',},{'t',},{'t',},
{'s',},{'t',},{'t',},{'s',},{'t',},{'t',},{'t',},{'t',},{'t',},
{'t',},{'r',},{'r',},{'t',},{'r',},{'r',},{'t',},{'t',},{'t',},
{'s',},{'t',},{'t',},{'s',},{'t',},{'t',},{'t',},{'t',},{'t',},
{'s',},{'t',},{'t',},{'s',},{'t',},{'t',},{'t',},{'t',},{'t',},
{'t',},{'s',},{'t',},{'t',},{'s',},{'t',},{'t',},{'t',},{'t',},
{'t',},{'r',},{'s',},{'t',},{'r',},{'r',},{'t',},{'t',},{'t',},
{'t',},{'r',},{'r',},{'t',},{'r',},{'r',},{'t',},{'t',},{'t',},
{'t',},{'r',},{'r',},{'t',},{'r',},{'r',},{'t',},{'t',},{'t',},
}; //符号栈以及状态栈
struct node1
{
int pop;
int data[];
char str[];
}q1; int total=; //步骤
int i; //输入串下标
int function(int a,char c,int temp); int main()
{ char ch[];//存储输入串 //栈初始化
q1.data[]=;
q1.pop=;
q1.str[]='#'; i=;
int temp; //下标转换
int aaa; //函数返回值,0代表输入串成功分析,1代表出错或者接受 gets(ch); //输入串; // 输出表头和初始化的状态
printf("步骤\t状态栈\t符号栈\t输入串\t动作\n");
printf("%d\t%d\t#\t%s\t",++total,q1.data[],ch); //循环输出中间过程
while()
{
temp=;
//面临不同的输入串采取不同的动作,由函数function实现
if(ch[i] == 'i')
{
temp=q1.data[q1.pop-]*+;
aaa=function(,'i',temp); }
else if(ch[i] == '+')
{
temp=q1.data[q1.pop-]*+;
aaa=function(,'+',temp); }
else if(ch[i] == '*')
{
temp=q1.data[q1.pop-]*+;
aaa=function(,'*',temp); }
else if(ch[i] == '(')
{
temp=q1.data[q1.pop-]*+;
aaa=function(,'(',temp); }
else if(ch[i] == ')')
{
temp=q1.data[q1.pop-]*+;
aaa=function(,')',temp); }
else if(ch[i] == '#')
{
temp=q1.data[q1.pop-]*+;
aaa=function(,'#',temp); }
if(aaa==)
break; //输出
printf("%c%d",table[temp].ch,table[temp].num);
printf("\n");
q1.str[q1.pop]='\0';
printf("%d\t",++total);
for(int k=;k<q1.pop;k++)
{
printf("%d",q1.data[k]);
}
printf("\t");
printf("%s\t",q1.str);
for(k=i;k<strlen(ch);k++)
printf("%c",ch[k]);
printf("\t");
}
return ;
} int function(int a,char c,int temp)
{ temp=q1.data[q1.pop-]*+a;
if(table[temp].ch=='s')
{
q1.data[q1.pop]=table[temp].num;
q1.str[q1.pop++]=c;
i++;
}
if(table[temp].ch=='r')
{
if(table[temp].num == )
{
int leag,flag;
q1.pop=q1.pop-;
q1.str[q1.pop]='E';
leag=q1.data[q1.pop-]*+;
q1.data[q1.pop]=table[leag].num;
q1.pop++;
}
else if(table[temp].num == )
{
int leag,flag;
q1.pop=q1.pop-;
q1.str[q1.pop]='E';
leag=q1.data[q1.pop-]*+;
q1.data[q1.pop]=table[leag].num;
q1.pop++;
}
else if(table[temp].num == )
{
int leag,flag;
q1.pop=q1.pop-;
q1.str[q1.pop]='T';
leag=q1.data[q1.pop-]*+;
q1.data[q1.pop]=table[leag].num;
q1.pop++;
}
else if(table[temp].num == )
{
int leag,flag;
q1.pop=q1.pop-;
q1.str[q1.pop]='T';
leag=q1.data[q1.pop-]*+;
q1.data[q1.pop]=table[leag].num;
q1.pop++;
}
else if(table[temp].num == )
{
int leag,flag;
q1.pop=q1.pop-;
q1.str[q1.pop]='F';
leag=q1.data[q1.pop-]*+;
q1.data[q1.pop]=table[leag].num;
q1.pop++;
}
else if(table[temp].num == )
{
int leag,flag;
q1.pop=q1.pop-;
q1.str[q1.pop]='F';
leag=q1.data[q1.pop-]*+;
q1.data[q1.pop]=table[leag].num;
q1.pop++;
}
else
{
printf("出错\n");
return ;
}
}
if(table[temp].ch=='a')
{
printf("接受!\n");
return ;
}
return ;
}