版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
预测分析算法的设计与实现删除预测分析算法的设计与实现程序代码:#include#includeefineefineefineefineefineefineMaxRuleNum8MaxVnNum5MaxVtNum5MaxStackDepth20MaxPLength20MaxStLength50{pRNode/*产生式右部结构*/rCursor;pRNodenext;{pNoderLength;/*右部长度*/structpRNode*rHead;/*右部结点头指针*//*非终结符集*/+Vn/*非终结符集*/+vnNum/*终结符集*/+Vt/*终结符集*/+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("请输入符号串(以#结束):");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的内容*/ShowChArray(charShowChArray(char*collect){intk=0;while('\0'!=collect[k]){printf("%c",collect[k++]);}printf("\n");}/*输入非终结符*/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]='#';/*以"#"标志结束用于判断长度是否合法*/k=n;if('#'!=ch){if('#'!=(ch=getchar())){while('#'!=(ch=getchar()));ninErr=1;continue;}{}scanf("%c",&ch);}if('n'==ch){inErr=1;}{inErr=0;}}}/*输入终结符*/{tVt==n,k;charch;while(inErr){printf("以#号结束:\n");n=='/*初始化数组*/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;}}}/*产生式输入*/voidInputP(){charch;inti=0,n,num;scanf("%d",&num);PNum=num;getchar();/*消除回车符*/printf("\n请输入文法的%d个产生式,并以回车分隔每个产生式:",num);printf("\n");while(i<num){/*初始化*/for(n=0;n<MaxPLength;n++)buffer[n]='\0';/*输入产生式串*/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;}}}/*判断产生式正确性*/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)/*当产生式右部都能推出空时*/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)/*为终结符时*/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);}}/*构造预测分析表*/{{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)/*不含空的非终结符*/{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].lCursorelse/*当含有#号时*/analyseTable[P[i].lCursorct=ct->next;}}else/*为终结符*/{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表使用的产生式的序号*/snstprintf("步骤\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])>analyseStack[topAnalyse]){if(analyseStack[topAnalyse]==IndexCh(st[current])){Pop();current++;printf("%d\t",ShowStack();printf("\t\t%c\t\t}n,rent{talyseStacktreturn;}}{r/*当为非终结符时*/=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. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2026黑龙江绥棱县事业单位招聘16人备考题库及参考答案详解一套
- 2026北京大学光华管理学院招聘劳动合同制人员1人备考题库及答案详解(典优)
- 2026潍坊市蓝航技工学校教师招聘备考题库附答案详解(预热题)
- 2026安徽马鞍山市纤维检验所招聘编外聘用人员1人备考题库及参考答案详解1套
- 2026福建泉州鲤城区常泰街道社区卫生服务中心编外工作人员招聘2人备考题库附答案详解(典型题)
- 武汉地铁集团有限公司2026届春季校园招聘备考题库含答案详解(夺分金卷)
- 2026中国科学院广州地球化学研究所党务综合管理岗招聘1人备考题库及答案详解(名校卷)
- 2026银川市殡仪馆招聘13人备考题库及答案详解1套
- 2026山东济宁市东方圣地人力资源开发有限公司招聘辅助服务人员1人备考题库附答案详解(研优卷)
- 2026江西吉安新干县人民医院招聘见习岗专业技术人员20人备考题库及完整答案详解1套
- 3.2 工业区位因素与工业布局(第1课时)课件湘教版高中地理必修二
- 小学五年级英语下册 Unit6 Work quietly!Part A Let's try Let's talk 教学设计
- JBT 12530.4-2015 塑料焊缝无损检测方法 第4部分:超声检测
- 钣金车间安全生产安全生产
- GB/T 42177-2022加氢站氢气阀门技术要求及试验方法
- YC/T 336-2020烟叶收购站设计规范
- GB/T 4968-2008火灾分类
- Skopos and Commission in Translational Action翻译行为的目的与委托
- 赣价协〔2023〕9号江西省建设工程造价咨询服务收费基准价
- 某办公楼装修改造工程施工组织设计方案
- 计算机专业英语第6章
评论
0/150
提交评论