资源描述:
《预测分析法编译原理实验.docx》由会员上传分享,免费在线阅读,更多相关内容在行业资料-天天文库。
1、实验代码:#include#include#include#includechar*pro[][6]={"Te","","","Te","","","","+Te","","","$","$","Ft","","","Ft","","","","$","*Ft","","$","$","i","","","(E)","",""};#definemaxsize50typedefcharDateType;typedefstruct{DateTypedata[m
2、axsize];inttop;}SeqStack;//定义栈inti,now=0;DateTypee,string1[maxsize];voidInitSt(SeqStack*L)//初始化{L->top=0;}charpush(SeqStack*L,DateTypee)//入栈{if(L->top==maxsize){printf("ttt栈已满!");return0;}L->data[L->top]=e;L->top++;return1;}charpop(SeqStack*L)//出栈{i=L->top;e=L->da
3、ta[i-1];if(L->top==0){printf("ttt栈为空!");return0;}L->top--;return1;}DateTypegetTop(SeqStack*L)//取栈顶元素{DateTypex;i=L->top;if(L->top==0){printf("ttt栈为空!");}else{i--;x=L->data[i];}returnx;}voiddisplay(SeqStack*L)//显示元素{if(L->top==0){printf("ttt栈为空!");}else{f
4、or(i=0;itop;i++){printf("%c",L->data[i]);}}}voiddisplay1(){intj;for(j=now;;){printf("%c",string1[j]);j++;if(string1[j]==' ')break;}}introw(DateTypex)//列匹配{switch(x){case'i':return0;break;case'+':return1;break;case'*':return2;break;case'(':return3;break;case')':ret
5、urn4;break;case'#':return5;break;default:return-1;}}intcol(DateTypex)//行匹配{switch(x){case'E':return0;break;case'e':return1;break;case'T':return2;break;case't':return3;break;case'F':return4;break;default:return-1;}}voidproduction1(DateTypeb,DateTypet){intx,y;x=col(b);y=
6、row(t);printf("%c->%s",b,pro[x][y]);}voidmatch(SeqStack*L){charch;ch=getTop(L);printf(""%c"匹配",ch);pop(L);}voidtuidao(SeqStack*L){DateTypestackbot,*p;stackbot=getTop(L);p=&string1[now];if(*p==stackbot)match(L);elseproduction1(stackbot,*p);}intmain(){inttime,x,y,b;Dat
7、eTypeX,a,str[5];SeqStackL;time=0;charc[]={""};InitSt(&L);//初始化栈push(&L,'#');push(&L,'E');printf("请输入字符串:");gets(string1);one:X=getTop(&L);a=string1[now];b=row(X);if(b==0
8、
9、b==1
10、
11、b==2
12、
13、b==3
14、
15、b==4)gototwo;elsegotothree;two:if(X==a){time++;printf("%dt",time);display(&L);p
16、utchar('t');display1();putchar('t');tuidao(&L);putchar(10);now++;gotoone;}elsegotoerror1;three:if(X=='#'){if(X!=a)g