编译原理ll语法分析代码及结果

上传人:第*** 文档编号:33947513 上传时间:2018-02-19 格式:DOC 页数:21 大小:148KB
返回 下载 相关 举报
编译原理ll语法分析代码及结果_第1页
第1页 / 共21页
编译原理ll语法分析代码及结果_第2页
第2页 / 共21页
编译原理ll语法分析代码及结果_第3页
第3页 / 共21页
编译原理ll语法分析代码及结果_第4页
第4页 / 共21页
编译原理ll语法分析代码及结果_第5页
第5页 / 共21页
点击查看更多>>
资源描述

《编译原理ll语法分析代码及结果》由会员分享,可在线阅读,更多相关《编译原理ll语法分析代码及结果(21页珍藏版)》请在金锄头文库上搜索。

1、LL 语法分析:源代码#include stdio.h#include stdlib.h#define MaxRuleNum 8#define MaxVnNum 5#define MaxVtNum 5#define MaxStackDepth 20#define MaxPLength 20#define MaxStLength 50struct pRNode /*产生式右部结构*/int rCursor; struct pRNode *next;struct pNode int lCursor; int rLength; /*右部长度*/struct pRNode *rHead; /*右部结点

2、头指针 */;char VnMaxVnNum + 1; /*非终结符集*/int vnNum;char VtMaxVtNum + 1; /*终结符集*/int vtNum;struct pNode PMaxRuleNum; int PNum; char bufferMaxPLength + 1;char ch; char stMaxStLength; /*要分析的符号串*/struct collectNode int nVt; struct collectNode *next;struct collectNode* firstMaxVnNum + 1; /*first 集*/struct co

3、llectNode* followMaxVnNum + 1; /*follow 集*/int analyseTableMaxVnNum + 1MaxVtNum + 1 + 1;int analyseStackMaxStackDepth + 1; /*分析栈*/int topAnalyse; /*分析栈顶*/void Init();/*初始化 */int IndexCh(char ch);void InputVt(); /*输入终结符*/void InputVn();/*输入非终结符*/void ShowChArray(char* collect, int num);/*输出 Vn 或 Vt 的

4、内容*/void InputP();/*产生式输入*/bool CheckP(char * st);/*判断产生式正确性*/void First(int U);void AddFirst(int U, int nCh); /*加入 first 集*/bool HaveEmpty(int nVn); void Follow(int V);/*计算 follow 集*/void AddFollow(int V, int nCh, int kind);void ShowCollect(struct collectNode *collect);/*输出 first 或 follow 集*/void F

5、irstFollow();/*计算 first 和 follow*/void CreateAT();/*构造预测分析表*/void ShowAT();/*输出分析表*/void Identify(char *st);void InitStack();void ShowStack();void Pop();void Push(int r);void main(void)char todo,ch;Init();InputVn();InputVt();InputP();getchar();FirstFollow();printf(所得 first 集为:);ShowCollect(first); p

6、rintf(所得 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)int i;InitStack();printf(请输入符号串(以#结束) : );ch = getchar();i = 0;while(# != ch & i rCursor = I

7、ndexCh(buffer3);pt-next = NULL;Pi.rHead = pt;n = 4;while(0 != buffern) qt = (pRNode*)malloc(sizeof(pRNode);qt-rCursor = IndexCh(buffern);qt-next = NULL;pt-next = qt;pt = qt;n+;Pi.rLength = n - 3;i+;elseprintf(输入符号含非法在成分,请重新输入!n);/*判断产生式正确性*/bool CheckP(char * st)int n;if(100 IndexCh(st0)return false

8、;if(- != st1)return false;if( != st2)return false;for(n = 3; 0 != stn; n +)if(-1 = IndexCh(stn)return false;return true;void First(int U)int i,j;for(i = 0; i pt-rCursor)AddFirst(U, pt-rCursor);break;elseif(NULL = firstpt-rCursor - 100)First(pt-rCursor); AddFirst(U, pt-rCursor);if(!HaveEmpty(pt-rCurs

9、or)break;elsept = pt-next;j+;if(j = Pi.rLength) /*当产生式右部都能推出空时*/AddFirst(U, -1);/*加入 first 集*/void AddFirst(int U, int nCh) struct collectNode *pt, *qt;int ch; /*用于处理 Vn*/pt = NULL;qt = NULL;if(nCh nVt = nCh)break;elseqt = pt;pt = pt-next;if(NULL = pt)pt = (struct collectNode *)malloc(sizeof(struct

10、collectNode);pt-nVt = nCh;pt-next = NULL;if(NULL = firstU - 100)firstU - 100 = pt;elseqt-next = pt; /*qt 指向 first 集的最后一个元素*/pt = pt-next;elsept = firstnCh - 100;while(NULL != pt)ch = pt-nVt;if(-1 != ch)AddFirst(U, ch);pt = pt-next;bool HaveEmpty(int nVn) if(nVn nVt)return true;pt = pt-next;return fa

11、lse;void Follow(int V)int i;struct pRNode *pt ;if(100 = V) /*当为初始符时*/AddFollow(V, -1, 0 );for(i = 0; i rCursor != V) pt = pt-next;if(NULL != pt)pt = pt-next; if(NULL = pt) if(NULL = followPi.lCursor - 100 & Pi.lCursor != V)Follow(Pi.lCursor);AddFollow(V, Pi.lCursor, 0);else while(NULL != pt & HaveEm

12、pty(pt-rCursor)AddFollow(V, pt-rCursor, 1); pt = pt-next;if(NULL = pt) if(NULL = followPi.lCursor - 100 & Pi.lCursor != V) Follow(Pi.lCursor);AddFollow(V, Pi.lCursor, 0);else AddFollow(V, pt-rCursor, 1); void AddFollow(int V, int nCh, int kind)struct collectNode *pt, *qt;int ch; pt = NULL;qt = NULL;

13、if(nCh nVt = nCh)break;elseqt = pt;pt = pt-next;if(NULL = pt)pt = (struct collectNode *)malloc(sizeof(struct collectNode);pt-nVt = nCh;pt-next = NULL;if(NULL = followV - 100)followV - 100 = pt;else qt-next = pt; /*qt 指向 follow 集的最后一个元素*/pt = pt-next;else if(0 = kind)pt = follownCh - 100;while(NULL !

14、= pt)ch = pt-nVt;AddFollow(V, ch, 0);pt = pt-next;elsept = firstnCh - 100;while(NULL != pt)ch = pt-nVt;if(-1 != ch)AddFollow(V, ch, 1);pt = pt-next;/*输出 first 或 follow 集*/void ShowCollect(struct collectNode *collect)int i;struct collectNode *pt;i = 0;while(NULL != collecti)pt = collecti;printf(n%c:t

15、, Vni);while(NULL != pt) if(-1 != pt-nVt)printf( %c, Vtpt-nVt);elseprintf( #);pt = pt-next;i+;printf(n);/*计算 first 和 follow*/void FirstFollow()int i;i = 0;while(0 != Vni)if(NULL = firsti)First(100 + i);i+;i = 0;while(0 != Vni)if(NULL = followi)Follow(100 + i);i+;/*构造预测分析表*/void CreateAT()int i;struct pRNode *pt;struct collectNode *ct;for(i = 0; i rCursor) ct = firstpt-rCursor - 100; while(

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

当前位置:首页 > 办公文档 > 解决方案

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