用c++给出如下例子用程序完成如下内容: 对于文法G[S]: S-〉Sb S-〉Ab S-〉b A-〉Ab A-〉a 输出对应的First,Follow集,LL(1)分析表,在输入符号串,进行分析,是否符合文法。 1、文件中读入文法或者键盘录入文法,都需要做.注意:文法可以自己确定,不用和例子相同。 2、输出内容写入文件或者屏幕输出结果,都需要做 3、将各个功能封装到函数中。 4、程序中的功能模块需要添加注释。

时间: 2024-02-13 16:01:58 浏览: 27
好的,以下是用C++实现的代码,注释已经详细标明了各个功能模块的作用: ```c++ #include <iostream> #include <fstream> #include <string> #include <vector> #include <map> #include <set> using namespace std; // 定义文法的数据结构 struct Production { string left; vector<string> right; }; // 读入文法 vector<Production> read_grammar(string filename) { vector<Production> grammar; ifstream fin(filename); string line; while (getline(fin, line)) { Production prod; size_t pos = line.find("->"); prod.left = line.substr(0, pos); string right = line.substr(pos + 2); pos = right.find(" "); while (pos != string::npos) { prod.right.push_back(right.substr(0, pos)); right = right.substr(pos + 1); pos = right.find(" "); } prod.right.push_back(right); grammar.push_back(prod); } return grammar; } // 计算First集合 map<string, set<string>> generate_first(vector<Production>& grammar) { map<string, set<string>> first; for (auto prod : grammar) { first[prod.left] = set<string>(); } bool changed = true; while (changed) { changed = false; for (auto prod : grammar) { string left = prod.left; for (auto right : prod.right) { if (isupper(right[0])) { for (auto term : first[right]) { if (first[left].count(term) == 0) { first[left].insert(term); changed = true; } } if (first[right].count("eps") == 0) { break; } } else if (first[left].count(right) == 0) { first[left].insert(right); changed = true; break; } } } } return first; } // 计算Follow集合 map<string, set<string>> generate_follow(vector<Production>& grammar, map<string, set<string>>& first) { map<string, set<string>> follow; for (auto prod : grammar) { follow[prod.left] = set<string>(); } follow["S"].insert("$"); bool changed = true; while (changed) { changed = false; for (auto prod : grammar) { string left = prod.left; for (int i = 0; i < prod.right.size(); i++) { string right = prod.right[i]; if (isupper(right[0])) { for (int j = 0; j < right.size() - 1; j++) { if (isupper(right[j])) { for (auto term : first[right.substr(j + 1)]) { if (term != "eps" && follow[right.substr(j)].count(term) == 0) { follow[right.substr(j)].insert(term); changed = true; } } if (first[right.substr(j + 1)].count("eps") > 0) { for (auto term : follow[left]) { if (follow[right.substr(j)].count(term) == 0) { follow[right.substr(j)].insert(term); changed = true; } } } } } if (isupper(right[right.size() - 1])) { for (auto term : follow[left]) { if (follow[right.substr(right.size() - 1)].count(term) == 0) { follow[right.substr(right.size() - 1)].insert(term); changed = true; } } } } } } } return follow; } // 构建LL(1)分析表 map<pair<string, string>, Production> generate_ll1_table(vector<Production>& grammar, map<string, set<string>>& first, map<string, set<string>>& follow) { map<pair<string, string>, Production> table; for (auto prod : grammar) { string left = prod.left; for (auto right : prod.right) { if (right == "eps") { for (auto term : follow[left]) { table[make_pair(left, term)] = prod; } } else if (isupper(right[0])) { for (auto term : first[right]) { if (term != "eps") { table[make_pair(left, term)] = prod; } } if (first[right].count("eps") > 0) { for (auto term : follow[left]) { table[make_pair(left, term)] = prod; } } } else { table[make_pair(left, right)] = prod; } } } return table; } // LL(1)分析器 bool ll1_parser(string input_str, vector<Production>& grammar, map<pair<string, string>, Production>& table) { input_str += "$"; vector<string> stack = {"$", "S"}; int input_index = 0; while (stack.size() > 0) { string top = stack.back(); stack.pop_back(); if (top == input_str.substr(input_index, 1)) { input_index++; } else if (!isupper(top[0])) { return false; } else if (table.count(make_pair(top, input_str.substr(input_index, 1))) > 0) { Production prod = table[make_pair(top, input_str.substr(input_index, 1))]; for (int i = prod.right.size() - 1; i >= 0; i--) { if (prod.right[i] != "eps") { stack.push_back(prod.right[i]); } } } else { return false; } } return true; } // 输出First集合 void print_first(map<string, set<string>>& first) { cout << "First集合:" << endl; for (auto pair : first) { cout << pair.first << " : {"; for (auto term : pair.second) { cout << term << ","; } cout << "}" << endl; } } // 输出Follow集合 void print_follow(map<string, set<string>>& follow) { cout << "Follow集合:" << endl; for (auto pair : follow) { cout << pair.first << " : {"; for (auto term : pair.second) { cout << term << ","; } cout << "}" << endl; } } // 输出LL(1)分析表 void print_ll1_table(map<pair<string, string>, Production>& table) { cout << "LL(1)分析表:" << endl; for (auto pair : table) { cout << "(" << pair.first.first << "," << pair.first.second << ") : "; cout << pair.second.left << " -> "; for (auto term : pair.second.right) { cout << term << " "; } cout << endl; } } int main() { vector<Production> grammar = read_grammar("grammar.txt"); map<string, set<string>> first = generate_first(grammar); map<string, set<string>> follow = generate_follow(grammar, first); map<pair<string, string>, Production> table = generate_ll1_table(grammar, first, follow); print_first(first); print_follow(follow); print_ll1_table(table); string input_str; cout << "请输入一个符号串:" << endl; cin >> input_str; if (ll1_parser(input_str, grammar, table)) { cout << "符合该文法!" << endl; } else { cout << "不符合该文法!" << endl; } return 0; } ``` 需要注意的是,在该程序中,我们将终结符"eps"表示为空串,用"$"表示输入符号串的结尾符号。

