-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
291 lines (243 loc) · 7.6 KB
/
main.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
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
#include <stdio.h>
#include <iostream>
#include <fstream>
#include <set>
#include "parse_table_generator.hpp"
const static std::vector<std::string> G1 {
{"B -> C"},
{"C -> D"},
{"C -> E"},
{"D -> '{{' F '}}'"},
{"D -> '{{' V '}}'"},
{"D -> '{{' F '-}'"},
{"D -> '{{' V '-}'"},
{"E -> '{{:' F '}}'"},
{"Y -> 'ID' '.' Y"},
{"Y -> 'ID'"},
{"H -> 'ID' '(' I ')'"},
{"I -> J"},
{"I -> ~"},
{"J -> J ',' K"},
{"J -> K"},
{"K -> Y '=' K"},
{"K -> M"},
{"M -> M 'LOGIC_OP' U"},
{"M -> U"},
{"U -> U 'REL_OP' N"},
{"U -> N"},
{"N -> N '+' O"},
{"N -> N '-' O"},
{"N -> O"},
{"O -> O 'MULT_OP' L"},
{"O -> L"},
{"L -> '!' L"},
{"L -> '-' L"},
{"L -> P"},
{"P -> 'STRING'"},
{"P -> 'NUM'"},
{"P -> 'BOOL'"},
{"P -> Y"},
{"P -> '(' K ')'"},
{"Q -> W S T 'END'"},
{"W -> 'IF' '(' K ')' V"},
{"S -> X"},
{"S -> ~"},
{"X -> X 'ELSE_IF' '(' K ')' V"},
{"X -> 'ELSE_IF' '(' K ')' V"},
{"T -> 'ELSE' V"},
{"T -> ~"},
{"R -> 'FOR' '(' 'ID' 'IN' 'STRING' ')' V 'END'"},
{"R -> 'FOR' '(' 'ID' 'IN' Y ')' V 'END'"},
{"R -> 'FOR' '(' 'ID' 'IN' H ')' V 'END'"},
{"F -> K"},
{"F -> H"},
{"F -> Q"},
{"F -> R"},
{"V -> G"},
{"V -> ~"},
{"G -> G F ';'"},
{"G -> F ';'"},
};
const static std::vector<std::string> G2 {
{"program -> content"},
{"content -> content 'PASSTHROUGH'"},
{"content -> 'PASSTHROUGH'"},
{"content -> content blocks"},
{"content -> blocks"},
{"blocks -> block"},
{"blocks -> print_block"},
{"blocks -> if_statement_block"},
{"blocks -> for_block"},
{"block -> '{{' statement '}}'"},
{"block -> '{{' statement '-}'"},
{"print_block -> '{{:' statement '}}'"},
{"print_block -> '{{:' statement '-}'"},
{"if_statement_block -> '{{if' expression '}}' content 'END'"},
{"if_statement_block -> '{{if' expression '}}' content else_if_list 'END'"},
{"if_statement_block -> '{{if' expression '}}' content '{{else}}' content 'END'"},
{"if_statement_block -> '{{if' expression '}}' content else_if_list '{{else}}' content 'END'"},
{"else_if_list -> else_if_list '{{else_if' expression '}}' content"},
{"else_if_list -> '{{else_if' expression '}}' content"},
{"for_block -> '{{for' 'ID' 'in' 'STRING' '}}' content 'END'"},
{"for_block -> '{{for' 'ID' 'in' var_name '}}' content 'END'"},
{"for_block -> '{{for' 'ID' 'in' func_call '}}' content 'END'"},
{"statement -> expression"},
{"statement -> func_call"},
{"var_name -> var_name '.' 'ID'"},
{"var_name -> 'ID'"},
{"func_call -> 'ID' '(' args ')'"},
{"args -> arg_list"},
{"args -> ~"},
{"arg_list -> arg_list ',' expression"},
{"arg_list -> expression"},
{"expression -> var_name '=' expression"},
{"expression -> logic_expression"},
{"logic_expression -> logic_expression 'LOGIC_OP' rel_expression"},
{"logic_expression -> rel_expression"},
{"rel_expression -> rel_expression 'REL_OP' add_expression"},
{"rel_expression -> add_expression"},
{"add_expression -> add_expression '+' mult_expression"},
{"add_expression -> add_expression '-' mult_expression"},
{"add_expression -> mult_expression"},
{"mult_expression -> mult_expression 'MULT_OP' unary_expression"},
{"mult_expression -> unary_expression"},
{"unary_expression -> '!' unary_expression"},
{"unary_expression -> '-' unary_expression"},
{"unary_expression -> term_expression"},
{"term_expression -> 'STRING'"},
{"term_expression -> 'NUM'"},
{"term_expression -> 'BOOL'"},
{"term_expression -> var_name"},
{"term_expression -> '(' expression ')'"},
};
const static std::vector<std::string> G3 {
{"S -> S ';' A"},
{"S -> A"},
{"A -> E"},
{"A -> 'id' ':=' E"},
{"E -> E '+' 'id'"},
{"E -> 'id'"},
{"E -> ~"},
};
void print_first_sets(std::unordered_map<std::string, std::unordered_set<std::string>> first_sets) {
for(const auto& a : first_sets) {
std::cout << a.first << " : " << "[";
std::string set_str = "";
std::vector<std::string> first_set(a.second.begin(), a.second.end());
sort(first_set.begin(), first_set.end());
for(auto it = first_set.begin(); it != first_set.end(); ++it) {
set_str += (*it + ", ");
}
set_str = set_str.substr(0, set_str.size() - 2);
std::cout << set_str << "]\n";
}
std::cout << "\n";
}
void print_item_sets(const std::set<std::set<LR1Item, LR1Comparator>, LR1SetComparator>& item_sets) {
std::cout << "Item Sets: \n";
int set_num = 0;
for(const auto& item_set : item_sets) {
std::cout << " Set " << set_num << ":\n";
for(const auto& item : item_set) {
std::cout << " " << item.to_string() << "\n";
}
std::cout << "\n";
set_num++;
}
std::cout << "\n\n";
}
void print_parse_table(const std::vector<std::vector<std::string>>& parse_table, const std::vector<std::string>& symbols) {
// print symbols
printf("|%5s%5s", "state", "");
for(const auto& symbol : symbols) {
printf("|%5s%5s", symbol.c_str(), "");
}
printf("|\n");
for(int i = 0; i < parse_table.size(); ++i) {
printf("|%5d%5s", i, "");
for(const auto& action : parse_table[i]) {
printf("|%5s%5s", action.c_str(), "");
}
printf("|\n");
}
}
// Prints the parse table where each row is like
// {"si", "", "sj", "ri", "", "", "n"}
void print_go_parse_table(const std::vector<std::vector<std::string>>& parse_table, const std::vector<std::string>& symbols) {
printf("{");
for(int i = 0; i < symbols.size(); ++i) {
const auto& symbol = symbols[i];
printf("\"%s\"", symbol.c_str());
if(i < symbols.size() - 1) {
printf(", ");
}
}
printf("}\n");
for(int i = 0; i < parse_table.size(); ++i) {
printf("{");
int row_size = parse_table[i].size();
for(int j = 0; j < row_size; ++j) {
const std::string& action = parse_table[i][j];
printf("\"%s\"", action.c_str());
if(j < row_size - 1) {
printf(", ");
}
}
printf("},\n");
}
}
int write_table(const std::string out_path, const std::vector<std::vector<std::string>>& parse_table, const std::vector<std::string>& symbols) {
std::ofstream out_stream(out_path);
if(!out_stream) {
std::cerr << "failed to open output stream " << out_path << ": " << strerror(errno) << "\n";
return 1;
}
// write the symbol header
for(int i = 0; i < symbols.size(); ++i) {
std::string symbol = symbols[i];
if(symbol.size() > 0 && symbol[0] != '\'' && symbol[symbol.size()-1] != '\'') {
symbol = ('\'' + symbol + '\'');
}
out_stream << symbol;
if(i < symbols.size() - 1) {
out_stream << ",";
}
}
out_stream << "\n";
for(int i = 0; i < parse_table.size(); ++i) {
int row_size = parse_table[i].size();
for(int j = 0; j < row_size; ++j) {
const std::string& action = parse_table[i][j];
out_stream << action;
if(j < row_size - 1) {
out_stream << ", ";
}
}
out_stream << "\n";
}
out_stream.close();
return 0;
}
void print_usage() {
std::cout << "usage: set_generator <path/to/table/output>\n";
}
int main(int argc, char **argv) {
if(argc < 2) {
print_usage();
exit(0);
}
Grammar grammar(G2);
grammar.add_augmented_production();
LR1ParserTableGenerator generator(grammar);
std::cout << "generating parse table\n";
std::vector<std::vector<std::string>> parse_table = generator.build_parse_table();
std::cout << "generating table symbols\n";
std::vector<std::string> symbols = generator.get_table_columns();
std::string output_path(argv[1]);
std::cout << "writing table output to " << output_path << "\n";
int write_result = write_table(output_path, parse_table, symbols);
if(write_result == 0) {
std::cout << "table successfully written\n";
}
return write_result;
}