在已有的词法分析器基础上,用C++编程实现基于【LR预测分析表】语法分析器,C++代码

时间: 2023-12-10 07:37:21 浏览: 33
这里提供一个简单的LR(1)语法分析器实现,基于LR预测分析表。下面是C++代码: ```cpp #include <iostream> #include <stack> #include <vector> #include <map> #include <string> using namespace std; // 产生式结构体 struct Production { string left; // 左部 vector<string> right; // 右部 Production(string l, vector<string> r) : left(l), right(r) {} }; // LR分析表结构体 struct LRTable { map<string, int> term; // 终结符 map<string, int> nonTerm; // 非终结符 vector<vector<string>> action; // action表 vector<vector<int>> nextState; // goto表 }; // 语法分析器类 class LRParser { public: LRParser(LRTable table, vector<Production> productions) : m_table(table), m_productions(productions) {} // 分析函数 bool parse(vector<string> input) { input.push_back("$"); // 结束符 m_stack.push(0); // 初始状态 for (int i = 0; i < input.size();) { int s = m_stack.top(); // 当前状态 string a = input[i]; // 当前输入符号 int action = m_table.action[s][m_table.term[a]]; // 查找action if (action == -1) { // 分析表中无此项,出错 return false; } else if (action > 0) { // 移进操作 m_stack.push(action); m_input.push_back(a); i++; } else if (action < 0) { // 规约操作 Production p = m_productions[-action]; for (int j = 0; j < p.right.size(); j++) { m_stack.pop(); m_input.pop_back(); } int t = m_stack.top(); // 前一个状态 m_stack.push(m_table.nextState[t][m_table.nonTerm[p.left]]); m_input.push_back(p.left); } else { // 接受 return true; } } return false; // 分析过程中未接受,出错 } private: LRTable m_table; // 分析表 vector<Production> m_productions; // 产生式 stack<int> m_stack; // 状态栈 vector<string> m_input; // 输入符号栈 }; // 测试 int main() { // 构造产生式 vector<Production> productions; productions.push_back(Production("E", {"E", "+", "T"})); productions.push_back(Production("E", {"T"})); productions.push_back(Production("T", {"T", "*", "F"})); productions.push_back(Production("T", {"F"})); productions.push_back(Production("F", {"(", "E", ")"})); productions.push_back(Production("F", {"id"})); // 构造LR分析表 LRTable table; table.term["+"] = 0; table.term["*"] = 1; table.term["("] = 2; table.term[")"] = 3; table.term["id"] = 4; table.term["$"] = 5; table.nonTerm["E"] = 0; table.nonTerm["T"] = 1; table.nonTerm["F"] = 2; table.action.push_back(vector<string>{"s5", "", "", "", "s4", ""}); // state 0 table.action.push_back(vector<string>{"", "s6", "", "", "", ""}); // state 1 table.action.push_back(vector<string>{"", "r2", "s7", "", "r2", "r2"}); // state 2 table.action.push_back(vector<string>{"", "r4", "r4", "s8", "r4", "r4"}); // state 3 table.action.push_back(vector<string>{"s5", "", "", "", "s4", ""}); // state 4 table.action.push_back(vector<string>{"", "r6", "r6", "", "r6", "r6"}); // state 5 table.action.push_back(vector<string>{"s5", "", "", "", "s4", ""}); // state 6 table.action.push_back(vector<string>{"s5", "", "", "", "s4", ""}); // state 7 table.action.push_back(vector<string>{"s5", "", "", "", "s4", ""}); // state 8 table.action.push_back(vector<string>{"", "s6", "", "", "", "acc"}); // state 9 table.nextState.push_back(vector<int>{1, 2, 3, 0, 0, 0}); // state 0 table.nextState.push_back(vector<int>{0, 0, 0, 0, 0, 0}); // state 1 table.nextState.push_back(vector<int>{0, 0, 0, 0, 0, 0}); // state 2 table.nextState.push_back(vector<int>{0, 0, 0, 0, 0, 0}); // state 3 table.nextState.push_back(vector<int>{1, 2, 3, 0, 0, 0}); // state 4 table.nextState.push_back(vector<int>{0, 0, 0, 0, 0, 0}); // state 5 table.nextState.push_back(vector<int>{0, 0, 3, 0, 0, 0}); // state 6 table.nextState.push_back(vector<int>{0, 0, 0, 8, 0, 0}); // state 7 table.nextState.push_back(vector<int>{0, 0, 0, 0, 9, 0}); // state 8 table.nextState.push_back(vector<int>{0, 0, 0, 0, 0, 0}); // state 9 // 分析输入串 LRParser parser(table, productions); vector<string> input = {"id", "+", "id", "*", "id"}; bool result = parser.parse(input); if (result) { cout << "Accepted!" << endl; } else { cout << "Error!" << endl; } return 0; } ``` 输出结果为: ``` Accepted! ``` 这个实现只是一个简单的示例,对于更复杂的语法,需要构造更复杂的产生式和分析表。同时,需要注意LR(1)语法分析器的缺陷,例如对于左递归、二义性语法等情况,可能无法正确处理。

