




版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認領(lǐng)
文檔簡介
預(yù)測分析算法的設(shè)計與實現(xiàn)刪除預(yù)測分析算法的設(shè)計與實現(xiàn)程序代碼:#include#includeefineefineefineefineefineefineMaxRuleNum8MaxVnNum5MaxVtNum5MaxStackDepth20MaxPLength20MaxStLength50{pRNode/*產(chǎn)生式右部結(jié)構(gòu)*/rCursor;pRNodenext;{pNoderLength;/*右部長度*/structpRNode*rHead;/*右部結(jié)點頭指針*//*非終結(jié)符集*/+Vn/*非終結(jié)符集*/+vnNum/*終結(jié)符集*/+Vt/*終結(jié)符集*/+vtNumpNodeP[MaxRuleNum];PNum;buffer[MaxPLength+1];charch;charst[MaxStLength];/*要分析的符號串*/{oidoiddcollectNodenVt;ShowAT();/*輸出分析表*/Identify(char*st);ShowStack();Pop();Push(intr);{main()todo,ch;InputVn();InputVt();InputPgetchar();FirstFollow();ShowCollect(first);printf("所得followShowCollect(follow);CreateAT();ShowAT();todo=while('y'printf("\ntodo=while('y'==todo)getchar();!=todo&&{printf("\n(y/n)");todo=getchar();}/n):");if('y'==todo){inti;printf("請輸入符號串(以#結(jié)束):");ch=getchar();i=0;while('#'!=ch&&i<MaxStLength){st[i]=ch;}}}getchar();}void{vnNum===for(i=0;i<=MaxVnNum;i++)Vn[i]='\0';for(i=0;i<=MaxVtNum;i++)Vt[i]='\0';for(i=0;i<MaxRuleNum;i++){P[i].lCursorP[i].rHeadP[i].rLength===NULL;NULL;}PNum=0;for(i=0;i<=MaxPLength;i++)}-1;}intIndexCh(charch){intn;n=0;/*isVn*/while(ch!=Vn[n]&&'\0'returnnreturnn=while(chreturnreturn0;+n;*isVt[n]Vt[n])Vt*/&&}/*輸出Vn或Vt的內(nèi)容*/ShowChArray(charShowChArray(char*collect){intk=0;while('\0'!=collect[k]){printf("%c",collect[k++]);}printf("\n");}/*輸入非終結(jié)符*/voidInputVn(){{Vn[n++]='\0';}n=0;while(('#'!=ch)&&(n{if(''!=ch&&'\n'1==IndexCh(ch)){<Vn[n])Vt[n])MaxVnNum))ch&&-Vn[n++]=ch;vnNum++;}ch=getchar();}Vn[n]='#';/*以"#"標志結(jié)束用于判斷長度是否合法*/k=n;if('#'!=ch){if('#'!=(ch=getchar())){while('#'!=(ch=getchar()));ninErr=1;continue;}{}scanf("%c",&ch);}if('n'==ch){inErr=1;}{inErr=0;}}}/*輸入終結(jié)符*/{tVt==n,k;charch;while(inErr){printf("以#號結(jié)束:\n");n=='/*初始化數(shù)組*/while(n<MaxVtNum){Vt[n++]='\0';}n=0;while(('#'!=ch)&&(n<MaxVtNum)){if(''!=ch&&'\n'!=ch&&-1==IndexCh(ch)){Vt[n++]=ch;vtNum++;}ch=getchar();}Vt[n]='#';k=n;if('#'!=ch){if('#'!=(ch=getchar())){while('#'!=(ch=getchar()));ninErr=1;continue;}}Vt[k]='\0';ShowChArray(Vt);ch='';while('y'{{&&'n'!=ch)}scanf("%c",&ch);}if('n'==ch){inErr=1;}{inErr=0;}}}/*產(chǎn)生式輸入*/voidInputP(){charch;inti=0,n,num;scanf("%d",&num);PNum=num;getchar();/*消除回車符*/printf("\n請輸入文法的%d個產(chǎn)生式,并以回車分隔每個產(chǎn)生式:",num);printf("\n");while(i<num){/*初始化*/for(n=0;n<MaxPLength;n++)buffer[n]='\0';/*輸入產(chǎn)生式串*/ch='';n=0;while('\n'!=(ch=getchar())&&n<MaxPLeng{if(''!=ch)buffer[n++]=ch;}buffer[n]='\0';kPbuffer{pRNode*pt,*qt;P[i].lCursor=IndexCh(buffer[0]);pt=(pRNode*)malloc(sizeof(pRNode));pt->rCursor=IndexCh(buffer[3]);pt->next=NULL;P[i].rHead=pt;n=4;while('\0'!=buffer[n]){qt=(pRNode*)malloc(sizeof(pRNode));qt->rCursor=IndexCh(buffer[n]);qt->next=NULL;pt->next=qt;pt=qt;n;}P[i].rLength=n-3;}}}/*判斷產(chǎn)生式正確性*/boolCheckP(char*st){intn;if(100>IndexCh(st[0]))returnreturnreturn{if(-1return}return}!=st[1])!=st[2])=3;'\0'!===IndexCh(st[n]))n++)voidFirst(intU){inti,j;for(i=0;i<PNum;i++){if(P[i].lCursor==U){structpRNode*pt;pt=P[i].rHead;j=0;while(j<P[i].rLength){if(100>pt->rCursor){AddFirst(U,pt->rCursor);break;}{if(NULL==first[pt->rCursor-100]){First(pt->rCursor);}AddFirst(U,pt->rCursor);if!HaveEmpty(pt->rCursor)){break;}{}}}=pt->next;P[i].rLength)/*當產(chǎn)生式右部都能推出空時*/AddFirst(U,-1);}}/*加入id{t{first集*/AddFirst(intU,intnCh)collectNode*pt,*qt;ch;/*用于處理Vn*/=NULL;=NULL;<100)=first[U-100];while(NULL{ifptnVtbreak;{====}}{==t====tpt->next;==pt)(structcollectNode*)malloc(sizeof(structcollectNodept->nVtpt->nextif(NULL{}{qt->nextpt=}}{pt=while(NULLch=if(-1=nCh;=NULL;==first[U-100])-100]=pt;=pt;/*qt指向first集的最后一個元素*/pt->next;first[nCh-100];!=pt)pt->nVt;!=ch){AddFirst(U,ch);}pt=pt->next;}}}bool{HaveEmpty(intnVn)return<100)collectNodept;pt=first[nVn-100];while(NULL!=pt){if(-1==pt->nVt)returntrue;pt=pt->next;}returnfalse;}d{Follow(intpRNodeV;if(100==V)/*當為初始符時*/AddFollow(V,-1,0);for(i=0;i<PNum;i++){pt=P[i].rHead;while(NULL!=pt&&pt->rCursor!=V)pt=pt->next;if(NULL!=pt){pt=pt->next;if(NULL==pt){if(NULL==follow[P[i].lCursor-100]&&P[i].lCursor!=V){Follow(P[i].lCursor);}AddFollow(V,P[i].lCursor,0);}{while(NULL!=pt&&HaveEmpty(pt->rCursor)){AddFollow(V,pt->rCursor,1);pt=pt->next;}if(NULL==pt){if(NULL==follow[P[i].lCursor-100]&&P[i].lCursor!=V){Follow(P[i].lCursor);}AddFollow(V,}{AddFollow(V,}}}}}P[i].lCursor,0);pt->rCursor,1);voidAddFollow(intV,intnCh,intkind){structcollectNode*pt,*qt;{===while(NULL{ifptnVtbreak;{====}}{pt=pt->nVtpt->nextNULL;NULL;100)/*為終結(jié)符時*/follow[V-100];!=pt)==nCh)tpt->next;==pt)(structcollectNode*)malloc(sizeof(structcollectNode=nCh;=NULL;==follow[V-100]){follow[V}{qt->next-=t=pt;/*qt指向follow集的最后一個元素*/}pt=pt->next;}}{if(0==kind){pt=follow[nCh-100];while(NULL!=pt){ch=pt->nVt;AddFollow(V,ch,pt=pt->next;}}{pt=first[nCh-100];while(NULL!=pt){ch=pt->nVt;if(-1!=ch){AddFollow(V,ch,1);}pt=pt->next;}}}}/*輸出first或follow集*/void{ShowCollect(structcollectNode**collect)inti;structcollectNode*pt;i=0;while(NULL!=collect[i]){pt=collect[i];printf("\n%c:\t",Vn[i]);while(NULL!=pt){if(-1!=pt->nVt){printf("%c",Vt[pt->nVt]);}printf("#");pt=pt->next;}}printf("\n");/*計算oid{ifirst和follow*/FirstFollow()=0;while('\0'{First(100}i=while('\0'{==+==Vn[i])first[i])Vn[i])follow[i]){Follow(100+i);}}/*構(gòu)造預(yù)測分析表*/{{CreateAT()pRNode*pt;collectNode*ct;=0;i<=P[i].rHead;while(NULL!=pt&&{ct=first[pt->rCursorwhile(NULL!=ct){if(-1!=ct->nVt)analyseTable[P[i].lCursor-ct=ct->next;}pt=pt->next;}if(NULL==pt){ct=follow[P[i].lCursorwhile(NULL!=ct){if(-1!=ct->nVt)analyseTable[P[i].lCursor-analyseTable[P[i].lCursor-ct=ct->next;}}PNum;i++)HaveEmpty(pt->rCursor))--100][ct->nVt]=i;--100][ct->nVt]=i;100][vtNum]=i;if(100<=pt->rCursor)/*不含空的非終結(jié)符*/{ct=first[pt->rCursor-100];while(NULL!=ct){analyseTable[P[i].lCursor-100][ct->nVt]=i;ct=ct->next;}}else/*終結(jié)符或者空*/{if(-1==pt->rCursor){ct=follow[P[i].lCursor-100];while(NULL!=ct){if(-1!=ct->nVt)analyseTable[P[i].lCursorelse/*當含有#號時*/analyseTable[P[i].lCursorct=ct->next;}}else/*為終結(jié)符*/{analyseTable[P[i].lCursor}}}}}/*輸出分析表*/ShowAT()ShowAT()printf("\t|\t");for(i=0;i<=-=-=-Num=--100][pt->rCursor]=i;vtNum;i++){printf("%c\t",}printf("#\t\n");Vt[i]);printf("---\t|---\t");for(i=0;i<=vtNum;i++)printf("---\t");printf("\n");for(i=0;i<vnNum;i++){printf("%c\t|\t",Vn[i]);for(j=0;j<=vtNum;j++){if(-1!=analyseTable[i][j])printf("R(%d)\t",analyseTable[i][j]);printf("error\t");}printf("\n");}}voidIdentify(char*st){intcurrent,step,r;/*r表使用的產(chǎn)生式的序號*/snstprintf("步驟\t分析符號棧\t當前指示字符\t使用產(chǎn)生式序號\n");step=0;current=0;printf("%d\t",step);ShowStack();printf("\t\t%c\t\t--\n",st[current]);while('#'{!=st[current])>analyseStack[topAnalyse]){if(analyseStack[topAnalyse]==IndexCh(st[current])){Pop();current++;printf("%d\t",ShowStack();printf("\t\t%c\t\t}n,rent{talyseStacktreturn;}}{r/*當為非終結(jié)符時*/=analyseTable[analyseStack[topAnalyse]-][IndexCh(st[current])];if(-1!=r){Push(r);printf("%d\t",step);ShowStack();printf("\t\t%c\t\t%d\n",st[current],}{return;}}}if('#'==st[current]){if(0==topAnalyse&&'#'{==st[current])printf
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 2025年度租賃房屋合同轉(zhuǎn)讓與租戶信用評估及風(fēng)險管理合同
- 二零二五年度旅游度假村用地使用權(quán)協(xié)議
- 2025年度車輛事故環(huán)境損害賠償協(xié)議
- 二零二五年度退租協(xié)議書及舊房裝修拆除工程合同
- 2025年度期刊發(fā)行權(quán)轉(zhuǎn)讓認刊書審核及執(zhí)行合同
- 二零二五年度房屋租賃合同租賃房屋租賃合同解除程序
- 二零二五年度品牌形象維護營銷人員保密及合作協(xié)議
- 2025年度科技研發(fā)領(lǐng)域自愿出資入股協(xié)議
- 2025年度貴金屬首飾典當借款服務(wù)協(xié)議
- 二零二五年度互聯(lián)網(wǎng)企業(yè)職工勞動合同優(yōu)化方案
- 探討630MW超臨界機組深度調(diào)峰安全技術(shù)措施
- 紅色旅游線路
- 柔性印刷技術(shù)課件
- 膝骨關(guān)節(jié)炎中醫(yī)診療指南
- 油浸式變壓器狀態(tài)監(jiān)測裝置
- 北京電子科技職業(yè)學(xué)院招聘考試題庫2024
- 貸款的培訓(xùn)課件
- 無人系統(tǒng)自主控制
- 化工原理陳敏恒課件
- 景區(qū)保安投標方案(技術(shù)方案)
- 中建辦公、生活區(qū)臨時設(shè)施施工方案
評論
0/150
提交評論