程序源代码:
一:头文件 lr.h
//LR 分析表
#include<stdio.h>
#include<stdlib.h>
//0--11 表示状态结点,21--26 表示规约标号,
//-1 表示 error(出错),12 表示 acc(接受)
int table[13][9] = {{ 5,-1,-1, 4,-1,-1, 1, 2, 3},\
{-1, 6,-1,-1,-1,12,-1,-1,-1},\
{-1,22, 7,-1,22,22,-1,-1,-1},\
{-1,24,24,-1,24,24,-1,-1,-1},\
{ 5,-1,-1, 4,-1,-1, 8, 2, 3},\
{-1,26,26,-1,26,26,-1,-1,-1},\
{ 5,-1,-1, 4,-1,-1,-1, 9, 3},\
{ 5,-1,-1, 4,-1,-1,-1,-1,10},\
{-1, 6,-1,-1,11,-1,-1,-1,-1},\
{-1,21, 7,-1,21,21,-1,-1,-1},\
{-1,23,23,-1,23,23,-1,-1,-1},\
{-1,25,25,-1,25,25,-1,-1,-1}};
//规约规则
struct rule{
char x; int y;
}r[6]={{'E',3},{'E',1},{'T',3},{'T',1},{'F',3},{'F',1}};
//输入字符
char index_char[9]={'i','+','*','(',')','#','E','T','F'};//
//获取 index_char[9]中元素的位置
int get_index_char(char i)
{
for(int j=0;j<9;j++)
{
if(index_char[j] == i)
return j;
}
return -1;
}
二:头文件 status_stack.h
#include<stdio.h>
#include<stdlib.h>
#define MAX 20
typedef struct{
int stack[MAX];
int top;
}status;
//初始化栈
void init_stack(status *p)
{
if( !p)
printf(" 初始化状态栈出错!");
p->top = -1;
}
//压栈
void push(status *p,int x)