1 / 22
文档名称:

LL1语法分析器实验报告.doc

格式:doc   大小:81KB   页数:22页
下载后只包含 1 个 DOC 格式的文档,没有任何的图纸或源代码,查看文件列表

如果您已付费下载过本站文档,您可以点这里二次下载

分享

预览

LL1语法分析器实验报告.doc

上传人:63229029 2017/2/3 文件大小:81 KB

下载得到文件列表

LL1语法分析器实验报告.doc

文档介绍

文档介绍:南京信息工程大学实验(实****报告实验(实****名称 LL (1)文法语法分析设计实验(实****日期 11月 28日得分指导教师林美华系计算机专业计算机科学与技术年级 2011 班次计科 3班姓名王欣学号 20112308915 一. 实验目的 1 .熟悉判断 LL (1 )文法的方法及对某一输入串的分析过程。 2 .学会构造表达式文法的预测分析表。二. 实验内容编写一个语法分析程序,对于给定的输入串,能够判断识别该串是否为给定文法的句型。三. 实验步骤从键盘读入输入串,并判断正误; 若无误,由程序自动构造 FIRST 、 FOLLOW 集以及 SELECT 集合,判断是否为 LL(1 )文法; 若符合 LL(1 )文法,由程序自动构造 LL(1 )分析表; 由算法判断输入符号串是否为该文法的句型【源代码】#include "" #include "" #define MaxRuleNum 8 #define MaxVnNum 5 #define MaxVtNum 5 #define MaxStackDepth 20 #define MaxPLength 20 #define MaxStLength 50 struct pRNode /* 产生式右部结构*/ { int rCursor; /* 右部序号*/ struct pRNode *next; }; struct pNode /* 产生式结点结构*/ { int lCursor; /* 左部符号序号*/ int rLength; /* 右部长度*/ /* 注当 rLength =1 时, rCursor = -1 为空产生式*/ struct pRNode *rHead; /* 右部结点头指针*/ }; char Vn[MaxVnNum + 1]; /* 非终结符集*/ int vnNum; char Vt[MaxVtNum + 1]; /* 终结符集*/ int vtNum; struct pNode P[MaxRuleNum]; /* 产生式*/ int PNum; /* 产生式实际个数*/ char buffer[MaxPLength + 1]; char ch; /* 符号或 string ch;*/ char st[MaxStLength]; /* 要分析的符号串*/ struct collectNode /* 集合元素结点结构*/ { int nVt; /* 在终结符集中的下标*/ struct collectNode *next; }; struct collectNode* first[MaxVnNum + 1]; /*first 集*/ struct collectNode* follow[MaxVnNum + 1]; /*follow 集*/ int analyseTable[MaxVnNum + 1][MaxVtNum +1+ 1]; /* 预测分析表存放为产生式的编号, +1 用于存放结束符,多+1 用于存放#(-1)*/ int analyseStack[MaxStackDepth + 1]; /* 分析栈*/ int topAnalyse; /* 分析栈顶*/ /*int reverseStack[MaxStackDepth + 1]; /* 颠倒顺序栈*/ /*int topReverse; /* 倒叙栈顶*/ void Init();/* 初始化*/ int IndexCh(char ch); /* 返回 Vn在 Vn 表中的位置+100 、 Vt在 Vt 表中的位置, -1 表示未找到*/ void InputVt(); /* 输入终结符*/ void InputVn();/* 输入非终结符*/ void ShowChArray(char* collect, int num);/* 输出 Vn或 Vt 的内容*/ void InputP();/* 产生式输入*/ bool CheckP(char * st);/* 判断产生式正确性*/ void First(int U);/* 计算 first 集,U->xx...*/ void AddFirst(int U, int nCh); /* 加入 first 集*/ bool HaveEmpty(int nVn); /* 判断 first 集中是否有空(-1)*/ void Follow(int V);/* 计算 follow 集*/ void AddFollow(int V, int nCh, int kind);/* 加入 follow 集, kind =0 表加入 follow 集, kind =1 加入 first 集*/ void ShowCollect(st