-
Notifications
You must be signed in to change notification settings - Fork 0
/
ll1_parser.cpp
212 lines (189 loc) · 7.59 KB
/
ll1_parser.cpp
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
#include "ll1_parser.h"
#include <stack>
enum error_type {
MISSING_SYMBOL,
INVALID_TOKEN,
ERROR_PRODUCTION,
EXCESS_SYMBOL,
REACHED_EOF
};
std::string production_to_string(std::string lhs, std::vector<std::string> rhs) {
std::string prod = lhs + " ->";
for (std::string sym : rhs) {
prod += " " + sym;
}
return prod;
}
std::string get_message(std::string cur_symbol, std::string cur_token, error_type error) {
switch (error) {
case MISSING_SYMBOL:
return "Error: Missing \"" + cur_symbol + "\", Inserted.";
break;
case INVALID_TOKEN:
return "Error: Invalid token or identifier \"" + cur_token + "\"";
break;
case ERROR_PRODUCTION:
return "Error production near: \"" + cur_token + "\"";
break;
case EXCESS_SYMBOL:
return "Error excess symbol near: \"" + cur_token + "\", Expected EOF";
break;
case REACHED_EOF:
return "Error: Expected \"" + cur_symbol + "\" , but end of file was found.";
break;
default:
return "";
break;
}
}
void panic(parsetable &parsetable, std::vector<std::string> &stack,
lexer &lex, lexer::token &cur_token, std::string &prev_production,
std::string cur_symbol, error_type err_type, std::istream &input_stream,
bool &prev_is_production, std::vector<error> &errors) {
errors.push_back(error(cur_token.get_line(),
cur_token.get_col(),
get_message(cur_symbol, cur_token.get_str(), err_type)));
prev_production = get_message(cur_symbol, cur_token.get_str(), err_type);
prev_is_production = true;
if (err_type == REACHED_EOF) {
stack.push_back(EOI);
} else if (err_type == MISSING_SYMBOL) {
stack.pop_back();
} else if (err_type == ERROR_PRODUCTION) {
parsetable::entry entry;
do {
cur_token = lex.next_token(input_stream);
std::string cur_token_class = cur_token.get_class() == EOF_MARK ? EOI : cur_token.get_class();
entry = parsetable.get_entry(cur_symbol, cur_token_class);
} while (entry.state = parsetable::entry::ERROR);
} else if (err_type == EXCESS_SYMBOL) {
cur_token = lexer::token("", EOF_MARK);
} else {
cur_token = lex.next_token(input_stream);
}
}
leftmost_derivation parse::parse_ll1(parsetable &parsetable, machine &mac, std::istream &input_stream) {
std::cout << parsetable;
std::vector<std::string> stack;
lexer lex(mac);
stack.push_back(EOI);
std::string starting_symbol = parsetable.get_starting_symbol_key();
stack.push_back(starting_symbol);
std::vector<std::vector<std::string> > derivations;
std::vector<std::string> productions;
std::vector<error> errors;
bool substitute = false;
bool inserted_missing_sym = false;
std::string substitute_str = "";
bool prev_is_production = false;
std::string prev_production = "";
int step = 0;
int cur_symbol_idx = 0;
lexer::token cur_token = lex.next_token(input_stream);
while (true) {
std::string cur_symbol = stack.back();
std::string cur_token_class = cur_token.get_class() == EOF_MARK ? EOI : cur_token.get_class();
if (cur_token_class == ERROR_CLASS) {
errors.push_back(error(cur_token.get_line(),
cur_token.get_col(),
get_message(cur_symbol, cur_token.get_str(), INVALID_TOKEN)));
cur_token = lex.next_token(input_stream);
continue;
}
derivations.push_back(std::vector<std::string> ());
productions.push_back("");
if (step > 0) {
for (int i = 0 ; i < cur_symbol_idx ; i++) {
std::string symbol = derivations[step - 1][i];
if (!parsetable.is_nonterm(symbol))
derivations[step].push_back(symbol);
else
break;
}
}
if (prev_is_production) {
productions[step] = std::string(prev_production);
prev_is_production = false;
}
if (substitute) {
substitute = false;
if (inserted_missing_sym) {
inserted_missing_sym = false;
productions[step] = "Inserted Missing Symbol: " + substitute_str;
} else {
productions[step] = "Substitute: "
+ derivations[step][cur_symbol_idx - 1] + " = " + substitute_str;
}
derivations[step][cur_symbol_idx - 1] = substitute_str;
}
for (auto it = stack.end() - 1; it > stack.begin() ; it--) {
if (*it != EOI)
derivations[step].push_back(*it);
}
if (cur_symbol == EOI && cur_token_class == EOI) {
break;
}
step++;
if (!parsetable.is_nonterm(cur_symbol)) {
if (cur_symbol == cur_token_class) {
stack.pop_back();
substitute = true;
cur_symbol_idx++;
substitute_str = cur_token.get_str();
cur_token = lex.next_token(input_stream);
} else {
error_type err_type;
if (cur_symbol == EOI) {
err_type = EXCESS_SYMBOL;
} else if (cur_token_class == EOI) {
err_type = REACHED_EOF;
} else {
err_type = MISSING_SYMBOL;
substitute = true;
substitute_str = "'" + cur_symbol + "'";
inserted_missing_sym = true;
cur_symbol_idx++;
}
panic(parsetable, stack, lex, cur_token, prev_production,
cur_symbol, err_type, input_stream, prev_is_production, errors);
}
} else {
parsetable::entry entry = parsetable.get_entry(cur_symbol, cur_token_class);
std::vector<cfg::symbol *> symbols;
switch (entry.state) {
case parsetable::entry::PROD:
stack.pop_back();
prev_production = production_to_string(cur_symbol, entry.productions);
prev_is_production = true;
if (entry.productions.size() == 1 && entry.productions[0] == EPS_STR)
break;
for (int i = entry.productions.size() - 1 ; i >= 0 ; i--) {
stack.push_back(entry.productions[i]);
}
break;
case parsetable::entry::SYNC:
prev_production = "SYNC";
prev_is_production = true;
stack.pop_back();
break;
case parsetable::entry::ERROR:
if (cur_token_class != EOI) {
panic(parsetable, stack, lex, cur_token, prev_production,
cur_symbol, ERROR_PRODUCTION, input_stream, prev_is_production, errors);
} else {
panic(parsetable, stack, lex, cur_token, prev_production,
cur_symbol, REACHED_EOF, input_stream, prev_is_production, errors);
}
break;
default:
break;
}
}
}
std::vector<sentential_expression> expressions;
for (int i = 0 ; i < derivations.size() ; i++) {
expressions.push_back(sentential_expression(derivations[i], productions[i]));
}
leftmost_derivation lm_derivation(expressions, errors);
return lm_derivation;
}