设计简单算数表达式语法分析器算法(LR来实现)

#include<iostream>
#include<stack>
#include<algorithm>
using namespace std;
char L[20] = "i+-*/()#ETF";//判断依据
int search(char c)//对应列寻找
{
	int i = 0;
	while (L[i] != '\0')
	{
		if (c == L[i])
			return i;
		i++;
	}
	if (c >= 'a' && c <= 'z' || c >= '0' && c <= '9')
	{
		return 0;
	}
	return -1;

}
int toTen(string s)
{
	int num = 0;
	int i = 1;
	while (s[i] != '\0')
	{
		num = num * 10 + s[i] - '0';
		i++;
	}
	return num;
}
int main()
{
	//分析表
	string LR[16][12] = {
		//i      +     -     *     /      (      )     #       E    T    F
		{"s1","null","null","null","null","s5","null","null","s2","s3","s4"},
		{"null","r8","r8","r8","r8","null","r8","r8","null","null","null"},
		{"null","s6","s7","null","null","null","null","acc","null","null","null"},
		{"null","r3","r3","s8","s9","null","r3","r3","null","null","null"},
		{"null","r6","r6","r6","r6","null","r6","r6","null","null","null"},
		{"s1","null","null","null","null","s5","null","null","s15","s3","s4"},
		{"s1","null","null","null","null","s5","null","null","null","s13","s4"},
		{"s1","null","null","null","null","s5","null","null","null","s14","s4"},
		{"s1","null","null","null","null","s5","null","null","null","null","s11"},
		{"s1","null","null","null","null","s5","null","null","null","null","s10"},
		{"null","r5","r5","r5","r5","null","r5","r5","null","null","null"},
		{"null","r4","r4","r4","r4","null","r4","r4","null","null","null"},
		{"null","r7","r7","r7","r7","null","r7","r7","null","null","null"},
		{"null","r1","r1","s8","s9","null","r1","r1","null","null","null"},
		{"null","r2","r2","s8","s9","null","r2","r2","null","null","null"},
		{"null","s6","s7","null","null","null","s12","null","null","null","null"}
	};
	stack<int>status;//状态栈
	stack<char>letter;//符号栈
	status.push(0);
	letter.push('#');
	int len[10] = { 0,3,3,1,3,3,1,3,1 };//1-8号文法每个文法长度
	char head[20] = { 'S','E','E','E','T','T','T','F','F' };


	string str;
	cin >> str;//读入一段算术表达式
	str += '#';//在末尾加上结束标识符
	int index = 0;
	while (index < str.length())
	{
		int column = search(str[index]);
		if (LR[status.top()][column][0] == 's')//移进
		{
			int st1 = toTen(LR[status.top()][column]);
			status.push(st1);//状态入栈
			letter.push(str[index]);//符号入栈
		}
		else if (LR[status.top()][column][0] == 'r')//规约
		{
			while (LR[status.top()][column][0] == 'r')//若继续规约
			{
				int st1 = toTen(LR[status.top()][column]);//规约表达式序号
				char ch = head[st1];//规约所得符号
				int st2 = len[st1];//规约表达式长度
				for (int i = 0; i < st2; i++)//状态和符号出栈
				{
					status.pop();
					letter.pop();
				}
				letter.push(ch);//将规约符号入栈
				int col = search(ch);//找到规约符号对应列
				status.push(toTen(LR[status.top()][col]));//将规约符号对应的状态入栈
			}
			if (LR[status.top()][column] == "acc")//结束标志
			{
				cout << endl << "true";
				break;
			}
			else if(LR[status.top()][column][0] == 's')//移进标志
			{
				letter.push(str[index]);//符号入栈
				status.push(toTen(LR[status.top()][column]));//对应状态入栈
			}
			else//错误
			{
				cout << endl << "false" << endl;
				break;
			}
			
		}
		else if ((LR[status.top()][column] == "acc"))
		{
			cout <<endl<< "true" ;
			break;
		}
		else
		{
			cout <<endl<< "false";
			break;
		}
		index++;
	}
	return 0;
}

毕业论文引言 随着计算机技术的发展与普及,计算机已经成为各行业最基本的工具之一,迅速进入千家万户。因此,掌握计算机应用的基本技能成为新世纪人才不可缺少的基本素质之一。为使计算机能正常工作, 除了构成计算机各个组成部分的物理设备外, 一般说来, 还必须要有指挥计算机“做什么”和“如何做”的“程序”。程序及其有关文档构成计算机软件, 其中用以书写计算机软件的语言称为计算机程序设计语言。 1 计算机程序设计语言简介 计算机程序设计语言是计算机可以识别的语言,用于描述解决问题的方法,供计算机阅读和执行,通常简称为编程语言,是一组用来定义计算机程序的语法规则。它是一种被标准化的交流技巧,用来向计算机发出指令。一种计算机语言让程序员能够准确地定义计算机所需要使用的数据,并精确地定义在不同情况下所应当采取的行动。使用程序设计语言往往使程序员能够比使用机器语言更准确地表达他们所想表达的目的。对那些从事计算机科学的人来说,懂得程序设计语言是十分重要的,因为所有的程序都需要程序设计语言才能完成,而计算机的工作是用程序来控制的,离开了程序,计算机将一事无成。 2 开发背景及意义 现有计算器不能计算表达式,这是一个缺陷,为此,开发了一个能直接计算表达式的计算器,这为计算提高了更大的方便,可以大幅度提高计算效率。 第二章 第三章 第一节 递归下降法的描述 3.1.1实现思想 它的主要原理是,对每个非终极符按其产生式结构构造相应语法分析子程序,其中终极符产生匹配命令,而非终极符则产生过程调用命令。因为文法递归相应子程序也递归,所以称这种方法为递归子程序下降法或递归下降法。其中子程序的结构与产生式结构几乎是一致的。文法中每个非终结符对应一个递归过程(子程序),每个过程的功能是识别由该非终结符推出的串,当某非终结符的产生式有多个候选式时能够按LL(1)形式可唯一地确定选择某个候选式进行推导。 3.1.2算法的特点 递归下降法是语法分析中最易懂的一种方法。递归下降法要满足的条件:假设A的全部产生式为Aα1|α2|……|αn ,则必须满足如下条件才能保证可以唯一的选择合适的产生式 predict(Aαi)∩predict(Aαj)=Φ,当i≠j. 3.1.3构造递归下降语法分析程序 采用了递归子程序方法进行语法分析,对文法中的每个非终极符号按其产生式结构产生相应的语法分析子程序,完成相应的识别任务。其中终结符产生匹配命令,非终结符则产生调用命令。每次进入子程序之前都预先读入一个单词。因为使用了递归下降方法,所以程序结构和层次清晰明了,易于手工实现,且时空效率较高。实际的语法分析工作,从调用总程序的分析子程序开始,根据产生式进行递归调用各个分析子程序。 第二节
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值