运行截图
代码
#include<iostream>
#include<map>
#include <string>
#include <stack>
#include<queue>
#include<algorithm>
using namespace std;
/*multimap<string, multimap<char, string>> table; //分析表
multimap<string, multimap<char, string>>::iterator muliter;//外部map迭代器
multimap<char, string>::iterator iter; //内部map迭代器*/
map<string, map<char, string>> table; //分析表
map<string, map<char, string>>::iterator muliter;//外部map迭代器
map<char, string>::iterator iter; //内部map迭代器
string vt = "i+*()"; //非终结符集
stack<string> a; //分析栈
queue<char> b; //输入串
string x;//栈顶符号
string css;//对应产生式
string symbol;//逆序入栈
void init() { //分析表初始化 /*multimap<char, string> inside;inside.insert(make_pair('i', "TE'"));inside.insert(make_pair('(', "TE'"));table.insert(make_pair("E", inside));inside.clear();inside.insert(make_pair('+', "+TE'"));inside.insert(make_pair(')', "0"));inside.insert(make_pair('#', "0"));table.insert(make_pair("E'", inside));inside.clear();inside.insert(make_pair('i', "FT'"));inside.insert(make_pair('(', "FT'"));table.insert(make_pair("T", inside));inside.clear();inside.insert(make_pair('+', "0"));inside.insert(make_pair('*', "*FT'"));inside.insert(make_pair(')', "0"));inside.insert(make_pair('#', "0"));table.insert(make_pair("T'", inside));inside.clear();inside.insert(make_pair('i', "i"));inside.insert(make_pair('(', "(E)"));table.insert(make_pair("F", inside));inside.clear();*/table["E"]['i'] = "TE'";table["E"]['('] = "TE'";table["E'"]['+'] = "+TE'";table["E'"][')'] = "0";table["E'"]['#'] = "0";table["T"]['i'] = "FT'";table["T"]['('] = "FT'";table["T'"]['+'] = "0";table["T'"]['*'] = "*FT'";table["T'"][')'] = "0";table["T'"]['#'] = "0";table["F"]['i'] = "i";table["F"]['('] = "(E)";
}void analyze() {a.push("#");a.push("E");char tar = b.front(); //要匹配的符号bool flag = true;muliter = table.begin();iter = muliter->second.begin();while (flag) {string x = a.top();// 栈顶符号if (vt.find(x[0])!=vt.npos) {//栈顶符号是否属于终结符集合if (x[0] == tar) {//是否匹配cout << "栈顶符号=" << a.top() << " 匹配字符=" << tar << " 匹配成功" << endl;b.pop();a.pop();tar = b.front();}else {cout << "栈顶符号不属于终结符集合" << endl;} }else {if (x == "#") {if (x[0] == tar) { flag = false;}else {cout << "x!=#" << endl;}}else {if ((muliter=table.find(a.top())) != table.end() && (iter=muliter->second.find(tar)) != muliter->second.end()) {//是否有对应产生式string css = iter->second;cout << "栈顶符号=" << a.top() << " 匹配字符=" << tar << " 对应产生式=" << a.top() << "->" << css << endl;if (css == "0") {// 0代表空a.pop();continue;}a.pop();while (!css.empty()) { //将产生式逆序入栈 //*********此处可应用词法分析器*********if (css[css.size() - 1] == '\'') {string symbol(css, css.size() - 2);a.push(symbol);css.erase(css.size() - 2, 2);}else {string symbol(css, css.size() - 1);a.push(symbol);css.erase(css.size() - 1, 1);}}}else {cout << "没有对应产生式" << endl;break;}}}}if (flag == true) { //出现错误break跳出时,说明语法错误cout << "语法错误" << endl;}else { cout << "语法正确" << endl;}
}
int main() {//初始化init();//输出分析表for (muliter = table.begin(); muliter != table.end(); muliter++) {for (iter = muliter->second.begin(); iter != muliter->second.end(); iter++)cout << "(" << muliter->first << ", " << iter->first << ") -> (" << iter->second << ")" << endl;}//输入所需分析串并入队cout << "请输入所需分析字符串:";while (1) {char c;cin >> c;b.push(c);if (c == '#')break;}//进行分析analyze();//输出串return 0;
}