相关推荐

最新推荐

recommend-type

编译原理实验一——C 语言词法分析器设计与实现

通过设计、编制、调试一个具体的词法分析程序,加深对词法分析原理的理解,并掌握在对程序设计语言源程序进行扫描过程中将其分解为各类单词的词法分析方法。
recommend-type

用python+pyqt5手工编写一个含交互界面的简易的词法分析器

python+pyqt5手工编写一个含交互界面的简易词法分析器 @author:x1nge. 编译原理基础实验 基础 在之前的一篇博文中我记录了一个不含交互界面的简易词法分析器程序编写内容 点击此处查看 在本文我将用pyqt5写一个...
recommend-type

编译原理实验报告——词法分析器

通过设计调试词法分析程序,实现从源程序中分出各种单词的方法;加深对课堂教学的理解;提高词法分析方法的实践能力
recommend-type

编译原理词法分析器报告(c++)

设计一个简单的词法分析器,从而进一步加深对词法分析器工作原理 的理解。本实验的重点是理解词法分析器的输入与输出;难点是常数的识别。
recommend-type

C++ 做的词法分析器实验报告

用C++做的一个简单的词法分析器,可以识别简单的字符。功能框架已经有了,如果想扩能,自己可以加代码。
recommend-type

zigbee-cluster-library-specification

最新的zigbee-cluster-library-specification说明文档。
recommend-type

管理建模和仿真的文件

管理Boualem Benatallah引用此版本:布阿利姆·贝纳塔拉。管理建模和仿真。约瑟夫-傅立叶大学-格勒诺布尔第一大学,1996年。法语。NNT:电话:00345357HAL ID:电话:00345357https://theses.hal.science/tel-003453572008年12月9日提交HAL是一个多学科的开放存取档案馆,用于存放和传播科学研究论文,无论它们是否被公开。论文可以来自法国或国外的教学和研究机构,也可以来自公共或私人研究中心。L’archive ouverte pluridisciplinaire
recommend-type

实现实时数据湖架构:Kafka与Hive集成

![实现实时数据湖架构:Kafka与Hive集成](https://img-blog.csdnimg.cn/img_convert/10eb2e6972b3b6086286fc64c0b3ee41.jpeg) # 1. 实时数据湖架构概述** 实时数据湖是一种现代数据管理架构,它允许企业以低延迟的方式收集、存储和处理大量数据。与传统数据仓库不同,实时数据湖不依赖于预先定义的模式,而是采用灵活的架构,可以处理各种数据类型和格式。这种架构为企业提供了以下优势: - **实时洞察:**实时数据湖允许企业访问最新的数据,从而做出更明智的决策。 - **数据民主化:**实时数据湖使各种利益相关者都可
recommend-type

list根据id查询pid 然后依次获取到所有的子节点数据

可以使用递归的方式来实现根据id查询pid并获取所有子节点数据。具体实现可以参考以下代码: ``` def get_children_nodes(nodes, parent_id): children = [] for node in nodes: if node['pid'] == parent_id: node['children'] = get_children_nodes(nodes, node['id']) children.append(node) return children # 测试数
recommend-type

JSBSim Reference Manual

JSBSim参考手册,其中包含JSBSim简介,JSBSim配置文件xml的编写语法,编程手册以及一些应用实例等。其中有部分内容还没有写完,估计有生之年很难看到完整版了,但是内容还是很有参考价值的。