版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、编译原理实验报告学号姓名实验名称语法分析实验LL(1)实验目的输入:文法的多个产生式或产生式选项。输出:(1) 产生式中非终结符的first集合和follow集合。(2) 判断该文法是否是LL(1)文法。(3) 建立LL(1)文法的分析表。(4) 对输入的符号串进行LL(1)分析,并给出分析结果。实验方案设文法GS(VN,VT,P,S),则首字符集为: FIRST()a | a,aVT,,V *。若,FIRST()。由定义可以看出,FIRST()是指符号串能够推导出的所有符号串中处于串首的终结符号组成的集合。所以FIRST集也称为首符号集。设x1x2xn,FIRST()可按下列方法求得:令FI
2、RST(),i1;(1) 若xiVT,则xiFIRST();(2) 若xiVN; 若FIRST(xi),则FIRST(xi)FIRST(); 若FIRST(xi),则FIRST(xi)FIRST();(3) ii+1,重复(1)、(2),直到xiVT,(i2,3,n)或xiVN且若FIRST(xi)或in为止。当一个文法中存在产生式时,例如,存在A,只有知道哪些符号可以合法地出现在非终结符A之后,才能知道是否选择A产生式。这些合法地出现在非终结符A之后的符号组成的集合被称为FOLLOW集合。下面我们给出文法的FOLLOW集的定义。设文法GS(VN,VT,P,S),则 FOLLOW(A)a |
3、S Aa ,aVT。若SA,#FOLLOW(A)。由定义可以看出,FOLLOW(A)是指在文法GS的所有句型中,紧跟在非终结符A后的终结符号的集合。FOLLOW集可按下列方法求得:(1) 对于文法GS的开始符号S,有#FOLLOW(S);(2) 若文法GS中有形如BxAy的规则,其中x,yV *,则FIRST(y)FOLLOW(A);若文法GS中有形如BxA的规则,或形如BxAy的规则且FIRST(y),其中x,yV *,则FOLLOW(B)FOLLOW(A);(1) LL(1)文法的定义LL(1)分析法属于确定的自顶向下分析方法。LL(1)的含义是:第一个L表明自顶向下分析是从左向右扫描输入
4、串,第2个L表明分析过程中将使用最左推导,1表明只需向右看一个符号便可决定如何推导,即选择哪个产生式(规则)进行推导。LL(1)文法的判别需要依次计算FIRST集、FOLLOW集和SELLECT集,然后判断是否为LL(1)文法,最后再进行句子分析。需要预测分析器对所给句型进行识别。即在LL(1)分析法中,每当在符号栈的栈顶出现非终极符时,要预测用哪个产生式的右部去替换该非终极符;当出现终结符时,判断其与剩余输入串的第一个字符是否匹配,如果匹配,则继续分析,否则报错。LL(1)分析方法要求文法满足如下条件:对于任一非终极符A的两个不同产生式Aa,Ab,都要满足下面条件:SELECT(Aa)SEL
5、ECT(Ab)=(2) 预测分析表构造LL(1)分析表的作用是对当前非终极符和输入符号确定应该选择用哪个产生式进行推导。它的行对应文法的非终极符,列对应终极符,表中的值有两种:一是产生式的右部的字符串,一是null。若用M表示LL(1)分析表,则M可表示如下:M: VNVTPErrorM(A, t) = A,当tselect(A) ,否则M(A, t) = Error其中P表示所有产生式的集合。(3) 语法分析程序构造LL(1)分析中X为符号栈栈顶元素,a为输入流当前字符,E为给定测试数据的开始符号,#为句子括号即输入串的括号。分析表用一个二位数组M表示,数组元素MA,a中的下标A表示非终结符
6、,a为终结符或句子括号#,二维数组中存放的是一条关于A 的产生式,表明当非终结符A向下推导时,面临输入符a时,所采用的候选产生式,当元素内容无产生式时,则表明用A 的左部向下推导时出现了不该出现的符号,因此元素内容转向出错处理的信息。LL(1)分析过程主要包括以下四个动作:替换:当XVN时选相应产生式的右部b去替换X。此时X出栈,b逆序入栈。匹配:当XVT时它与a进行匹配,其结果可能成功,也可能失败,如果成功则符号栈中将X退栈并将输入流指针向前移动一位,否则报错。接受:当格局为(#,空#)时报告分析成功。报错:出错后,停止分析。并给出相应的错误提示信息。实验记录#include stdio.h
7、#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; /*右部结点头指针*/;char VnMaxVnNum + 1
8、; /*非终结符集*/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 collectNode* followMaxVnNum + 1;
9、 /*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的内容*/void InputP();/*产生式输入*/bool Chec
10、kP(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 FirstFollow();/*计算first和follow*/void CreateAT(
11、);/*构造预测分析表*/void ShowAT();/*输出分析表*/void Identify(char *st);void InitStack();void ShowStack();void Pop();void Push(int r);int main(void) char todo,ch; Init(); InputVn(); InputVt(); InputP(); getchar(); FirstFollow(); printf(所得first集为:); ShowCollect(first); printf(所得follow集为:); ShowCollect(follow); C
12、reateAT(); 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 MaxStLength) if( != ch & n != ch) sti+
13、= 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 = 0; i MaxRuleNum; i+) Pi.lCursor = NULL; Pi.rHead = NULL; Pi
14、.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 = 0; /*is Vn?*/ while(ch != Vnn & 0 != Vnn) n+; if(0 != Vnn)
15、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 inErr = 1; int n,k; char ch; while(inErr) printf(n请输入所有的非终结
16、符,注意:); 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(# != ch) if( # != (ch = getchar() while(# != (ch = getchar
17、() ; 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; /*输入终结符*/void InputVt() int inErr = 1; int n,k; char ch; wh
18、ile(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) if( # != (ch = getchar() while(# != (ch = getchar() ; pri
19、ntf(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 i = 0, n,num; printf(请输入文法产生式的个数:); scanf(%d, &num); PNum
20、= 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); pt-next = NULL; Pi.rHead = pt; n = 4; while(0 != buffern
21、) 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 false; if(- != st1) return false; if( != st2) return false; f
22、or(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(NULL = firstpt-rCursor - 100) First(pt-rCursor); AddFirst(
23、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(nCh nVt = nCh) break; else qt = pt; pt = pt-next; if(NULL =
24、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 = pt-nVt; if(-1 != ch) AddFirst(U, ch); pt = pt-next; bool
25、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 = pt) if(NULL = followPi.lCursor - 100 & Pi.lCursor != V) Fo
26、llow(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.lCursor, 0); else AddFollow(V, pt-rCursor, 1); void AddFollow(
27、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 = NULL; if(NULL = followV - 100) followV - 100 = pt; else qt-nex
28、t = 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); pt = pt-next; /*输出first或follow集*/void ShowCollect(struct c
29、ollectNode *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*/void FirstFollow() int i; i = 0; while(0 != Vni) if(NULL = fi
30、rsti) 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; ct = ct-next; pt = pt-next; if(NULL = pt) ct = followPi.lCursor - 100; while(NULL != ct) if(-1 != ct-nVt) analyseTablePi.lCursor - 100ct-nVt = i; else analyseTablePi.lCursor - 100vtNum = i; ct = ct-next; else if(100 rCursor) /*不含空的非终结符*/ ct = firstpt-rCursor - 100; while(NULL != ct) anal
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 建设工程合同范本及解读
- 普通话推广主题班会活动方案
- 2025及未来5年中国龙眼银耳茶市场调查、数据监测研究报告
- 企业行政人事部门职责说明
- 五年级科学水循环单元知识点梳理
- 企业员工职业成长培训计划
- 社会工作者典型工作心得体会
- 小学生写作能力提升专项练习题汇编
- 包装工岗位职责说明书
- 放射性药品许可证申请流程与要求
- 2025年安徽省社区工作者招聘考试(职业能力测试/职业能力倾向测验)历年参考题库含答案详解(5套)
- 2025店铺租赁合同对承租方有利范本
- 2025年员额法官入额考试题库
- 2025年“学宪法讲宪法”主题活动知识竞赛题库附答案
- 护理部季度工作汇报
- 烟雾病护理问题及护理措施
- 烟草职称管理办法
- 协会项目库管理办法
- 村委会印章使用管理制度
- 高校负面清单管理办法
- 2024年度网络安全培训(安全意识)课件
评论
0/150
提交评论