ImageVerifierCode 换一换
格式:DOCX , 页数:29 ,大小:19.29KB ,
资源ID:3618304      下载积分:3 金币
快捷下载
登录下载
邮箱/手机:
温馨提示:
快捷下载时,用户名和密码都是您填写的邮箱或者手机号,方便查询和重复下载(系统自动生成)。 如填写123,账号就是123,密码也是123。
特别说明:
请自助下载,系统不会自动发送文件的哦; 如果您已付费,想二次下载,请登录后访问:我的下载记录
支付方式: 支付宝    微信支付   
验证码:   换一换

加入VIP,免费下载
 

温馨提示:由于个人手机设置不同,如果发现不能下载,请复制以下地址【https://www.bdocx.com/down/3618304.html】到电脑端继续下载(重复下载不扣费)。

已注册用户请登录:
账号:
密码:
验证码:   换一换
  忘记密码?
三方登录: 微信登录   QQ登录  

下载须知

1: 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。
2: 试题试卷类文档,如果标题没有明确说明有答案则都视为没有答案,请知晓。
3: 文件的所有权益归上传用户所有。
4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
5. 本站仅提供交流平台,并不能对任何下载内容负责。
6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

版权提示 | 免责声明

本文(LL1语法分析c++实现first集follow集分析表分析栈.docx)为本站会员(b****5)主动上传,冰豆网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。 若此文所含内容侵犯了您的版权或隐私,请立即通知冰豆网(发送邮件至service@bdocx.com或直接QQ联系客服),我们立即给予删除!

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

1、LL1语法分析c+实现first集follow集分析表分析栈/LL(1)文法(源代码)#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; /*右

2、部长度*/ struct pRNode *rHead; /*右部结点头指针*/;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* firstM

3、axVnNum + 1; /*first集*/struct collectNode* 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* co

4、llect, int num);/*输出Vn或Vt的内容*/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);/*输出fir

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

6、Collect(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) int i; InitStack(); printf(请输入符号串(以#结束) : ); ch = getchar(); i = 0

7、; while(# != ch & i MaxStLength) if( != ch & n != ch) sti+ = ch; ch = getchar(); if(# = ch & i MaxStLength) sti = ch; Identify(st); else printf(输入出错!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 =

8、0; i MaxRuleNum; i+) Pi.lCursor = NULL; Pi.rHead = NULL; Pi.rLength = 0; PNum = 0; for(i = 0; i = MaxPLength; i+) bufferi = 0; for(i = 0; i MaxVnNum; i+) firsti = NULL; followi = NULL; for(i = 0; i = MaxVnNum; i+) for(j = 0; j = MaxVnNum + 1; j+) analyseTableij = -1; int IndexCh(char ch) int n; n =

9、0; /*is Vn?*/ while(ch != Vnn & 0 != Vnn) n+; if(0 != Vnn) return 100 + n; n = 0; /*is Vt?*/ while(ch != Vtn & 0 != Vtn) n+; if(0 != Vtn) return n; return -1;/*输出Vn或Vt的内容*/void ShowChArray(char* collect) int k = 0; while(0 != collectk) printf( %c , collectk+); printf(n);/*输入非终结符*/void InputVn() int

10、inErr = 1; int n,k; char ch; while(inErr) printf(n请输入所有的非终结符,注意:); printf(请将开始符放在第一位,并以#号结束:n); ch = ; n = 0; /*初始化数组*/ while(n MaxVnNum) Vnn+ = 0; n = 0; while(# != ch) & (n MaxVnNum) if( != ch & n != ch & -1 = IndexCh(ch) Vnn+ = ch; vnNum+; ch = getchar(); Vnn = #; /*以“#”标志结束用于判断长度是否合法*/ k = n; if

11、(# != ch) if( # != (ch = getchar() while(# != (ch = getchar() ; printf(n符号数目超过限制!n); inErr = 1; continue; /*正确性确认,正确则,执行下下面,否则重新输入*/ Vnk = 0; ShowChArray(Vn); 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;

12、/*输入终结符*/void InputVt() int inErr = 1; int n,k; char ch; while(inErr) printf(n请输入所有的终结符,注意:); printf(以#号结束:n); ch = ; n = 0; /*初始化数组*/ while(n MaxVtNum) Vtn+ = 0; n = 0; while(# != ch) & (n MaxVtNum) if( != ch & n != ch & -1 = IndexCh(ch) Vtn+ = ch; vtNum+; ch = getchar(); Vtn = #; k = n; if(# != ch

13、) if( # != (ch = getchar() while(# != (ch = getchar() ; printf(n符号数目超过限制!n); inErr = 1; continue; Vtk = 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; /*产生式输入*/void InputP() char ch; int

14、 i = 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+) buffern = 0; /*输入产生式串*/ ch = ; n = 0; while(n != (ch = getchar() & n rCursor = IndexCh(buffer3

15、); 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+; else printf(输入符号含非法在成分,请重新输入!n); /*判断产生式正确性*/bool CheckP(char * st) int n; if(100 IndexCh(st0) return fa

16、lse; 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 PNum; i+) if(Pi.lCursor = U) struct pRNode* pt; pt = Pi.rHead; j = 0; while(j pt-rCursor) AddFirst(U, pt-rCursor); break; else if

17、(NULL = firstpt-rCursor - 100) First(pt-rCursor); AddFirst(U, pt-rCursor); if(!HaveEmpty(pt-rCursor) break; else pt = 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(n

18、Ch nVt = nCh) break; else qt = pt; pt = pt-next; if(NULL = pt) pt = (struct collectNode *)malloc(sizeof(struct collectNode); pt-nVt = nCh; pt-next = NULL; if(NULL = firstU - 100) firstU - 100 = pt; else qt-next = pt; /*qt指向first集的最后一个元素*/ pt = pt-next; else pt = firstnCh - 100; while(NULL != pt) ch

19、= pt-nVt; if(-1 != ch) AddFirst(U, ch); pt = pt-next; bool HaveEmpty(int nVn) if(nVn nVt) return true; pt = pt-next; return false;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 =

20、 pt) if(NULL = followPi.lCursor - 100 & Pi.lCursor != V) Follow(Pi.lCursor); AddFollow(V, Pi.lCursor, 0); else while(NULL != pt & HaveEmpty(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.lCu

21、rsor, 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; if(nCh nVt = nCh) break; else qt = pt; pt = pt-next; if(NULL = pt) pt = (struct collectNode *)malloc(sizeof(struct collectNode); pt-nVt = nCh; pt-next = NUL

22、L; 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 != pt) ch = pt-nVt; AddFollow(V, ch, 0); pt = pt-next; else pt = firstnCh - 100; while(NULL != pt) ch = pt-nVt; if(-1 != ch) AddFollow(V, ch, 1);

23、 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, Vni); while(NULL != pt) if(-1 != pt-nVt) printf( %c, Vtpt-nVt); else printf( #); pt = pt-next; i+; printf(n);/*计算first和follow*/vo

24、id 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(NULL != ct) if(-1 != ct-nVt) analyseTablePi.lCursor - 100ct-nVt = i;

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1