-
Notifications
You must be signed in to change notification settings - Fork 0
/
syntax.h
367 lines (337 loc) · 14.1 KB
/
syntax.h
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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
#ifndef __SYNTAX_H__
#define __SYNTAX_H__
#include <iomanip>
#include <iostream>
#include <map>
#include <set>
#include <stack>
#include <string>
#include <vector>
class Syntax {
public:
Syntax(const std::vector<std::pair<std::string, std::vector<std::string>>>& prods,
const std::set<std::string>& terms,
const std::set<std::string>& nonTerms, const std::string& s)
: productions(prods), terminals(terms), nonTerminals(nonTerms), start(s) {
constructFirstSet();
constructFollowSet();
constructSelectSet();
constructParseTable();
}
void displayFirstSets() const {
std::cout << "First Sets:" << std::endl;
for (const auto& nonTerminal : nonTerminals) {
std::cout << nonTerminal << ": { ";
for (const auto& symbol : firstSet.at(nonTerminal)) {
std::cout << symbol << " ";
}
std::cout << "}" << std::endl;
}
}
void displayFollowSets() const {
std::cout << "\nFollow Sets:" << std::endl;
for (const auto& nonTerminal : nonTerminals) {
std::cout << nonTerminal << ": { ";
for (const auto& symbol : followSet.at(nonTerminal)) {
std::cout << symbol << " ";
}
std::cout << "}" << std::endl;
}
}
void displaySelectSets() const {
std::cout << "\nSelect Sets:" << std::endl;
for (const auto& entry : selectSet) {
std::cout << "Select[" << entry.first.first << "->";
for (int i = 0; i < entry.first.second.size(); ++i) {
std::cout << entry.first.second[i];
}
std::cout << "]={";
int idx = 0;
for (const auto& symbol : entry.second) {
if (idx != entry.second.size() - 1) {
std::cout << symbol << ",";
} else {
std::cout << symbol;
}
idx++;
}
std::cout << "}" << std::endl;
}
}
void displayParseTable() const {
std::cout << "\nParse Table:" << std::endl;
// Display table header
std::cout << std::setw(15) << " ";
for (const auto& terminal : terminals) {
std::cout << std::setw(15) << terminal;
}
std::cout << std::setw(15) << "#" << std::endl;
// Display table rows
for (const auto& nonTerminal : nonTerminals) {
std::cout << std::setw(15) << nonTerminal;
for (const auto& terminal : terminals) {
if (!parseTable.at({nonTerminal, terminal}).empty()) {
std::string str;
for (auto it : parseTable.at({nonTerminal, terminal}))
str += it;
std::cout << std::setw(15) << str;
} else {
std::cout << std::setw(15) << " ";
}
}
// Display # column
if (!parseTable.at({nonTerminal, "#"}).empty()) {
std::string str;
for (auto it : parseTable.at({nonTerminal, "#"}))
str += it;
std::cout << std::setw(15) << str;
} else {
std::cout << std::setw(15) << " ";
}
std::cout << std::endl;
}
}
// 下推自动机
bool parse(std::vector<std::pair<std::string, std::string>>& tokens) const {
std::stack<std::string> stk;
stk.push("#"); // 输入结束符
stk.push(start); // 开始符号
tokens.push_back({"#", "#"}); // 在输入流的结尾添加结束符,便于对比
size_t index = 0;
while (!stk.empty()) {
std::string top = stk.top();
std::string token = tokens[index].first;
auto tmp = stk;
while (!tmp.empty()) {
std::cout << tmp.top() << " ";
tmp.pop();
}
std::cout << std::endl;
if (terminals.find(top) != terminals.end() || top == "#") {
if (top == token) {
// 匹配终结符
stk.pop();
index++;
} else {
std::cout << "Syntax error: unexpected token " << tokens[index].second << ", expected " << top << std::endl;
return false;
}
} else if (nonTerminals.find(top) != nonTerminals.end()) {
if (!parseTable.at({top, token}).empty()) {
// 使用对应的产生式替换栈顶的非终结符
stk.pop();
const auto& production = parseTable.at({top, token});
if (!(production.size() == 1 && production[0] == "@")) { // 如果不是产生式@,则逆序加入栈中
for (auto it = production.rbegin(); it != production.rend(); ++it) {
stk.push(*it);
}
}
} else {
std::cout << "Syntax error: no production rule for (" << top << ", " << tokens[index].second << ")" << std::endl;
// 尝试同步消费输入记号或跳过输入查看同步点
bool foundSync = false;
while (!stk.empty() && (!parseTable.at({stk.top(), token}).empty() && parseTable.at({stk.top(), token})[0] == "synch")) {
stk.pop();
foundSync = true;
}
if (!foundSync) {
return false;
}
}
} else {
std::cout << "Syntax error: invalid " << tokens[index].second << std::endl;
return false;
}
}
if (stk.empty() && tokens[index - 1].first == "#") {
std::cout << "Parsing successful!" << std::endl;
return true; // 成功解析
} else {
std::cout << "Syntax error: unexpected end of input" << std::endl;
return false;
}
}
private:
std::vector<std::pair<std::string, std::vector<std::string>>>
productions;
std::set<std::string> terminals;
std::set<std::string> nonTerminals;
std::string start;
std::map<std::string, std::set<std::string>> firstSet;
std::map<std::string, std::set<std::string>> followSet;
std::map<std::pair<std::string, std::vector<std::string>>, std::set<std::string>> selectSet;
std::map<std::pair<std::string, std::string>, std::vector<std::string>> parseTable; // 预测分析表
// 构建first集
void constructFirstSet() {
for (const auto& nonTerminal : nonTerminals) {
firstSet[nonTerminal] = {};
}
bool changed = true;
while (changed) {
changed = false;
for (const auto& prod : productions) {
const std::string& nonTerminal = prod.first;
const std::vector<std::string>& rhs = prod.second;
for (const auto& symbol : rhs) {
if (terminals.find(symbol) != terminals.end() || symbol == "@") {
if (firstSet[nonTerminal].find(symbol) == firstSet[nonTerminal].end()) {
firstSet[nonTerminal].insert(symbol);
changed = true;
}
break;
} else {
bool hasEpsilon = false;
for (const auto& firstSymbol : firstSet[symbol]) {
if (firstSymbol != "@") {
if (firstSet[nonTerminal].find(firstSymbol) == firstSet[nonTerminal].end()) {
firstSet[nonTerminal].insert(firstSymbol);
changed = true;
}
} else {
hasEpsilon = true;
}
}
if (!hasEpsilon) {
break;
}
}
}
}
}
}
// 构建follow集
void constructFollowSet() {
for (const auto& nonTerminal : nonTerminals) {
followSet[nonTerminal] = {};
}
followSet[start].insert("#"); // #表示输入结束
bool changed = true;
while (changed) {
changed = false;
for (const auto& prod : productions) {
const std::string& nonTerminal = prod.first;
const std::vector<std::string>& rhs = prod.second;
for (size_t i = 0; i < rhs.size(); ++i) {
if (nonTerminals.find(rhs[i]) != nonTerminals.end()) {
if (i < rhs.size() - 1) {
const std::string& nextSymbol = rhs[i + 1];
if (terminals.find(nextSymbol) != terminals.end()) {
if (followSet[rhs[i]].find(nextSymbol) == followSet[rhs[i]].end()) {
followSet[rhs[i]].insert(nextSymbol);
changed = true;
}
} else {
bool hasEpsilon = false;
for (const auto& firstSymbol : firstSet[nextSymbol]) {
if (firstSymbol != "@") {
if (followSet[rhs[i]].find(firstSymbol) == followSet[rhs[i]].end()) {
followSet[rhs[i]].insert(firstSymbol);
changed = true;
}
} else {
hasEpsilon = true;
}
}
if (hasEpsilon) {
for (const auto& followSymbol : followSet[nonTerminal]) {
if (followSet[rhs[i]].find(followSymbol) == followSet[rhs[i]].end()) {
followSet[rhs[i]].insert(followSymbol);
changed = true;
}
}
}
}
} else {
for (const auto& followSymbol : followSet[nonTerminal]) {
if (followSet[rhs[i]].find(followSymbol) == followSet[rhs[i]].end()) {
followSet[rhs[i]].insert(followSymbol);
changed = true;
}
}
}
}
}
}
}
}
// 构建select集
void constructSelectSet() {
for (const auto& prod : productions) {
// A->a
// 如果a=>@,则Select(A->a) = First(a)
const std::string& nonTerminal = prod.first;
const std::vector<std::string>& rhs = prod.second;
// 1. 获取产生式A->a First(a)
bool hasEpsilon = true;
std::set<std::string> firstOfRHS;
for (const auto& symbol : rhs) {
if (terminals.find(symbol) != terminals.end()) { // 终结符
firstOfRHS.insert(symbol);
hasEpsilon = false;
break;
} else if (symbol == "@") {
firstOfRHS.insert("@");
} else { // 非终结符
for (const auto& firstSymbol : firstSet[symbol]) {
if (firstSymbol != "@") {
firstOfRHS.insert(firstSymbol);
}
}
if (firstSet[symbol].find("@") == firstSet[symbol].end()) {
hasEpsilon = false;
break;
}
}
}
if (hasEpsilon) {
firstOfRHS.insert("@");
}
// 2. 如果 A->a, a !-> @
if (firstOfRHS.find("@") == firstOfRHS.end()) {
// Select(A->a) = First(a)
selectSet[prod] = firstOfRHS;
} else {
// Select(A->a) = {First(a)-{@}}+{Follow(A)}
selectSet[prod] = firstOfRHS;
selectSet[prod].erase("@");
for (auto it : followSet[nonTerminal]) {
selectSet[prod].insert(it);
}
}
}
}
// 构建预测分析表
void constructParseTable() {
// 1. 初始化预测分析表
std::set<std::string> tmp = terminals;
tmp.insert("#");
for (auto nonTerminal : nonTerminals) {
for (auto terminal : tmp) {
parseTable[{nonTerminal, terminal}] = std::vector<std::string>();
}
}
// 2. 构建预测分析表
// Select[E->TE']={(,i}
for (auto entry : selectSet) {
std::string nonTerminal = entry.first.first;
for (auto terminal : entry.second) {
// table[E,i] = E->TE`
if (parseTable[{nonTerminal, terminal}].size() != 0) {
std::cout << "不是LL(1)文法" << std::endl;
exit(1);
}
parseTable[{nonTerminal, terminal}] = entry.first.second;
}
}
// 添加同步词法单元到预测分析表
for (const auto& nonTerminal : nonTerminals) {
// 把表中的非终结符的follow集合中的元素并且非空的设置成synch
for (const auto& terminal : followSet[nonTerminal]) {
if (parseTable[{nonTerminal, terminal}].size() == 0) {
parseTable[{nonTerminal, terminal}] = {"synch"};
}
}
}
}
};
#endif // __SYNTAX_H__