一、实验目的
通过预测分析算法的设计与实现,加深对自上而下语法分析方法的理解;能够采用一种编程语言实现简单的语法分析程序;能够使用自己编写的分析程序对简单的程序段进行语法翻译;加深对语法分析器工作过程的理解。
二、实验内容
用预测分析法编制语法分析程序,输入文法及待分析的输入串,输出其预测分析过程及结果。
三、实验要求
1.对语法规则有明确的定义;
2.编写的分析程序能够对输入的文法和待分析串进行正确的语法分析;
3.对于遇到的语法错误,能够做出简单的错误处理,给出简单的错误提示,保证顺利完成语法分析过程;
4.实验报告要求对语法定义做出详细说明,说明语法分析程序的工作过程。
四、实验步骤
1.定义目标语言的语法规则;
2.判断文法的左递归性,将左递归文法转换成非左递归文法。(该步骤可以省略,直接输入非左递归文法);
3.根据文法求FIRST集和FOLLOW集;
4.求解预测分析方法需要的分析表;
5.输入文法及待分析的输入串,输出其预测分析过程及结果。
五、实验内容
1.编程思路、源代码
编程思路:
语法分析是编译程序的核心。它的作用是识别由词法分析给出的单词符号串是否是给定文法的正确句子(程序)。通过求给定的表达式文法的First集和Follow集,然后得出预测分析表,输入串根据预测分析表进行分析,输出分析过程 。
源代码:
#include <stdio.h>
#include <stdlib.h>
#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; / 右部长度 /
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;
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];
int analyseStack[MaxStackDepth + 1]; / 分析栈 /
int topAnalyse; / 分析栈顶 /
void Init();/ 初始化 /
int IndexCh(char ch); //
void InputVt(); / 输入终结符 /
void InputVn();/ 输入非终结符 /
void ShowChArray(char collect, 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);/ 输出 first 或 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;
printf(" 语法分析器\n");
Init();
InputVn(); //输入非终结符
InputVt();//输入终结符
InputP();
getchar();
FirstFollow();
printf(“所有的非终结符 first 集为: “);
ShowCollect(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;
while(’#’ != ch && i < MaxStLength)
{
if(’ ’ != ch && ‘\n’ != ch)
{ st[i++] = ch;
}
ch = getchar();
}
if(’#’ == ch && i < MaxStLength)
{ st[i] = ch;
Identify(st);
}
语法分析器的构造
最新推荐文章于 2025-06-06 14:16:57 发布