資源描述:
《編譯原理-預(yù)測(cè)分析法(附源碼).doc》由會(huì)員上傳分享,免費(fèi)在線閱讀,更多相關(guān)內(nèi)容在行業(yè)資料-天天文庫。
1、預(yù)測(cè)分析法實(shí)驗(yàn)報(bào)告一、實(shí)驗(yàn)項(xiàng)目名稱預(yù)測(cè)分析法二、實(shí)驗(yàn)?zāi)康母鶕?jù)某一LL(1)文法編制調(diào)試預(yù)測(cè)分析程序,以便對(duì)任意輸入的符號(hào)串進(jìn)行分析。本次實(shí)驗(yàn)的目的主要是加深對(duì)預(yù)測(cè)分析法的理解。三、實(shí)驗(yàn)環(huán)境Windows10MicrosoftVisualStudio2015四、實(shí)驗(yàn)內(nèi)容本次實(shí)驗(yàn)的LL(1)文法為表達(dá)式文法:E→E+T
2、TT→T*F
3、FF→i
4、(E)編寫識(shí)別表達(dá)式文法的合法句子的預(yù)測(cè)分析程序,對(duì)輸入的任意符號(hào)串,給出分析過程及分析結(jié)果。分析過程要求輸出步驟、分析棧、剩余輸入串和所用產(chǎn)生式。如果該符號(hào)串不是表達(dá)式文法的合法句子,要給出盡量詳細(xì)的錯(cuò)誤提示五、源程序清單、測(cè)試數(shù)據(jù)、結(jié)果#in
5、clude#includeusingnamespacestd;constintNUM=20;//初始化的棧的大小//非終結(jié)符數(shù)組集charVar[5]={'E','R','T','M','F'};//終結(jié)符數(shù)組集charTer[6]={'i','+','*','(',')','#'};stringpred[5][6]={{"TR","","","TR","",""},{"","+TR","","","@","@"},{"FM","","","FM","",""},{"","@","*FM","","@","@"},{"i","","","(E)"
6、,"",""}};typedefstruct{char*top;char*base;intstacksize;intnum;}Stack;//棧結(jié)構(gòu)體voidinit(Stack*ss){//初始化棧ss->base=(char*)malloc(NUM*sizeof(char));if(!ss->base)exit(1);ss->top=ss->base;ss->stacksize=NUM;ss->num=0;}voidpush(Stack*ss,charc){//入棧操作if(ss->top-ss->base>=ss->stacksize)exit(1);*(ss->top)=c
7、;ss->top++;ss->num++;}voidpop(Stack*ss){//出棧操作if(ss->top==ss->base)exit(1);ss->top--;ss->num--;}chargetTop(Stack*ss){//取得棧頂元素if(ss->top==ss->base)exit(1);return*(ss->top-1);}intisT(charc){//判斷是否為終結(jié)符inti=0;intret=0;for(i=0;i<6;i++){if(Ter[i]==c){ret=1;break;}}returnret;}stringisInPred(charv,cha
8、rt){//查找預(yù)測(cè)分析表,并返回產(chǎn)生式右部inti,j;for(i=0;i<5;i++){if(Var[i]==v)break;}for(j=0;j<6;j++){if(Ter[j]==t)break;}if(pred[i][j]!=""){returnpred[i][j];}elsereturn"";}voiddisplayStack(Stack*stack){//輸出分析站的內(nèi)容stringstr;inti=0;Stackss=*stack;while(ss.num!=0){str+=getTop(&ss);pop(&ss);}for(i=str.length()-1;i>=
9、0;i--){cout<10、p(&stack)!='#'){stringproduce="";ctop=getTop(&stack);cinput=input.at(i);if(isT(ctop))//棧頂符號(hào)為終結(jié)符{if(ctop==cinput){pop(&stack);i++;}else{error=1;break;}produce+=""";produce+=ctop;produce+=""匹配";}else//棧頂符號(hào)位非終結(jié)符{stringstr=isInPred(ctop,c