#include "string.h"
#include "stdio.h"
#include "conio.h"
#define MOVEIN 1
#define GUIYUE 2
#define ACC 3
#define OK 1
#define ERROR 0
#define MAXSH 7
#define MAXSHL 10
#define MAXINPUT 50
#define maxsize 100
int act;
int ip=0;
int line=0; /*line为要写的行号,全局变量*/
int writeok;
int right;
typedef struct
{int data[maxsize];
int top;
}stack;
int emptystack(stack *S)
{if(S->top==48&&S->data[S->top]==35)return(1); /*35 is '#'*/
else return(0);
}
int push(stack *S,int x)
{if(S->top>=maxsize-1)return(-1);
else{S->top++;
S->data[S->top]=x;
return(0);
}
}
int gettop(stack *S)
{return S->data[S->top];
}
int pop(stack *S)
{if(emptystack(S)){
printf("the stack is empty\n");
exit(1); }
else S->top--;
return S->data[S->top+1];
}
void initstack(stack *S)
{int i;
S->top=0;S->data[S->top]=35;
}
int analys(int s,int a)
{int hh,pos;
switch(a)
{case (int)'i':hh=0;break;
case (int)'+':hh=1;break;
case (int)'*':hh=2;break;
case (int)'(':hh=3;break;
case (int)')':hh=4;break;
case (int)'#':hh=5;break;
case (int)'E':hh=6;break;
case (int)'T':hh=7;break;
case (int)'F':hh=8;break;
default:{printf(" \n analys()分析发现不该有的字符 %c !(位置:%d)",a,ip+1);
writeerror('0',"\n............分析出现错误!!!");
writeerror(a,"\n 错误类型: 不该有字符 ");
return ERROR;
}
}
pos=(s-48)*10+hh;
switch(pos)
{case 3:
case 43:
case 63:
case 73:act=4;return MOVEIN;
case 0:
case 40:
case 60:
case 70:act=5;return MOVEIN;
case 11:
case 81: act=6;return MOVEIN;
case 92:
case 22:act=7;return MOVEIN;
case 84:act=11;return MOVEIN;
/*-------------------------------------------*/
case 91:
case 94:
case 95:
act=1;return GUIYUE;
case 21:
case 24:
case 25:
act=2;return GUIYUE;
case 101:
case 102:
case 104:
case 105:act=3;return GUIYUE;
case 31:
case 32:
case 34:
case 35:act=4;return GUIYUE;
case 111:
case 112:
case 114:
case 115:act=5;return GUIYUE;
case 51:
case 52:
case 54:
case 55:act=6;return GUIYUE;
case 15:return ACC;
case 6:return 1;
case 7:
case 47:return 2;
case 8:
case 48:
case 68:return 3;
case 46:return 8;
case 67:return 9;
case 78:return 10;
default:{if(a=='#')printf("");
else printf(" \n analys() 分析发现字符 %c 不是所期望的!(位置:%d)",a,ip+1);
writeerror('0',"\n ...........分析出现错误!!!");
writeerror(a,"\n 错误类型: 字符 ");
writeerror('0'," 不是所期望的! ");
return ERROR;
}
}
}
int writefile(int a,char *st)
{FILE *fp;
fp=fopen("an_slr.txt","a");
if(fp==0){printf("\nwrite error!!");writeok=0;}
else{if(a==-1)
{fprintf(fp," %s ",st); /*若a==-1则为添加的注释*/
}
else if(a>=0) fprintf(fp,"\n step: %02d , %s",a,st);
writeok=1;
fclose(fp);}
return writeok;
}
int writeerror(char a,char *st) /*错误类型文件*/
{FILE *fpp;
fpp=fopen("an_slr.txt","a");
if(fpp==0){printf("\nwrite error!!");writeok=0;}
else{if(a=='0') fprintf(fpp," %s ",st); /*若a=='0' 则为添加的注释*/
else fprintf(fpp," %s \'%c\'(位置:%d) ",st,a,ip+1);
writeok=1;
fclose(fpp);}
return writeok;
}
main()
{int an,flag=1,action,lenr;
char a,w[MAXINPUT];
int len,s,ss,aa,ana;
stack *st;
char r[MAXSH][MAXSHL]; /*初始化产生式*/
strcpy(r[0],"S->E");
strcpy(r[1],"E->E+T");
strcpy(r[2],"E->T");
strcpy(r[3],"T->T*F");
strcpy(r[4],"T->F");
strcpy(r[5],"F->(E)");
strcpy(r[6],"F->i");
clrscr();
printf("\nplease input your string:\n");
gets(w);
len=strlen(w);
w[len]='#';
w[len+1]='\0';
initstack(st);
push(st,48); /* (int)0 进栈*/
writefile(-1," ");
writefile(-1,w);
writefile(-1," ");
writefile(-2," ");
do{
s=gettop(st);
aa=(int)w[ip];
action=analys(s,aa);
if(action==MOVEIN)
{ss=48+act;
push(st,aa);
push(st,ss); /* if ss=4 int =52 */
ip++;
}
else if(action==GUIYUE)
{lenr=strlen(r[act])-3;
for(an=0;an<=2*lenr-1;an++)
pop(st); /* #0 */
s=gettop(st); /* s=0 */
push(st,(int)r[act][0]);
/*将产生式左端 F 进栈 */
ana=analys(s,(int)r[act][0])+48;
if(ana>59)printf("\分析出错:ana>59!!!");
push(st,ana);
/*analys(s,aa)即为goto(s',aa) */
if((line+1)%20==0)
{printf("\nThis screen is full,press any key to continue!!!");
getche();
clrscr();
}
printf(" step %02d: %s\n",line++,r[act]);
writefile(line,r[act]);
}
else if(action==ACC)
{flag=0;
right=1;}
else if(action==ERROR)
{flag=0;
right=0;
} /*接受 成功*/
else
{flag=0;
right=0;
} /* 出错*/
}while(flag==1);
if(right==1)printf("\nok,输入串 %s 为可接受串!!",w);
if(right==0)printf("\nsorry,输入串 %s 分析出错!!",w);
if(writeok==1){printf("\nAnWin soft have wrote a file an_slr.txt");
if(right==1)writefile(-1,"\n成功");
}
}