LL1文法分析器c++代码
时间: 2023-11-06 16:05:34 浏览: 139
递归下降LL1文法实现文法分析附完整代码C
LL(1)文法分析器的C++代码如下:
```
#include <iostream>
#include <string>
#include <stack>
#include <map>
using namespace std;
// 定义文法符号类型
enum SymbolType {
TERMINAL, // 终结符
NONTERMINAL // 非终结符
};
// 定义文法符号结构体
struct Symbol {
SymbolType type; // 符号类型
string name; // 符号名称
};
// 定义产生式结构体
struct Production {
Symbol left; // 产生式左部
vector<Symbol> right; // 产生式右部
};
// 定义LL(1)文法分析器类
class LL1Parser {
public:
LL1Parser(vector<Production> productions, Symbol startSymbol, vector<Symbol> terminals, vector<Symbol> nonterminals, map<Symbol, map<Symbol, Production>> parsingTable) :
productions(productions), startSymbol(startSymbol), terminals(terminals), nonterminals(nonterminals), parsingTable(parsingTable) {}
// 分析函数
bool parse(string input) {
stack<Symbol> symbolStack;
symbolStack.push(Symbol{ TERMINAL, "$" }); // 压入结束符
symbolStack.push(startSymbol); // 压入开始符号
int i = 0;
while (!symbolStack.empty()) {
Symbol topSymbol = symbolStack.top();
symbolStack.pop();
if (topSymbol.type == TERMINAL) {
if (topSymbol.name == "$" && input[i] == '$') {
return true; // 分析成功
}
else if (topSymbol.name == string(1, input[i])) {
i++;
}
else {
return false; // 分析失败
}
}
else if (topSymbol.type == NONTERMINAL) {
Production production = parsingTable[topSymbol][Symbol{ TERMINAL, string(1, input[i]) }];
if (production.left.name != "") {
for (int j = production.right.size() - 1; j >= 0; j--) {
symbolStack.push(production.right[j]);
}
}
else {
return false; // 分析失败
}
}
}
return false; // 分析失败
}
private:
vector<Production> productions; // 产生式集合
Symbol startSymbol; // 开始符号
vector<Symbol> terminals; // 终结符集合
vector<Symbol> nonterminals; // 非终结符集合
map<Symbol, map<Symbol, Production>> parsingTable; // LL(1)分析表
};
// 测试代码
int main() {
// 定义文法符号
Symbol E{ NONTERMINAL, "E" };
Symbol T{ NONTERMINAL, "T" };
Symbol F{ NONTERMINAL, "F" };
Symbol plus{ TERMINAL, "+" };
Symbol minus{ TERMINAL, "-" };
Symbol multi{ TERMINAL, "*" };
Symbol div{ TERMINAL, "/" };
Symbol lparen{ TERMINAL, "(" };
Symbol rparen{ TERMINAL, ")" };
Symbol id{ TERMINAL, "id" };
Symbol num{ TERMINAL, "num" };
// 定义产生式
vector<Production> productions{
{ E, { T, plus, E } },
{ E, { T, minus, E } },
{ E, { T } },
{ T, { F, multi, T } },
{ T, { F, div, T } },
{ T, { F } },
{ F, { lparen, E, rparen } },
{ F, { id } },
{ F, { num } }
};
// 定义开始符号
Symbol startSymbol = E;
// 定义终结符集合和非终结符集合
vector<Symbol> terminals{ plus, minus, multi, div, lparen, rparen, id, num };
vector<Symbol> nonterminals{ E, T, F };
// 定义LL(1)分析表
map<Symbol, map<Symbol, Production>> parsingTable{
{ E, {
{ id, productions[0] },
{ num, productions[0] },
{ lparen, productions[0] },
{ plus, productions[0] },
{ minus, productions[1] },
{ multi, Production{ Symbol{ NONTERMINAL, "" }, {} } },
{ div, Production{ Symbol{ NONTERMINAL, "" }, {} } },
{ rparen, Production{ Symbol{ NONTERMINAL, "" }, {} } },
{ Symbol{ TERMINAL, "$" }, productions[0] }
} },
{ T, {
{ id, productions[3] },
{ num, productions[3] },
{ lparen, productions[3] },
{ plus, Production{ Symbol{ NONTERMINAL, "" }, {} } },
{ minus, Production{ Symbol{ NONTERMINAL, "" }, {} } },
{ multi, productions[3] },
{ div, productions[3] },
{ rparen, Production{ Symbol{ NONTERMINAL, "" }, {} } },
{ Symbol{ TERMINAL, "$" }, productions[3] }
} },
{ F, {
{ id, productions[7] },
{ num, productions[8] },
{ lparen, productions[6] },
{ plus, Production{ Symbol{ NONTERMINAL, "" }, {} } },
{ minus, Production{ Symbol{ NONTERMINAL, "" }, {} } },
{ multi, Production{ Symbol{ NONTERMINAL, "" }, {} } },
{ div, Production{ Symbol{ NONTERMINAL, "" }, {} } },
{ rparen, Production{ Symbol{ NONTERMINAL, "" }, {} } },
{ Symbol{ TERMINAL, "$" }, Production{ Symbol{ NONTERMINAL, "" }, {} } }
} }
};
// 创建LL(1)文法分析器
LL1Parser parser(productions, startSymbol, terminals, nonterminals, parsingTable);
// 测试分析函数
string input = "id+id*id$";
bool result = parser.parse(input);
if (result) {
cout << "分析成功" << endl;
}
else {
cout << "分析失败" << endl;
}
return 0;
}
```
阅读全文