1、 四、实验结果与数据处理1文法分析表:2.程序分析:实验源程序:#includestdlib.hint Action126= 105,0,0,104,0,0, 0,106,0,0,0,-1, 0,52,107,0,52,52, 0,54,54,0,54,54, 105,0,0,104,0,0, 0,56,56,0,56,56, 0,106,0,0,111,0, 0,51,107,0,51,51, 0,53,53,0,53,53, 0,55,55,0,55,55; int Goto123= 1,2,3, 0,0,0, 8,2,3, 0,9,3, 0,0,10, 0,0,0 ; char Gram
2、mar2010=0; char VT10,VN10; char AVT6=i,+*()# char GVN3=ETF int vnNum,vtNum,stateNum=12; int VNum10; int grammarNum; typedef struct char *base; char *top; SymbolStack; int *base; int *top; StateStack; StateStack state; SymbolStack symbol; int ScanGrammar() FILE *fp=fopen(SLR文法.txt,r); FILE *tp; char
3、singleChar,nextChar; int i=0,j=0,k,count; while(!feof(fp) fscanf(fp,%c,&singleChar); if(singleChar=?) Grammarij=; break; n i+; j=0; continue;- tp=fp; fscanf(tp,nextChar); if(nextChar= fp=tp; continue; | Grammari+10=Grammari0; j=1; Grammarij=singleChar; if(singleChar=A&singleCharZ count=0; while(VNco
4、unt!=singleChar&VNcount! count+; if(VNcount= vnNum=count+1; if(singleChar=S j+; continue; VNcount=singleChar; else while(VTcount!VTcount! if(VTcount= VTcount=singleChar; vtNum=count+1; j+; printf(输入的文法:n for(k=0;k printf(,Grammarkj); j+; printf( count=0;VT: while(VTcount!%3c,VTcount); count+; VTcoun
5、t= vtNum=count+1;nVN: while(VNcount!,VNcount); / n%d %dn,vtNum,vnNum); fclose(fp); grammarNum=i+1; return i; int vNumCount() int i,j; for(i=0;igrammarNum;i+) j=1; while(Grammarij! VNumi=j;%3d,VNumi); printf( return 0;void InitStack() state.base=(int *)malloc(100*sizeof(int); if(!state.base)exit(1);
6、state.top=state.base; *state.top=0; symbol.base=(char *)malloc(100*sizeof(char);symbol.base)exit(1); symbol.top=symbol.base; *symbol.top=int Judge(int stateTop,char inputChar) stateNum; if(stateTop=i)break; for(j=0;jvtNum;j+) if(inputChar=AVTj)break; return Actionij;int GetGoto(int stateTop,char inp
7、utChar) vnNum; if(inputChar=GVNj)break; return Gotoij;int print(int count,int i,char Input,int action,int gt,int sign) int *p=state.base,stateNum; int j,jj; char *q=symbol.base,symbolNum;%dt,count); while(p!=state.top+1) stateNum=*p;%d,stateNum); p+;t while(q!=symbol.top+1) symbolNum=*q;,symbolNum);
8、 q+; j=i; jj=0; while(jjInputi=100) sign=1; action=ssValue-100; state.top+; *state.top=action; symbol.top+; *symbol.top=Inputi; i+; print(count,i,Input,action,0,sign);=50&ssValue100) sign=2; action=ssValue-50; gt=Pop(action); print(count,i,Input,action,gt,sign); count+;int main() ScanGrammar(); vNumCount(); InitStack(); Reduction();五、分析与讨论通过此次试验我学会了:掌握非递归预测分析的编程方法。成绩
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1