编译原理 LL(1)文法源代码.doc
上传人:qw****27 上传时间:2024-09-12 格式:DOC 页数:21 大小:120KB 金币:15 举报 版权申诉
预览加载中,请您耐心等待几秒...

编译原理 LL(1)文法源代码.doc

编译原理LL(1)文法源代码.doc

预览

免费试读已结束,剩余 11 页请下载文档后查看

15 金币

下载此文档

如果您无法下载资料,请参考说明:

1、部分资料下载需要金币,请确保您的账户上有足够的金币

2、已购买过的文档,再次下载不重复扣费

3、资料包下载后请先用软件解压,在使用对应软件打开

LL(1)文法(源代码)#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;structcollectNode*next;};structcollectNode*first[MaxVnNum+1];/*first集*/structcollectNode*follow[MaxVnNum+1];/*follow集*/intanalyseTable[MaxVnNum+1][MaxVtNum+1+1];intanalyseStack[MaxStackDepth+1];/*分析栈*/inttopAnalyse;/*分析栈顶*/voidInit();/*初始化*/intIndexCh(charch);voidInputVt();/*输入终结符*/voidInputVn();/*输入非终结符*/voidShowChArray(char*collect,intnum);/*输出Vn或Vt的内容*/voidInputP();/*产生式输入*/boolCheckP(char*st);/*判断产生式正确性*/voidFirst(intU);voidAddFirst(intU,intnCh);/*加入first集*/boolHaveEmpty(intnVn);voidFollow(intV);/*计算follow集*/voidAddFollow(intV,intnCh,intkind);voidShowCollect(structcollectNode**collect);/*输出first或follow集*/voidFirstFollow();/*计算first和follow*/voidCreateAT();/*构造预测分析表*/voidShowAT();/*输出分析表*/voidIdentify(char*st);voidInitStack();voidShowStack();voidPop();voidPush(intr);voidmain(void){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){if(''!=ch&&'\n'!=ch){st[i++]=ch;}ch=getchar();}if('#'==ch&&i<MaxStLength){st[i]=ch;Identify(st);}elseprintf("输入出错!\n");}}getchar();}voidInit(){inti,j;vnNum=0;vtNum=0;PNum=0;