![預測分析算法的設計與實現_第1頁](http://file4.renrendoc.com/view/218b0e17b9a47dd9f66cf0fccec87fca/218b0e17b9a47dd9f66cf0fccec87fca1.gif)
![預測分析算法的設計與實現_第2頁](http://file4.renrendoc.com/view/218b0e17b9a47dd9f66cf0fccec87fca/218b0e17b9a47dd9f66cf0fccec87fca2.gif)
![預測分析算法的設計與實現_第3頁](http://file4.renrendoc.com/view/218b0e17b9a47dd9f66cf0fccec87fca/218b0e17b9a47dd9f66cf0fccec87fca3.gif)
![預測分析算法的設計與實現_第4頁](http://file4.renrendoc.com/view/218b0e17b9a47dd9f66cf0fccec87fca/218b0e17b9a47dd9f66cf0fccec87fca4.gif)
![預測分析算法的設計與實現_第5頁](http://file4.renrendoc.com/view/218b0e17b9a47dd9f66cf0fccec87fca/218b0e17b9a47dd9f66cf0fccec87fca5.gif)
版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領
文檔簡介
/預料分析算法的設計和實現程序代碼:#include"stdio.h"#include"stdlib.h"#defineMaxRuleNum8#defineMaxVnNum5#defineMaxVtNum5#defineMaxStackDepth20#defineMaxPLength20#defineMaxStLength50structpRNode/*產生式右部結構*/{intrCursor;structpRNode*next;};structpNode{intlCursor;intrLength;/*右部長度*/structpRNode*rHead;/*右部結點頭指針*/};charVn[MaxVnNum+1];/*非終結符集*/intvnNum;charVt[MaxVtNum+1];/*終結符集*/intvtNum;structpNodeP[MaxRuleNum];intPNum;charbuffer[MaxPLength+1];charch;charst[MaxStLength];/*要分析的符號串*/structcollectNode{intnVt;voidShowAT();/*輸出分析表*/voidIdentify(char*st);voidInitStack();voidShowStack();voidPop();voidPush(intr);intmain(){chartodo,ch;Init();InputVn();InputVt();InputP();getchar();FirstFollow();printf("所得first集為:");ShowCollect(first);printf("所得follow集為:");ShowCollect(follow);CreateAT();ShowAT();todo='y';while('y'==todo){printf("\n是否接著進行句型分析?(y/n):");todo=getchar();while('y'!=todo&&'n'!=todo){printf("\n(y/n)?");todo=getchar();}if('y'==todo){inti;InitStack();printf("請輸入符號串(以#結束):");ch=getchar();i=0;while('#'!=ch&&i<MaxStLength){st[i]=ch;Identify(st);}elseprintf("輸入出錯!\n");}}getchar();}voidInit(){inti,j;vnNum=0;vtNum=0;PNum=0;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].lCursor=NULL;P[i].rHead=NULL;P[i].rLength=0;}PNum=0;for(i=0;i<=MaxPLength;i++)}}intIndexCh(charch){intn;n=0;/*isVn?*/while(ch!=Vn[n]&&'\0'!=Vn[n])n++;if('\0'!=Vn[n])return100+n;n=0;/*isVt?*/while(ch!=Vt[n]&&'\0'!=Vt[n])n++;if('\0'!=Vt[n])returnn;return-1;}/*輸出Vn或Vt的內容*/voidShowChArray(char*collect){intk=0;while('\0'!=collect[k]){printf("%c",collect[k++]);}printf("\n");}/*輸入非終結符*/voidInputVn(){{Vn[n++]='\0';}n=0;while(('#'!=ch)&&(n<MaxVnNum)){if(''!=ch&&'\n'!=ch&&-1==IndexCh(ch)){Vn[n++]=ch;vnNum++;}ch=getchar();}Vn[n]='#';/*以"#"標記結束用于推斷長度是否合法*/k=n;if('#'!=ch){if('#'!=(ch=getchar())){while('#'!=(ch=getchar()));printf("\n符號數目超過限制!\n");inErr=1;continue;}{printf("輸入正確確認?(y/n):");}scanf("%c",&ch);}if('n'==ch){printf("錄入錯誤重新輸入!\n");inErr=1;}else{inErr=0;}}}/*輸入終結符*/voidInputVt(){intinErr=1;intn,k;charch;while(inErr){printf("\n請輸入全部的終結符,留意:");printf("以#號結束:\n");ch='';n=0;/*初始化數組*/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()));printf("\n符號數目超過限制!\n");inErr=1;continue;}}Vt[k]='\0';ShowChArray(Vt);ch='';while('y'!=ch&&'n'!=ch){if('\n'!=ch){printf("輸入正確確認?(y/n):");}scanf("%c",&ch);}if('n'==ch){printf("錄入錯誤重新輸入!\n");inErr=1;}else{inErr=0;}}}/*產生式輸入*/voidInputP(){charch;inti=0,n,num;printf("請輸入文法產生式的個數:");scanf("%d",&num);PNum=num;getchar();/*消退回車符*/printf("\n請輸入文法的%d個產生式,并以回車分隔每個產生式:",num);printf("\n");while(i<num){printf("第%d個:",i);/*初始化*/for(n=0;n<MaxPLength;n++)buffer[n]='\0';/*輸入產生式串*/ch='';n=0;while('\n'!=(ch=getchar())&&n<MaxPLength){if(''!=ch)buffer[n++]=ch;}buffer[n]='\0';if(CheckP(buffer)){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;i++;}elseprintf("輸入符號含非法在成分,請重新輸入!\n");}}/*推斷產生式正確性*/boolCheckP(char*st){intn;if(100>IndexCh(st[0]))returnfalse;if('-'!=st[1])returnfalse;if('>'!=st[2])returnfalse;for(n=3;'\0'!=st[n];n++){if(-1==IndexCh(st[n]))returnfalse;}returntrue;}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;}else{if(NULL==first[pt->rCursor-100]){First(pt->rCursor);}AddFirst(U,pt->rCursor);if(!HaveEmpty(pt->rCursor)){break;}else{pt=pt->next;}}j++;}if(j>=P[i].rLength)/*當產生式右部都能推出空時*/AddFirst(U,-1);}}}/*加入first集*/voidAddFirst(intU,intnCh){structcollectNode*pt,*qt;intch;/*用于處理Vn*/pt=NULL;qt=NULL;if(nCh<100){pt=first[U-100];while(NULL!=pt){if(pt->nVt==nCh)break;else{qt=pt;pt=pt->next;}}if(NULL==pt){pt=(structcollectNode*)malloc(sizeof(structcollectNode));pt->nVt=nCh;pt->next=NULL;if(NULL==first[U-100]){first[U-100]=pt;}else{qt->next=pt;/*qt指向first集的最終一個元素*/}pt=pt->next;}}else{pt=first[nCh-100];while(NULL!=pt){ch=pt->nVt;if(-1!=ch){AddFirst(U,ch);}pt=pt->next;}}}boolHaveEmpty(intnVn){if(nVn<100)returnfalse;structcollectNode*pt;pt=first[nVn-100];while(NULL!=pt){if(-1==pt->nVt)returntrue;pt=pt->next;}returnfalse;}voidFollow(intV){inti;structpRNode*pt;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);}else{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,P[i].lCursor,0);}else{AddFollow(V,pt->rCursor,1);}}}}}voidAddFollow(intV,intnCh,intkind){structcollectNode*pt,*qt;intch;pt=NULL;qt=NULL;if(nCh<100)/*為終結符時*/{pt=follow[V-100];while(NULL!=pt){if(pt->nVt==nCh)break;else{qt=pt;pt=pt->next;}}if(NULL==pt){pt=(structcollectNode*)malloc(sizeof(structcollectNode));pt->nVt=nCh;pt->next=NULL;if(NULL==follow[V-100]){follow[V-100]=pt;}else{qt->next=pt;/*qt指向follow集的最終一個元素*/}pt=pt->next;}}else{if(0==kind){pt=follow[nCh-100];while(NULL!=pt){ch=pt->nVt;AddFollow(V,ch,0);pt=pt->next;}}else{pt=first[nCh-100];while(NULL!=pt){ch=pt->nVt;if(-1!=ch){AddFollow(V,ch,1);}pt=pt->next;}}}}/*輸出first或follow集*/voidShowCollect(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]);}elseprintf("#");pt=pt->next;}i++;}printf("\n");}/*計算first和follow*/voidFirstFollow(){inti;i=0;while('\0'!=Vn[i]){if(NULL==first[i])First(100+i);i++;}i=0;while('\0'!=Vn[i]){if(NULL==follow[i])Follow(100+i);i++;}}/*構造預料分析表*/voidCreateAT(){inti;structpRNode*pt;structcollectNode*ct;for(i=0;i<PNum;i++){pt=P[i].rHead;while(NULL!=pt&&HaveEmpty(pt->rCursor)){ct=first[pt->rCursor-100];while(NULL!=ct){if(-1!=ct->nVt)analyseTable[P[i].lCursor-100][ct->nVt]=i;ct=ct->next;}pt=pt->next;}if(NULL==pt){ct=follow[P[i].lCursor-100];while(NULL!=ct){if(-1!=ct->nVt)analyseTable[P[i].lCursor-100][ct->nVt]=i;elseanalyseTable[P[i].lCursor-100][vtNum]=i;ct=ct->next;}}else{if(100<=pt->rCursor)/*不含空的非終結符*/{ct=first[pt->rCursor-100];while(NULL!=ct){analyseTable[P[i].lCursor-100][ct->nVt]=i;ct=ct->next;}}else/*終結符或者空*/{if(-1==pt->rCursor){ct=follow[P[i].lCursor-100];while(NULL!=ct){if(-1!=ct->nVt)analyseTable[P[i].lCursor-100][ct->nVt]=i;else/*當含有#號時*/analyseTable[P[i].lCursor-100][vtNum]=i;ct=ct->next;}}else/*為終結符*/{analyseTable[P[i].lCursor-100][pt->rCursor]=i;}}}}}/*輸出分析表*/voidShowAT(){inti,j;printf("構造預料分析表如下:\n");printf("\t|\t");for(i=0;i<vtNum;i++){printf("%c\t",Vt[i]);}printf("#\t\n");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]);elseprintf("error\t");}printf("\n");}}voidIdentify(char*st){intcurrent,step,r;/*r表運用的產生式的序號*/printf("\n%s的分析過程:\n",st);printf("步驟\t分析符號棧\t當前指示字符\t運用產生式序號\n");step=0;current=0;printf("%d\t",step);ShowStack();printf("\t\t%c\t\t--\n",st[current]);while('#'!=st[current]){if(100>analyseStack[topAnalyse]){if(analyseStack[topAnalyse]==IndexCh(st[current])){Pop();current++;step++;printf("%d\t",step);ShowStack();printf("\t\t%c\t\t出棧、后移\n",st[current]);}else{printf("%c-%c不匹配!",analyseStack[topAnalyse],st[current]);printf("此串不是此文法的句子!\n");return;}}else/*當為非終結符時*/{r=analyseTable[analyseStack[topAnalyse]-100][IndexCh(st[current])];if(-1!=r){Push(r);step++;printf("%d\t",step);ShowStack();printf("\t\t%c\t\t%d\n",st[current],r);}else{printf("此串不是此文法的句子!\n");return;}}}if('#'==st[current]){if(0==topAnalyse&&'#'==st[current]){step++;printf("%d\t",step);ShowStack();printf("\t\t%c\t\t分析成功!\n",st[current]);printf("%s是給定文法的句子!\n",st);}else{while(topAnalyse>0){if(100>analyseStack[topAnalyse]){printf("此串不是此文法的句子!\n");return;}e
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯系上傳者。文件的所有權益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網頁內容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
- 4. 未經權益所有人同意不得將文件中的內容挪作商業(yè)或盈利用途。
- 5. 人人文庫網僅提供信息存儲空間,僅對用戶上傳內容的表現方式做保護處理,對用戶上傳分享的文檔內容本身不做任何修改或編輯,并不能對任何下載內容負責。
- 6. 下載文件中如有侵權或不適當內容,請與我們聯系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 2025年鶴崗貨運考試題目
- 2025年萊蕪貨運資格證安檢考試題
- 小學二年級數學上口算紙
- 2025年濟寧道路客貨運輸從業(yè)資格證b2考試題庫
- 2025年焦作道路運輸從業(yè)人員從業(yè)資格考試
- 電焊工入職合同(2篇)
- 《北魏政治和北方民族大交融》聽課評課記錄2(新部編人教版七年級上冊歷史)
- 2024-2025學年高中英語Module6TheInternetandTelecommunicationsSectionⅤWriting-正反觀點對比類議論文教案含解析外研版必修1
- 企業(yè)年終工作總結報告
- 公司人事部門年終工作總結
- 課件:《教育強國建設規(guī)劃綱要(2024-2035年)》學習宣講
- 生產制造工藝流程規(guī)范與作業(yè)指導書
- 梁湘潤《子平基礎概要》簡體版
- 開工“第一課”安全培訓課件
- 財務收支月報表excel模板
- 江蘇省社會穩(wěn)定風險評估辦法(試行)
- 國標充電協(xié)議報文整理
- 水餃類產品質量檢驗作業(yè)指導書
- 電力變壓器計算單
- 紅外測溫培訓
- 新型城市化建設中城鄉(xiāng)結合部存在的問題及解決方案
評論
0/150
提交評論