优化这段代码#include <iostream> #include <iomanip> #include <stdio.h> #include <fstream> #include <string> #include <algorithm> #include <unordered_map> #include <map> #include <stack> #include <set> #include <vector> using namespace std; struct Production { char left; // 产生式的左部非终结符 string right; // 产生式的右部字符串 }; map<char, set<char>> firstSets; int main() { vector<Production> productions; ifstream infile("grammar.txt"); char left; string right; while (infile >> left >> right) { productions.push_back({ left, right }); } for (auto p : productions) { char left = p.left; string right = p.right; char firstChar = right[0]; if (isupper(firstChar)) { // 如果是非终结符 // 计算非终结符的first集合 set<char> firstSet = firstSets[firstChar]; for (int i = 0; i < right.length(); i++) { char ch = right[i]; if (isupper(ch)) { // 如果是非终结符 firstSet.insert(firstSets[ch].begin(), firstSets[ch].end()); if (firstSets[ch].find('$') == firstSets[ch].end()) { // 如果该非终结符没有空串 break; } } else { // 如果是终结符 firstSet.insert(ch); break; } } firstSets[left].insert(firstSet.begin(), firstSet.end()); } else { // 如果是终结符 firstSets[left].insert(firstChar); } } for (auto p : productions) { char left = p.left; cout << "First(" << left << ") = {"; for (auto ch : firstSets[left]) { cout << ch << ", "; } cout << "}" << endl; } }使得能正确求出frist集
时间: 2024-02-14 17:09:23 浏览: 63
以下是优化后的代码:
```c++
#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>
#include <vector>
#include <map>
#include <set>
using namespace std;
// 产生式结构体
struct Production {
char left; // 产生式的左部非终结符
string right; // 产生式的右部字符串
};
// 计算first集合
set<char> calcFirstSet(char ch, const vector<Production>& productions, map<char, set<char>>& firstSets) {
// 如果该字符已经计算过first集合,直接返回
if (firstSets.find(ch) != firstSets.end()) {
return firstSets[ch];
}
set<char> result;
// 遍历所有产生式,查找该字符作为左部的产生式
for (auto p : productions) {
if (p.left == ch) {
// 如果右部第一个字符是终结符,则加入该字符
if (!isupper(p.right[0])) {
result.insert(p.right[0]);
}
// 如果右部第一个字符是非终结符,则计算该非终结符的first集合
else {
set<char> subResult = calcFirstSet(p.right[0], productions, firstSets);
result.insert(subResult.begin(), subResult.end());
// 如果该非终结符的first集合中包含空串,则计算下一个字符的first集合
for (int i = 1; i < p.right.length() && subResult.find('$') != subResult.end(); i++) {
if (!isupper(p.right[i])) {
result.insert(p.right[i]);
break;
} else {
subResult = calcFirstSet(p.right[i], productions, firstSets);
result.insert(subResult.begin(), subResult.end());
}
}
}
}
}
// 将计算得到的first集合插入到map中
firstSets.insert(make_pair(ch, result));
return result;
}
int main() {
vector<Production> productions;
ifstream infile("grammar.txt");
// 读取产生式
char left;
string right;
while (infile >> left >> right) {
productions.push_back({ left, right });
}
map<char, set<char>> firstSets;
// 计算每个非终结符的first集合
for (auto p : productions) {
if (isupper(p.right[0])) {
set<char> result = calcFirstSet(p.left, productions, firstSets);
firstSets.insert(make_pair(p.left, result));
} else {
firstSets.insert(make_pair(p.left, set<char>{p.right[0]}));
}
}
// 输出每个非终结符的first集合
for (auto p : productions) {
char left = p.left;
cout << "First(" << left << ") = {";
for (auto ch : firstSets[left]) {
cout << ch << ", ";
}
cout << "}" << endl;
}
}
```
优化后的代码主要是将计算first集合的部分提取出来,封装成了一个函数`calcFirstSet`。在计算每个非终结符的first集合时,只需调用该函数进行计算即可。同时,在计算非终结符的first集合时,使用了递归的方式,可以很方便地计算出包含空串的情况。
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![](https://img-home.csdnimg.cn/images/20210720083646.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)