Skip to content

Commit bc24722

Browse files
committed
update non recursion LL
1 parent 0117c2d commit bc24722

File tree

3 files changed

+58
-58
lines changed

3 files changed

+58
-58
lines changed

Compiler/LLNRec.cpp

+54-54
Original file line numberDiff line numberDiff line change
@@ -3,38 +3,38 @@
33
void LLNRec::initGrammar()
44
{
55
// 输入语法要求:一个产生式的右侧的每个元素用空格隔开
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" };
1212

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"};
3838

3939
for (map<string, vector<string> >::iterator it = grammar.begin(); it != grammar.end(); it++) {
4040
first[it->first] = set<string>();
@@ -179,42 +179,42 @@ void LLNRec::buildTable()
179179

180180
void LLNRec::showTable()
181181
{
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-
//}
200182
cout << "预测分析表" << endl;
201183
vector<string> sign;
202-
cout << "\\t";
203-
for (auto s : inputSign) {
184+
cout << setw(10) << "";
185+
for (auto s: inputSign) {
204186
sign.push_back(s);
205-
cout << s << "\\t";
187+
cout << setw(14) << left << s;
206188
}
207189
cout << endl;
208190
for (it_msv it = grammar.begin(); it != grammar.end(); it ++) {
209-
cout << it->first << "\\t";
191+
cout << setw(10) << left << it->first;
210192
for (auto s: sign) {
211193
pair<string, vector<string> > p = predictTable[it->first][s];
212194
stringstream ss;
213195
if(p.first.size()) ss << p.first << "->" << p.second;
214-
cout << ss.str() << "\\t";
196+
cout << setw(14) << left << ss.str();
215197
}
216198
cout << endl;
217199
}
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+
//}
218218
}
219219

220220
void LLNRec::initStk() {

Compiler/main.cpp

+3-3
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
#define INPUTPATH "E:\\Progress\\Compiler\\test.T"
1010
#define OUTPUTPATH "E:\\Progress\\Compiler\\out.T"
1111

12-
#define LLNRC_TEST1
12+
#define nLLNRC_TEST1
1313

1414
vector<Token> getTokens(string testStr) {
1515
vector<Token> ret;
@@ -42,8 +42,8 @@ int main() {
4242
LLNRec parser;
4343
parser.showTable();
4444
//string test = "id + id * id $";
45-
string test = "id + id * ( id + id ) $";
46-
//string test = "( id + id ) * id * id + id $";
45+
//string test = "id + id * ( id + id ) $";
46+
string test = "( id + id ) * id * id + id $";
4747
vector<Token> testTokens = getTokens(test);
4848
parser.analyze(testTokens);
4949
#else

Compiler/util.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ using namespace std;
2525
#define nDEBUG_LL
2626
#define nDEBUG_LLNRC
2727
#define nOUTPUT_PREDICT_TABLE
28-
#define OUTPUT_PARSE_TREE
28+
#define nOUTPUT_PARSE_TREE
2929

3030
// ´Ê·¨µ¥Ôª
3131
struct Token {

0 commit comments

Comments
 (0)