#include "stdio.h"
#include "string.h"
#define ACC -2
/****************************************/
#define sy_if 0
#define sy_then 1
#define sy_else 2
#define sy_while 3
#define sy_begin 4
#define sy_do 5
#define sy_end 6
#define a 7
#define semicolon 8 //分号
#define e 9
#define jinghao 10 //井号
#define S 11
#define L 12
#define tempsy 15
#define EA 18
#define EO 19
#define plus 34 //加号
#define minus 35 //减号
#define times 36 //乘号
#define division 37 //除号
#define becomes 38
#define op_and 39
#define op_or 40
#define op_not 41
#define rop 42
#define lparent 48
#define rparent 49
#define ident 56
#define intconst 57
/******************************************/
char ch='\0';
int count=0;
static char spelling[10]={""};
static char line[81]={""};
char *pline;
static char ntab1[100][10];
struct ntab
{
int tc;
int fc;
}ntab2[200];
int label=0;
struct rwords{
char sp[10];
int sy;
};
struct rwords reswords[10]={{"if",sy_if},
{"do",sy_do},
{"else",sy_else},
{"while",sy_while},
{"then",sy_then},
{"begin",sy_begin},
{"end",sy_end},
{"and",op_and},
{"or",op_or},
{"not",op_not}};
struct aa{
int sy1;
int pos;
}buf[1000],
n,
n1,
E,
sstack[100],
ibuf[100],
stack[1000];
struct aa oth;
struct fourexp{
char op[10];
struct aa arg1;
struct aa arg2;
int result;
}fexp[200];
int ssp=0;
struct aa *pbuf=buf;
int nlength=0;
int lnum=0;
int tt1=0;
FILE *cfile;
/*********************************************************/
int newt=0;
int nxq=100;
int lr;
int lr1;
int sp=0;
int stack1[100];
int sp1=0;
int num=0;
struct ll{
int nxq1;
int tc1;
int fc1;
}labelmark[10];
int labeltemp[10];
int pointmark=-1,pointtemp=-1;
int sign=0;
/***************** 程序语句的LR分析表 *****************/
static int action[19][13]=
/*0*/ {{2,-1,-1,3,4,-1,-1,5,-1,-1,10,1,-1},
/*1*/ {-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,ACC,-1,-1},
/*2*/ {-1,-1,-1,-1,-1,-1,-1,-1,-1,6,-1,-1,-1},
/*3*/ {-1,-1,-1,-1,-1,-1,-1,-1,-1,7,-1,-1,-1},
/*4*/ {2,-1,-1,3,4,-1,-1,5,-1,-1,-1,9,8},
/*5*/ {-1,-1,104,-1,-1,-1,104,-1,104,-1,104,-1,-1},
/*6*/ {-1,10,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1},
/*7*/ {-1,-1,-1,-1,-1,11,-1,-1,-1,-1,-1,-1,-1},
/*8*/ {-1,-1,-1,-1,-1,-1,12,-1,-1,-1,-1,-1,-1},
/*9*/ {-1,-1,-1,-1,-1,-1,105,-1,13,-1,-1,-1,-1},
/*10*/ {2,-1,-1,3,4,-1,-1,5,-1,-1,-1,14,-1},
/*11*/ {2,-1,-1,3,4,-1,-1,5,-1,-1,-1,15,-1},
/*12*/ {-1,-1,103,-1,-1,-1,103,-1,103,-1,103,-1,-1},
/*13*/ {2,-1,-1,3,4,-1,-1,5,-1,-1,-1,9,16},
/*14*/ {-1,-1,17,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1},
/*15*/ {-1,-1,102,-1,-1,-1,102,-1,102,-1,102,-1,-1},
/*16*/ {-1,-1,-1,-1,-1,-1,106,-1,-1,-1,-1,-1,-1},
/*17*/ {2,-1,-1,3,4,-1,-1,5,-1,-1,-1,18,-1},
/*18*/ {-1,-1,101,-1,-1,-1,101,-1,101,-1,101,-1,-1}};
/**************** 算术表达式的LR分析表 *******************/
//static int action1[10][7]=
///*0*/ {{3,-1,-1,2,-1,-1,1},
///*1*/ {-1,4,5,-1,-1,ACC,-1},
///*2*/ {3,-1,-1,2,-1,-1,6},
///*3*/ {-1,104,104,-1,104,104,-1},
///*4*/ {3,-1,-1,2,-1,-1,7},
///*5*/ {3,-1,-1,2,-1,-1,8},
///*6*/ {-1,4,5,-1,9,-1,-1},
///*7*/ {-1,101,5,-1,101,101,-1},
///*8*/ {-1,102,102,-1,102,102,-1},
///*9*/ {-1,103,103,-1,103,103,-1}};
static int action1[14][9]=
/*0*/ {{3,-1,-1,-1,-1,2,-1,-1,1},
/*1*/ {-1,4,5,6,7,-1,-1,ACC,-1},
/*2*/ {3,-1,-1,-1,-1,2,-1,-1,8},
/*3*/ {-1,106,106,106,106,-1,106,106,-1},
/*4*/ {3,-1,-1,-1,-1,2,-1,-1,9},
/*5*/ {3,-1,-1,-1,-1,2,-1,-1,10},
/*6*/ {3,-1,-1,-1,-1,2,-1,-1,11},
/*7*/ {3,-1,-1,-1,-1,2,-1,-1,12},
/*8*/ {-1,4,5,6,7,-1,13,-1,-1},
/*9*/ {-1,101,101,101,101,-1,101,101,-1},
/*10*/ {-1,102,102,102,102,-1,102,102,-1},
/*11*/ {-1,103,103,103,103,-1,103,103,-1},
/*12*/ {-1,104,104,104,104,-1,104,104,-1},
/*13*/ {-1,105,105,105,105,-1,105,105,-1}};
/***************布尔表达式的LR分析表 *********************/
static int action2[16][11]=
/*0*/ {{1,-1,4,-1,5,-1,-1,-1,13,7,8},
/*1*/ {-1,2,-1,101,-1,101,101,101,-1,-1,-1},
/*2*/ {3,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1},
/*3*/ {-1,-1,-1,102,-1,102,102,102,-1,-1,-1},
/*4*/ {1,-1,4,-1,5,-1,-1,-1,11,7,8},
/*5*/ {1,-1,4,-1,5,-1,-1,-1,6,7,8},
/*6*/ {-1,-1,-1,104,-1,9,10,104,-1,-1,-1},
/*7*/ {1,-1,4,-1,5,-1,-1,-1,14,7,8},
/*8*/ {1,-1,4,-1,5,-1,-1,-1,15,7,8},
/*9*/ {105,-1,105,-1,105,-1,-1,-1,-1,-1,-1},
/*10*/ {107,-1,107,-1,107,-1,-1,-1,-1,-1,-1},
/*11*/ {-1,-1,-1,12,-1,9,10,-1,-1,-1,-1},
/*12*/ {-1,-1,-1,103,-1,103,103,103,-1,-1,-1},
/*13*/ {-1,-1,-1,-1,-1,9,10,ACC,-1,-1,-1},
/*14*/ {-1,-1,-1,106,-1,9,10,106,-1,-1,-1},
/*15*/ {-1,-1,-1,108,-1,9,10,108,-1,-1,-1}};
/**************** 从文件读一行到缓冲区 ***********************/
readline()
{
char ch1;
pline=line;
ch1=getc(cfile);
while (ch1!='\n')
{
*pline=ch1;
pline++;
ch1=getc(cfile);
}
*pline='\0';
pline=line;
}
/***************** 从缓冲区读取一个字符 ************************/
readch()
{
if (ch=='\0')
{
readline();
lnum++;
}
ch=*pline;
pline++;
}
/****************** 标识符和关键字的识别 ************************/
find(char spel[])
{
int ss1=0;
int ii=0;
while((ss1==0)&&(ii<nlength))
{
if (!strcmp(spel,ntab1[ii])) ss1=1;
ii++;
}
if (ss1==1) return ii-1;
else return -1;
}
identifier()
{
int iii=0,j,k;
int ss=0;
k=0;
do
{
spelling[k]=ch;
k++;
readch();
}while(((ch>='a')&&(ch<='z'))||((ch>='0')&&(ch<='9')));
pline--;
spelling[k]='\0';
while((ss==0)&&(iii<10))
{
if (!strcmp(spelling,reswords[iii].sp)) ss=1;
iii++;
}
/*关键字匹配*/
if(ss==1)
{
buf[count].sy1=reswords[iii-1].sy;
}
else
{
buf[count].sy1=ident;
j=find(spelling);
if (j==-1)
{
buf[count].pos=tt1;
strcpy(ntab1[tt1],spelling);
tt1++;
nlength++;
}
else buf[count].pos=j;
}
count++;
for(k=0;k<10;k++) spelling[k]=' ';
}
/********************** 数字识别 ***************************/
number()
{
int ivalue=0;
int digit;
do
{
digit=ch-'0';
ivalue=ivalue*10+digit;
readch();
}while((ch>='0')&&(ch<='9'));
buf[count].sy1=intconst;
buf[count].pos=ivalue;
count++;
pline--;
}
/********************* 扫描主函数 ************************/
scan()
{
int i;
while(ch!='~')
{
switch (ch)
{
case ' ':
break;
case 'a':
case 'b':
case 'c':
case 'd':
case 'e':
case 'f':
case 'g':
case 'h':
case 'i':
case 'j':
case 'k':
case 'l':
case 'm':
case 'n':
case 'o':
case 'p':
case 'q':
case 'r':
case 's':
case 't':
case 'u':
case 'v':
case 'w':
case 'x':
case 'y':
case 'z':
identifier();
break;
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
number();
break;
case '<':
readch();
if(ch=='=')
{
buf[count].pos=0; /* '<='标志 */
}
else
{
if(ch=='>') buf[count].pos=4; /* '<>'标志 */
else
{
buf[count].pos=1; /* '<'标志 */
pline--;
}
}
buf[count].sy1=rop;
count++;
break;
case '>':
readch();
if(ch=='=')
{
buf[count].pos=2; /* '>='标志 */
}
else
{
buf[count].pos=3; /* '>'标志 */
pline--;
}
buf[count].sy1=rop;
count++;
break;
case '(':
buf[count].sy1=lparent;
count++;
break;
case ')':
buf[count].sy1=rparent;
count++;
break;
case '#':
buf[count].sy1=jinghao;
count++;
break;
case '+':
buf[count].sy1=plus;
count++;
break;
case '-':
buf[count].sy1=minus;
count++;
brea