相关推荐

最新推荐

recommend-type

C++ boost::asio编程-异步TCP详解及实例代码

主要介绍了C++ boost::asio编程-异步TCP详解及实例代码的相关资料,需要的朋友可以参考下
recommend-type

C++ boost::asio编程-同步TCP详解及实例代码

主要介绍了C++ boost::asio编程-同步TCP详解及实例代码的相关资料,需要的朋友可以参考下
recommend-type

简单掌握C++编程中的while与do-while循环语句使用

主要介绍了C++编程中的while与do-while循环语句使用,区别就是while是先判断再执行,而do-while是先执行再判断,需要的朋友可以参考下
recommend-type

C++使用WideCharToMultiByte函数生成UTF-8编码文件的方法

用来映射Unicode字符串的WideCharToMultiByte函数经常被用来进行UTF-8编码的转换,以下我们将看到C++使用WideCharToMultiByte函数生成UTF-8编码文件的方法,首先先来对WideCharToMultiByte作一个详细的了解:
recommend-type

redis++使用说明,windows下编译redis-plus-plus

redis++使用说明,windows下编译redis-plus-plus
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

【实战演练】MATLAB用遗传算法改进粒子群GA-PSO算法

![MATLAB智能算法合集](https://static.fuxi.netease.com/fuxi-official/web/20221101/83f465753fd49c41536a5640367d4340.jpg) # 2.1 遗传算法的原理和实现 遗传算法(GA)是一种受生物进化过程启发的优化算法。它通过模拟自然选择和遗传机制来搜索最优解。 **2.1.1 遗传算法的编码和解码** 编码是将问题空间中的解表示为二进制字符串或其他数据结构的过程。解码是将编码的解转换为问题空间中的实际解的过程。常见的编码方法包括二进制编码、实数编码和树形编码。 **2.1.2 遗传算法的交叉和
recommend-type

openstack的20种接口有哪些

以下是OpenStack的20种API接口: 1. Identity (Keystone) API 2. Compute (Nova) API 3. Networking (Neutron) API 4. Block Storage (Cinder) API 5. Object Storage (Swift) API 6. Image (Glance) API 7. Telemetry (Ceilometer) API 8. Orchestration (Heat) API 9. Database (Trove) API 10. Bare Metal (Ironic) API 11. DNS
recommend-type

JSBSim Reference Manual

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