LL1语法分析c++实现,first集,follow集,分析表,分析栈

上传人:人*** 文档编号:485450054 上传时间:2022-07-19 格式:DOCX 页数:38 大小:20.92KB
返回 下载 相关 举报
LL1语法分析c++实现,first集,follow集,分析表,分析栈_第1页
第1页 / 共38页
LL1语法分析c++实现,first集,follow集,分析表,分析栈_第2页
第2页 / 共38页
LL1语法分析c++实现,first集,follow集,分析表,分析栈_第3页
第3页 / 共38页
LL1语法分析c++实现,first集,follow集,分析表,分析栈_第4页
第4页 / 共38页
LL1语法分析c++实现,first集,follow集,分析表,分析栈_第5页
第5页 / 共38页
点击查看更多>>
资源描述

《LL1语法分析c++实现,first集,follow集,分析表,分析栈》由会员分享,可在线阅读,更多相关《LL1语法分析c++实现,first集,follow集,分析表,分析栈(38页珍藏版)》请在金锄头文库上搜索。

1、LL (1)文法(源代码)#i nclude stdio.h#in elude stdlib.h#defi ne MaxRuleNum 8#defi ne Max VnNum 5#defi ne MaxVtNum 5#defi ne MaxStackDepth 20#defi ne MaxPLe ngth 20#defi ne MaxStLe ngth 50struct pRNode /*产生式右部结构*/intCursor;struct pRNode *n ext;;struct pNodeint lCursor;int rLength; /* 右部长度 */struct pRNode *r

2、Head; /*右部结点头指针 */ ;char VnMaxVnNum + 1; /*非终结符集 */int vnNum;char VtMaxVtNum + 1; /* 终结符集 */int vtNum;struct pNode PMaxRuleNum;int PNum;char bufferMaxPLe ngth + 1;char ch;char stMaxStLength; /*要分析的符号串 */struct collectNodeint n Vt;struct collectNode *n ext;struct collectNode* firstMax VnNum + 1; /*fi

3、rst集 */集*/struct collectNode* followMaxV nNum + 1; /*followintanalyseT ableMaxVnNum + 1MaxVtNum + 1 + 1;int analyseStackMaxStackDepth + 1; /* 分析栈 */int topAnalyse; /* 分析栈顶 */void Init();/* 初始化 */int In dexCh(char ch);void InputVt(); /* 输入终结符 */void In put Vn ();/* 输入非终结符*/void ShowChArray(char* coll

4、ect, int num);/* 输出 Vn 或 Vt 的内容 */void InputP();/* 产生式输入 */bool CheckP(char * st);/*判断产生式正确性 */void First(i nt U);void AddFirst(int U, int nCh); /* 加入 first 集 */bool HaveEmpty(i nt nVn);void Follow(int V);/*计算 follow 集 */void AddFollow(i nt V, i nt n Ch, i nt kin d);void ShowCollect(struct collectNo

5、de *collect);/* 输出 first 或 follow 集 */void FirstFollow();/* 计算 first 和 follow*/void CreateAT();/*构造预测分析表*/void ShowAT();/* 输出分析表 */void In itStack();void ShowStack();void Pop();void Push(i nt r);int mai n()char todo,ch;In it();In putV n();In putVt();In putP();getchar();FirstFollow();printf(所得 first

6、集为:”);ShowCollect(first);printf(所得 follow 集为:”);CreateAT();ShowAT();todo = y;while(y = todo)printf(n是否继续进行句型分析? (y / n):);todo = getchar();while(y != todo & n != todo)prin tf(n(y / n)?);todo = getchar();if(y = todo)int i;In itStack();printf(请输入符号串(以#结束):”);ch = getchar();i = 0;while(# != ch & i MaxS

7、tLe ngth)if( != ch & n != ch)sti+ = ch;ch = getchar();if(# = ch & i MaxStLe ngth)sti = ch;Ide ntify(st);elseprintf(” 输入出错! n);getchar();void Init()int i,j;vnNum = 0;vtNum = 0;PNum = 0;for(i = 0; i = MaxVnNum; i+)Vni = 0;for(i = 0; i = MaxVtNum; i+)Vti = 0;for(i = 0; i MaxRuleNum; i+)PiCursor = NULL;

8、Pi.rHead = NULL;Pi.rLe ngth = 0;PNum = 0;for(i = 0; i = MaxPLe ngth; i+)bufferi = 0;for(i = 0; i Max VnNum; i+)firsti = NULL;followi = NULL;for(i = 0; i = MaxVnNum; i+)for( j = 0; j = MaxVnNum + 1; j+) an alyseT ableij = -1;int In dexCh(char ch)int n;n = 0; /*is Vn?*/while(ch != Vnn & 0 != Vnn )n+;i

9、f(0 != Vnn)return 100 + n;n = 0; /*is Vt?*/while(ch != Vt n & 0 != Vt n)n+;if(0 != Vt n)return n;return -1;/*输出Vn或Vt的内容*/ void ShowChArray(char* collect)int k = 0;while(0 != collectk)printf(” %c , collectk+);prin tf(n);/*输入非终结符*/void In putV n()int inErr = 1;int n ,k;char ch;while(i nErr)printf(n请输入

10、所有的非终结符,注意:”);printf(请将开始符放在第一位,并以#号结束:n);ch =;n = 0;I*初始化数组*/while( n Max Vn Num)Vnn+ = 0:n = 0;while(# != ch) & (n MaxV nNu m)if( != ch & n != ch & -1 = In dexCh(ch)Vnn+ = ch;vnNu m+;ch = getchar();Vn n = #; /* 以# ”标志结束用于判断长度是否合法*/k = n;if(# != ch)if( # != (ch = getchar()while(# != (ch = getchar()

11、printf(n符号数目超过限制! n);inErr = 1;con ti nue;/*正确性确认,正确则,执行下下面,否则重新输入*/Vnk = 0;ShowChArray(V n);ch =;while(y != ch & n != ch)if(n != ch)printf(”输入正确确认?(y/n):”);sca nf(%c, & ch);if(n = ch)printf(录入错误重新输入! n);inErr = 1;elseinErr = 0;/*输入终结符*/void In putVt()int inErr = 1;int n ,k;char ch;while(i nErr),注意:

12、);prin tf(n请输入所有的终结符printf(” 以#号结束:n);ch =;n = 0;/*初始化数组*/while( n MaxVtNum)Vt n+ = 0:n = 0;while(# != ch) & (n MaxVtNum)if( != ch & n != ch & -1 = In dexCh(ch)Vtn+ = ch;vtNum+;ch = getchar();Vtn = #;k = n;if(# != ch)if( # != (ch = getchar()while(# != (ch = getchar()printf(n符号数目超过限制! n);inErr = 1;co

13、n ti nue;Vtk = 0:ShowChArray(Vt);ch =;while(y != ch & n != ch)if(n != ch)printf(”输入正确确认?(y/n):);sca nf(%c, & ch);if(n = ch)printf(录入错误重新输入! n);inErr = 1;elseinErr = 0;/*产生式输入*/void In putP()char ch;int i = 0, n,num;printf(请输入文法产生式的个数:”);scan f(%d, &n um);PNum = num;getchar(); /*消除回车符*/printf(n请输入文法的%d个产生式,并以回车分隔每个产生式:,num);prin tf(n);while(i num)printf(第 %d 个:,i);/*初始化*/for(n =0; n

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 办公文档 > 活动策划

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号