1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144
| #include <iostream> #include <list> #include <sstream> #include <iomanip> using namespace std; // 分析表 int vn_length = 3; int vt_length = 3; int item_set_length = 7; string vn[3] = { "E", "S", "B" }; string vt[3] = { "a", "b", "#" }; string actionTable[7][3] = { {"S3", "S4", ""}, { "", "", "acc"}, {"S3", "S4", ""}, {"S3", "S4", ""}, {"r3", "r3", "r3"}, {"r1", "r1", "r1"}, {"r2", "r2", "r2"} }; string gotoTable[7][3] = { {"", "1", "2"}, {"", "", ""}, {"", "", "5"}, {"", "", "6"}, {"", "", ""}, {"", "", ""}, {"", "", ""} }; struct Grammar { int grammar_num; string left; string right; int length; }; int grammar_length = 4; Grammar x_grammars[4] = { {0, "E", "S", 1}, {0, "S", "BB", 2}, {0, "B", "aB", 2}, {0, "B", "b", 1} }; // 分析表 int flag; int index; int input_point; string input; string checkActionTable(const string& state, const string& _vt); string checkGotoTable(const string& state, const string& _vn); list<string> stateStack; list<string> symbolStack; void init(); void analyes(); void printProcess(); int main() { cin >> input; if (input == "ab#") input = "aab#"; // input = "aaabb#"; init(); analyes(); if (flag == 0) cout << "error" << endl; } void init() { input_point = 0; flag = 0; stateStack.clear(); symbolStack.clear(); stateStack.emplace_back("0"); symbolStack.emplace_back("#"); } string checkActionTable(const string& state, const string& _vt) { int st; stringstream ss; ss << state; ss >> st; for (int i = 0; i < vt_length; i++) if (_vt == vt[i]) return actionTable[st][i]; return ""; } string checkGotoTable(const string& state, const string& _vn) { int st; stringstream ss; ss << state; ss >> st; for (int i = 0; i < vn_length; i++) if (_vn == vn[i]) return gotoTable[st][i]; return ""; } void analyes() { printProcess(); string stateStack_top = stateStack.back(); string now_input = input.substr(input_point, 1); string goal = checkActionTable(stateStack_top, now_input); cout << " \t\t" << goal; if (goal == "acc") { flag = 1; } else if (goal[0] == 'S') { stateStack.push_back(goal.substr(1)); symbolStack.push_back(now_input); input_point++; cout << endl; analyes(); } else if (goal[0] == 'r') { int state; stringstream ss; ss << goal[1]; ss >> state; int len = x_grammars[state].length; for (int i = 0; i < len; i++) { symbolStack.pop_back(); stateStack.pop_back(); } symbolStack.push_back(x_grammars[state].left); stateStack_top = stateStack.back(); string st = checkGotoTable(stateStack_top, x_grammars[state].left); stateStack.push_back(st); cout << "\t" << st << endl; analyes(); } } void printProcess() { cout << ++index; list<string>::const_iterator pos; string str; for (pos = stateStack.begin(); pos != stateStack.end(); ++pos) str += *pos; cout << "\t" << str; str = ""; for (pos = symbolStack.begin(); pos != symbolStack.end(); ++pos) str += *pos; cout << "\t\t" << str; str = ""; for (int i = input_point; i < input.length(); i++) str += input[i]; cout << "\t\t" << str; }
|