-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathruntime_parser.cpp
More file actions
404 lines (347 loc) · 11.1 KB
/
Copy pathruntime_parser.cpp
File metadata and controls
404 lines (347 loc) · 11.1 KB
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
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
#include <cassert>
#include <exception>
#include <iostream>
#include <map>
#include <memory>
#include <optional>
#include <sstream>
#include <string>
#include <vector>
struct Token {
enum class Type {
// variable
Variable = 0,
// braces
BraceOpen = 1,
BraceClose = 2,
// logical operators
LogicNot = -10,
LogicAnd = 10,
LogicOr = 20,
LogicImplies = 30,
// temporal operators
TemporalNext = 100,
TemporalGlobal = 200,
TemporalFuture = 300,
TemporalUntil = 400,
TemporalRelease = 500,
// eof
End = -1,
} type;
int arg1{0};
};
enum class ErrorCode {
BadLexeme = 100,
BraceBalance = 200,
IOError = -200,
ParseError = 1000,
};
class ParserError : public std::exception {
public:
ErrorCode ec;
virtual const char* what() const noexcept
{
switch (ec) {
case ErrorCode::BadLexeme: return "bad lexeme";
case ErrorCode::BraceBalance: return "wrong brace balance";
case ErrorCode::IOError: return "I/O error";
case ErrorCode::ParseError: return "failed to parse";
default: assert(false && "unknown error code");
};
}
ParserError(ErrorCode ec_) : ec(ec_) {}
};
class VariableMap {
private:
std::map<std::string, int> id_{};
std::map<int, std::string> name_{};
int last_id_{0};
public:
int get_id(const std::string& name)
{
if (id_.count(name) == 0U) {
id_[name] = ++last_id_;
name_[last_id_] = name;
}
return id_[name];
}
const std::string& get_name(int id) const { return name_.at(id); }
} vm;
class Lexer {
private:
std::istream& in_;
char try_get_()
{
try {
return in_.get();
} catch (std::ios_base::failure const&) {
throw ParserError(ErrorCode::IOError);
}
}
static bool is_name_(char ch)
{
return ch == '_' || ('a' <= ch && ch <= 'z');
}
public:
Lexer(std::istream& in) : in_(in) {}
Lexer(const Lexer&) = delete;
Lexer(Lexer&&) = delete;
Lexer& operator=(const Lexer&) = delete;
Lexer& operator=(Lexer&&) = delete;
~Lexer() = default;
std::istream::pos_type mark() const { return in_.tellg(); }
void reset(std::istream::pos_type pos) { in_.seekg(pos); }
Token next()
{
char ch;
while (isspace(ch = try_get_())) {}
switch (ch) {
case std::istream::traits_type::eof(): return {Token::Type::End};
case '(': return {Token::Type::BraceOpen};
case ')': return {Token::Type::BraceClose};
case '&':
if (try_get_() == '&') return {Token::Type::LogicAnd};
throw ParserError(ErrorCode::BadLexeme);
case '|':
if (try_get_() == '|') return {Token::Type::LogicOr};
throw ParserError(ErrorCode::BadLexeme);
case '-':
if (try_get_() == '>') return {Token::Type::LogicImplies};
throw ParserError(ErrorCode::BadLexeme);
case 'X': return {Token::Type::TemporalNext};
case 'F': return {Token::Type::TemporalFuture};
case 'G': return {Token::Type::TemporalGlobal};
case 'U': return {Token::Type::TemporalUntil};
case 'R': return {Token::Type::TemporalRelease};
case '!': return {Token::Type::LogicNot};
default: {
if (!is_name_(ch)) {
throw ParserError(ErrorCode::BadLexeme);
}
std::string name{ch};
while (true) {
auto mark1 = mark();
if (char next_ch = try_get_(); is_name_(next_ch)) {
name += next_ch;
} else {
reset(mark1);
break;
}
}
return {Token::Type::Variable, vm.get_id(name)};
}
}
}
};
struct Term {
enum class Type {
// prop var
Prop = 0,
// logical operators
LogicNot = -10,
LogicAnd = 10,
LogicOr = 20,
LogicImplies = 30,
// temporal operators
TemporalNext = 100,
TemporalGlobal = 200,
TemporalFuture = 300,
TemporalUntil = 400,
TemporalRelease = 500,
} type;
std::vector<std::unique_ptr<Term>> args{};
int arg3{0};
};
/*
// TODO: add Until, Release
C >> I {-> I}
D >> C {&& C}*
E >> D {|| D}*
I >> P , (E) , XI , FI , GI , !I
TERM >> I
*/
class Parser {
private:
Lexer& lexer_;
// std::optional<Term> parse_clause_();
std::optional<Term> parse_implication_()
{
auto mark = lexer_.mark();
Term result{Term::Type::LogicImplies};
std::optional<Term> lhs, rhs;
if (lhs = parse_clause_(); !lhs) goto fail;
if (auto mark1 = lexer_.mark();
lexer_.next().type != Token::Type::LogicImplies) {
lexer_.reset(mark1);
return *std::move(lhs);
}
if (rhs = parse_clause_(); !rhs) goto fail;
result.args.push_back(std::make_unique<Term>(std::move(*lhs)));
result.args.push_back(std::make_unique<Term>(std::move(*rhs)));
return result;
fail:
lexer_.reset(mark);
return {};
}
std::optional<Term> parse_conjunction_()
{
auto mark = lexer_.mark();
Term result{Term::Type::LogicAnd};
auto term = parse_implication_();
if (!term) goto fail;
result.args.push_back(std::make_unique<Term>(std::move(*term)));
while (true) {
if (auto mark1 = lexer_.mark();
lexer_.next().type != Token::Type::LogicAnd) {
lexer_.reset(mark1);
break;
} else {
if (term = parse_implication_(); !term) goto fail;
result.args.push_back(
std::make_unique<Term>(std::move(*term)));
}
}
return result.args.size() > 1 ? std::move(result)
: std::move(*result.args[0]);
fail:
lexer_.reset(mark);
return {};
}
std::optional<Term> parse_expression_()
{
auto mark = lexer_.mark();
Term result{Term::Type::LogicOr};
auto term = parse_conjunction_();
if (!term) goto fail;
result.args.push_back(std::make_unique<Term>(std::move(*term)));
while (true) {
if (auto mark1 = lexer_.mark();
lexer_.next().type != Token::Type::LogicOr) {
lexer_.reset(mark1);
break;
} else {
if (term = parse_implication_(); !term) goto fail;
result.args.push_back(
std::make_unique<Term>(std::move(*term)));
}
}
return result.args.size() > 1 ? std::move(result)
: std::move(*result.args[0]);
fail:
lexer_.reset(mark);
return {};
}
std::optional<Term> parse_clause_()
{
auto mark = lexer_.mark();
auto lexeme = lexer_.next();
switch (lexeme.type) {
case Token::Type::Variable:
return Term{Term::Type::Prop, {}, lexeme.arg1};
case Token::Type::BraceOpen: {
auto term = parse_expression_();
if (!term || lexer_.next().type != Token::Type::BraceClose) {
goto fail;
}
return *std::move(term);
}
case Token::Type::TemporalNext:
case Token::Type::TemporalFuture:
case Token::Type::TemporalGlobal:
case Token::Type::LogicNot: {
auto term = parse_clause_();
if (!term) goto fail;
Term result{static_cast<Term::Type>(lexeme.type)};
result.args.push_back(
std::make_unique<Term>(std::move(*term)));
return result;
}
default: goto fail;
}
fail:
lexer_.reset(mark);
return {};
}
std::optional<Term> parse_term_() { return parse_expression_(); }
public:
Parser(Lexer& lexer) : lexer_(lexer) {}
~Parser() = default;
Parser(const Parser&) = delete;
Parser(Parser&&) = delete;
Parser& operator=(const Parser&) = delete;
Parser& operator=(Parser&&) = delete;
Term parse()
{
auto term = parse_term_();
if (!term) throw ParserError(ErrorCode::ParseError);
return *std::move(term);
}
};
const char* op_mnemonic(Term::Type op)
{
switch (op) {
case Term::Type::LogicNot: return "!";
case Term::Type::LogicOr: return "||";
case Term::Type::LogicAnd: return "&&";
case Term::Type::LogicImplies: return "->";
case Term::Type::TemporalNext: return "X";
case Term::Type::TemporalFuture: return "F";
case Term::Type::TemporalGlobal: return "G";
default: assert(0 && "no known mnemonic");
}
}
void dump_term_recursive(const Term& term, std::ostream& out)
{
switch (term.type) {
case Term::Type::Prop:
out << reinterpret_cast<uintptr_t>(&term) << " "
<< "[label = \"" << vm.get_name(term.arg3) << "\" "
<< "color = blue];" << std::endl;
break;
case Term::Type::LogicNot:
case Term::Type::TemporalNext:
case Term::Type::TemporalFuture:
case Term::Type::TemporalGlobal:
out << reinterpret_cast<uintptr_t>(&term) << " "
<< "[label = \"" << op_mnemonic(term.type) << "\" "
<< "color = red];" << std::endl;
dump_term_recursive(*term.args[0], out);
out << reinterpret_cast<uintptr_t>(&term) << " -> { "
<< reinterpret_cast<uintptr_t>(&*term.args[0]) << " } "
<< "[color = black];" << std::endl;
break;
case Term::Type::LogicAnd:
case Term::Type::LogicOr:
case Term::Type::LogicImplies:
out << reinterpret_cast<uintptr_t>(&term) << " "
<< "[label = \"" << op_mnemonic(term.type) << "\" "
<< "color = green];" << std::endl;
for (const auto& arg : term.args) {
dump_term_recursive(*arg, out);
out << reinterpret_cast<uintptr_t>(&term) << " -> { "
<< reinterpret_cast<uintptr_t>(&*arg) << " } "
<< "[color = black];" << std::endl;
}
break;
default: assert(0);
}
}
void dump_term(const Term& term, std::ostream& out)
{
out << "digraph G {" << std::endl;
dump_term_recursive(term, out);
out << "}" << std::endl;
}
int main()
{
std::string prog = "(G (kek -> lol) && F kek) -> F lol";
std::istringstream in(prog);
try {
Lexer lexer(in);
Parser parser(lexer);
auto term = parser.parse();
dump_term(term, std::cout);
} catch (std::exception const& exc) {
std::cout << exc.what() << std::endl;
}
}