|
3 | 3 | void LLNRec::initGrammar()
|
4 | 4 | {
|
5 | 5 | // 输入语法要求:一个产生式的右侧的每个元素用空格隔开
|
6 |
| - begin = "E"; |
7 |
| - grammar["E"] = vector<string>{ "T E'" }; |
8 |
| - grammar["E'"] = vector<string>{ "+ T E'", E }; |
9 |
| - grammar["T"] = vector<string>{ "F T'" }; |
10 |
| - grammar["T'"] = vector<string>{ "* F T'", E }; |
11 |
| - grammar["F"] = vector<string>{ "( E )", "id" }; |
| 6 | + //begin = "E"; |
| 7 | + //grammar["E"] = vector<string>{ "T E'" }; |
| 8 | + //grammar["E'"] = vector<string>{ "+ T E'", E }; |
| 9 | + //grammar["T"] = vector<string>{ "F T'" }; |
| 10 | + //grammar["T'"] = vector<string>{ "* F T'", E }; |
| 11 | + //grammar["F"] = vector<string>{ "( E )", "id" }; |
12 | 12 |
|
13 |
| - //begin = "<program>"; |
14 |
| - //grammar["<program>"] = vector<string>{ "{ <declaration_list> <statement_list> }" }; |
15 |
| - //grammar["<declaration_list>"] = vector<string>{ "<declaration_list_R> " }; |
16 |
| - //grammar["<declaration_list_R>"] = vector<string>{ " <declaration_stat> <declaration_list_R>", E }; |
17 |
| - //grammar["<declaration_stat>"] = vector<string>{"int ID ;"}; |
18 |
| - //grammar["<statement_list>"] = vector<string>{ "<statement_list_R> " }; |
19 |
| - //grammar["<statement_list_R>"] = vector<string>{ " <statement> <statement_list_R>", E }; |
20 |
| - //grammar["<statement>"] = vector<string>{" <if_stat> ", " <while_stat>", "<for_stat>", "<read_stat>", "<write_stat>", "<compound_stat>", "<expression_stat> "}; |
21 |
| - //grammar["<if_stat>"] = vector<string>{"if ( <expression> ) <statement> <else_part>"}; |
22 |
| - //grammar["<else_part>"] = vector<string>{"else <statement>", E}; |
23 |
| - //grammar["<while_stat>"] = vector<string>{"while ( <expression> ) <statement> "}; |
24 |
| - //grammar["<for_stat>"] = vector<string>{"for ( <expression> ; <expression> ; <expression> ) <statement> "}; |
25 |
| - //grammar["<write_stat>"] = vector<string>{"write <expression> ;"}; |
26 |
| - //grammar["<read_stat>"] = vector<string>{"read ID ;"}; |
27 |
| - //grammar["<compound_stat>"] = vector<string>{"{ <statement_list> }"}; |
28 |
| - //grammar["<expression_stat>"] = vector<string>{" <expression> ;", ";"}; |
29 |
| - //grammar["<expression>"] = vector<string>{"ID = <bool_expr>", "<bool_expr>"}; // 问题 |
30 |
| - //grammar["<bool_expr>"] = vector<string>{"<additive_expr> <bool_expr_right>"}; |
31 |
| - //grammar["<bool_expr_right>"] = vector<string>{"> <additive_expr>", "< <additive_expr>", |
32 |
| - // ">= <additive_expr>", "<= <additive_expr>", "== <additive_expr>", "!= <additive_expr>", E}; |
33 |
| - //grammar["<additive_expr>"] = vector<string>{ "<term> <additive_expr_right>"}; |
34 |
| - //grammar["<additive_expr_right>"] = vector<string>{"+ <term>", "- <term>", E}; |
35 |
| - //grammar["<term>"] = vector<string>{ "<factor> <term_right>" }; |
36 |
| - //grammar["<term_right>"] = vector<string>{"* <factor>", "/ <factor>", E}; |
37 |
| - //grammar["<factor>"] = vector<string>{"( <expression> )", "ID", "NUM"}; |
| 13 | + begin = "<program>"; |
| 14 | + grammar["<program>"] = vector<string>{ "{ <declaration_list> <statement_list> }" }; |
| 15 | + grammar["<declaration_list>"] = vector<string>{ "<declaration_list_R> " }; |
| 16 | + grammar["<declaration_list_R>"] = vector<string>{ " <declaration_stat> <declaration_list_R>", E }; |
| 17 | + grammar["<declaration_stat>"] = vector<string>{"int ID ;"}; |
| 18 | + grammar["<statement_list>"] = vector<string>{ "<statement_list_R> " }; |
| 19 | + grammar["<statement_list_R>"] = vector<string>{ " <statement> <statement_list_R>", E }; |
| 20 | + grammar["<statement>"] = vector<string>{" <if_stat> ", " <while_stat>", "<for_stat>", "<read_stat>", "<write_stat>", "<compound_stat>", "<expression_stat> "}; |
| 21 | + grammar["<if_stat>"] = vector<string>{"if ( <expression> ) <statement> <else_part>"}; |
| 22 | + grammar["<else_part>"] = vector<string>{"else <statement>", E}; |
| 23 | + grammar["<while_stat>"] = vector<string>{"while ( <expression> ) <statement> "}; |
| 24 | + grammar["<for_stat>"] = vector<string>{"for ( <expression> ; <expression> ; <expression> ) <statement> "}; |
| 25 | + grammar["<write_stat>"] = vector<string>{"write <expression> ;"}; |
| 26 | + grammar["<read_stat>"] = vector<string>{"read ID ;"}; |
| 27 | + grammar["<compound_stat>"] = vector<string>{"{ <statement_list> }"}; |
| 28 | + grammar["<expression_stat>"] = vector<string>{" <expression> ;", ";"}; |
| 29 | + grammar["<expression>"] = vector<string>{"ID = <bool_expr>", "<bool_expr>"}; // 问题 |
| 30 | + grammar["<bool_expr>"] = vector<string>{"<additive_expr> <bool_expr_right>"}; |
| 31 | + grammar["<bool_expr_right>"] = vector<string>{"> <additive_expr>", "< <additive_expr>", |
| 32 | + ">= <additive_expr>", "<= <additive_expr>", "== <additive_expr>", "!= <additive_expr>", E}; |
| 33 | + grammar["<additive_expr>"] = vector<string>{ "<term> <additive_expr_right>"}; |
| 34 | + grammar["<additive_expr_right>"] = vector<string>{"+ <term>", "- <term>", E}; |
| 35 | + grammar["<term>"] = vector<string>{ "<factor> <term_right>" }; |
| 36 | + grammar["<term_right>"] = vector<string>{"* <factor>", "/ <factor>", E}; |
| 37 | + grammar["<factor>"] = vector<string>{"( <expression> )", "ID", "NUM"}; |
38 | 38 |
|
39 | 39 | for (map<string, vector<string> >::iterator it = grammar.begin(); it != grammar.end(); it++) {
|
40 | 40 | first[it->first] = set<string>();
|
@@ -179,42 +179,42 @@ void LLNRec::buildTable()
|
179 | 179 |
|
180 | 180 | void LLNRec::showTable()
|
181 | 181 | {
|
182 |
| - //cout << "预测分析表" << endl; |
183 |
| - //vector<string> sign; |
184 |
| - //cout << setw(10) << ""; |
185 |
| - //for (auto s: inputSign) { |
186 |
| - // sign.push_back(s); |
187 |
| - // cout << setw(14) << left << s; |
188 |
| - //} |
189 |
| - //cout << endl; |
190 |
| - //for (it_msv it = grammar.begin(); it != grammar.end(); it ++) { |
191 |
| - // cout << setw(10) << left << it->first; |
192 |
| - // for (auto s: sign) { |
193 |
| - // pair<string, vector<string> > p = predictTable[it->first][s]; |
194 |
| - // stringstream ss; |
195 |
| - // if(p.first.size()) ss << p.first << "->" << p.second; |
196 |
| - // cout << setw(14) << left << ss.str(); |
197 |
| - // } |
198 |
| - // cout << endl; |
199 |
| - //} |
200 | 182 | cout << "预测分析表" << endl;
|
201 | 183 | vector<string> sign;
|
202 |
| - cout << "\\t"; |
203 |
| - for (auto s : inputSign) { |
| 184 | + cout << setw(10) << ""; |
| 185 | + for (auto s: inputSign) { |
204 | 186 | sign.push_back(s);
|
205 |
| - cout << s << "\\t"; |
| 187 | + cout << setw(14) << left << s; |
206 | 188 | }
|
207 | 189 | cout << endl;
|
208 | 190 | for (it_msv it = grammar.begin(); it != grammar.end(); it ++) {
|
209 |
| - cout << it->first << "\\t"; |
| 191 | + cout << setw(10) << left << it->first; |
210 | 192 | for (auto s: sign) {
|
211 | 193 | pair<string, vector<string> > p = predictTable[it->first][s];
|
212 | 194 | stringstream ss;
|
213 | 195 | if(p.first.size()) ss << p.first << "->" << p.second;
|
214 |
| - cout << ss.str() << "\\t"; |
| 196 | + cout << setw(14) << left << ss.str(); |
215 | 197 | }
|
216 | 198 | cout << endl;
|
217 | 199 | }
|
| 200 | + //cout << "预测分析表" << endl; |
| 201 | + //vector<string> sign; |
| 202 | + //cout << "\\t"; |
| 203 | + //for (auto s : inputSign) { |
| 204 | + // sign.push_back(s); |
| 205 | + // cout << s << "\\t"; |
| 206 | + //} |
| 207 | + //cout << endl; |
| 208 | + //for (it_msv it = grammar.begin(); it != grammar.end(); it ++) { |
| 209 | + // cout << it->first << "\\t"; |
| 210 | + // for (auto s: sign) { |
| 211 | + // pair<string, vector<string> > p = predictTable[it->first][s]; |
| 212 | + // stringstream ss; |
| 213 | + // if(p.first.size()) ss << p.first << "->" << p.second; |
| 214 | + // cout << ss.str() << "\\t"; |
| 215 | + // } |
| 216 | + // cout << endl; |
| 217 | + //} |
218 | 218 | }
|
219 | 219 |
|
220 | 220 | void LLNRec::initStk() {
|
|
0 commit comments