資源描述:
《編譯原理-預(yù)測分析法(附源碼)》由會員上傳分享,免費在線閱讀,更多相關(guān)內(nèi)容在行業(yè)資料-天天文庫。
1、預(yù)測分析法實驗報告一、實驗項目名稱預(yù)測分析法二、實驗?zāi)康母鶕?jù)某一LL(1)文法編制調(diào)試預(yù)測分析程序,以便對任意輸入的符號串進行分析。本次實驗的目的主要是加深對預(yù)測分析法的理解。三、實驗環(huán)境Windows10MicrosoftVisualStudio2015四、實驗內(nèi)容本次實驗的LL(1)文法為表達式文法:E→E+T
2、TT→T*F
3、FF→i
4、(E)編寫識別表達式文法的合法句子的預(yù)測分析程序,對輸入的任意符號串,給出分析過程及分析結(jié)果。分析過程要求輸出步驟、分析棧、剩余輸入串和所用產(chǎn)生式。如果該符號串不是表達式文法的合法句子,要給出盡量詳細的錯誤提示五、源程序清單、測試數(shù)據(jù)、結(jié)果#include
5、#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)","",""}};t
6、ypedefstruct{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;ss->top++;ss->
7、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,chart){//查找預(yù)測分析表,并返回產(chǎn)生式
8、右部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>=0;i--){cout<9、voidpredict(Stackstack,stringinput)//預(yù)測分析總函數(shù){inta=1;charb;charctop;//當前棧頂符號charcinput;//當前輸入符號inti=0,j=0,count=0;interror=0;cout<<"步驟"<<'t'<<"棧"<<'t'<<"輸入緩沖區(qū)"<<'t'<<"所用的產(chǎn)生式"<10、"";ctop=getTop(&stack);cinput=input.at(i);if(isT(ctop))//棧頂符號為終結(jié)符{if(ctop==cinput){pop(&stack);i++;}else{error=1;break;}produce+=""";produce+=ctop;produce+=""匹配";}else//棧頂符號位非終結(jié)符{stringstr=isInPred(ctop,c