-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
684 lines (575 loc) · 18 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
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
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
#include <getopt.h>
#include <graphviz/gvc.h>
#include <array>
#include <vector>
#include <string>
#include <stack>
#include <queue>
#include <unordered_set>
#include <unordered_map>
#include <set>
#include <ranges>
#include <algorithm>
#include <numeric>
#include <optional>
#include <charconv>
#include <cassert>
#include <cstdint>
#include <sys/types.h>
/* Typedefs */
/* clang-format off */
using u8 = uint8_t;
using u32 = uint32_t;
using usize = size_t;
/* Namespace aliases */
namespace ranges = std::ranges;
/* Macros */
#define DEFAULT_ALPHABET "abcdefghijklmnopqrstuvwxyz"
#define ALL_ALPHANUMS "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890"
#define START_COLOR "turquoise"
#define FINAL_COLOR "x11green"
#define START_FINAL_COLOR START_COLOR ":" FINAL_COLOR
#define FONT "monospace"
#define S_LAMBDA '\0'
#define OP_CONCAT '.'
#define OP_UNION '|'
#define OP_KLEENE '*'
#define OP_PLUS '+'
#define OP_OPT '?'
#define IS_UNARY(x) (x == OP_KLEENE || x == OP_PLUS || x == OP_OPT)
#define NUM_CHARS (1 << 8)
#define LAMBDA_UTF {char(0xce), char(0xbb)}
/* Enums */
enum class TokenType : u8 {
REGULAR = 0,
OPERATOR,
LEFT_PAREN,
RIGHT_PAREN,
ERROR,
};
enum GraphNodeFlag : u32 {
VISITED = 1 << 0,
START = 1 << 1,
FINAL = 1 << 2,
};
/* clang-format on */
/* Structs */
struct NFAFragment {
usize start;
usize finish;
};
struct Transition {
constexpr auto operator<=>(const Transition&) const = default;
usize dest;
char symbol;
};
struct Graph {
std::vector<std::vector<Transition>> adj;
std::vector<u32> flags;
usize start;
};
struct AgobjAttrs {
const char* label = nullptr;
const char* style = nullptr;
const char* font = nullptr;
const char* color = nullptr;
const char* rankdir = nullptr;
};
/* Globals */
static std::string alphabet = DEFAULT_ALPHABET;
static constexpr auto OP_PREC = []() {
std::array<u8, NUM_CHARS> arr = {};
arr[OP_KLEENE] = 3;
arr[OP_PLUS] = 3;
arr[OP_OPT] = 3;
arr[OP_CONCAT] = 2;
arr[OP_UNION] = 1;
return arr;
}();
/* Functions declarations */
static TokenType type_of(char);
static std::string add_concatenation_op(std::string_view);
static std::optional<std::string> get_postfix(std::string_view);
static std::optional<Graph> get_nfa_graph(std::string_view);
static void add_transitive_closure_helper(usize, usize, std::vector<Transition>&, Graph&);
static void add_transitive_closure(Graph&);
static void remove_lambdas(Graph&);
static Graph to_dfa_graph(const Graph&);
static void print_components(const Graph&, FILE*);
static void set_attrs(void*, const AgobjAttrs&);
static void export_graph(const Graph&, FILE*, std::string_view);
static void usage();
/* Functions definitions */
template<>
struct std::hash<std::vector<usize>> {
std::size_t
operator()(const std::vector<usize>& xs) const noexcept
{
std::size_t seed = 0;
for (std::size_t x : xs)
seed ^= x + 0x9e3779b9 + (seed << 6) + (seed >> 2); /* from boost::hash_combine */
return seed;
}
};
TokenType
type_of(char token)
{
auto token_idx = u8(token);
if (OP_PREC[token_idx])
return TokenType::OPERATOR;
if (token == '(')
return TokenType::LEFT_PAREN;
if (token == ')')
return TokenType::RIGHT_PAREN;
if (alphabet.find(token) != alphabet.npos)
return TokenType::REGULAR;
return TokenType::ERROR;
}
std::string
add_concatenation_op(const std::string_view infix)
{
if (infix.empty())
return "";
std::string result{infix.substr(0, 1)};
for (usize i = 1; i < infix.size(); ++i) {
const char a = infix[i - 1];
const char b = infix[i];
const auto t_a = type_of(a);
const auto t_b = type_of(b);
/* Cases where the concatenation operator needs to be added */
if ((t_a == TokenType::REGULAR || IS_UNARY(a) || a == ')') &&
(t_b == TokenType::REGULAR || b == '('))
result += OP_CONCAT;
result += b;
}
return result;
}
std::optional<std::string>
get_postfix(const std::string_view infix)
{
/* Apply Dijkstra's 'shunting yard' algorithm */
std::string postfix = "";
std::stack<char, std::vector<char>> operators;
for (char token : infix) {
switch (type_of(token)) {
case TokenType::REGULAR:
postfix += token;
break;
case TokenType::OPERATOR:
while (!operators.empty() && operators.top() != '(' &&
OP_PREC[u8(operators.top())] >= OP_PREC[u8(token)]) {
postfix += operators.top();
operators.pop();
}
operators.push(token);
break;
case TokenType::LEFT_PAREN:
operators.push(token);
break;
case TokenType::RIGHT_PAREN:
while (!operators.empty() && operators.top() != '(') {
postfix += operators.top();
operators.pop();
}
if (operators.empty() || operators.top() != '(')
return std::nullopt;
operators.pop();
break;
case TokenType::ERROR:
return std::nullopt;
}
}
while (!operators.empty()) {
auto op = operators.top();
if (op == '(')
return std::nullopt;
postfix += op;
operators.pop();
}
return postfix;
}
std::optional<Graph>
get_nfa_graph(const std::string_view postfix)
{
/* Apply Thompson's construction algorithm */
Graph g{};
auto& [adj, flags, _] = g;
std::stack<NFAFragment, std::vector<NFAFragment>> nfa_components;
for (char token : postfix) {
usize q, f;
if (token == OP_CONCAT || token == OP_UNION) {
if (nfa_components.size() < 2)
return std::nullopt;
auto y = nfa_components.top();
nfa_components.pop();
auto x = nfa_components.top();
nfa_components.pop();
if (token == OP_CONCAT) {
adj[x.finish] = {{y.start, S_LAMBDA}};
q = x.start;
f = y.finish;
} else {
q = adj.size();
adj.emplace_back();
adj[q] = {{x.start, S_LAMBDA}, {y.start, S_LAMBDA}};
f = adj.size();
adj.emplace_back();
adj[x.finish] = {{f, S_LAMBDA}};
adj[y.finish] = {{f, S_LAMBDA}};
}
} else if (IS_UNARY(token)) {
if (nfa_components.empty())
return std::nullopt;
auto x = nfa_components.top();
nfa_components.pop();
f = adj.size();
adj.emplace_back();
q = adj.size();
adj.emplace_back();
if (token == OP_KLEENE) {
adj[q] = {{x.start, S_LAMBDA}, {f, S_LAMBDA}};
adj[x.finish] = {{x.start, S_LAMBDA}, {f, S_LAMBDA}};
} else if (token == OP_PLUS) {
adj[q] = {{x.start, S_LAMBDA}};
adj[x.finish] = {{x.start, S_LAMBDA}, {f, S_LAMBDA}};
} else {
adj[q] = {{x.start, S_LAMBDA}, {f, S_LAMBDA}};
adj[x.finish] = {{f, S_LAMBDA}};
}
} else {
f = adj.size();
adj.emplace_back();
q = adj.size();
adj.emplace_back();
adj[q] = {{f, token}};
}
nfa_components.push({q, f});
}
if (nfa_components.empty())
return std::nullopt;
auto [start, finish] = nfa_components.top();
g.start = start;
flags.resize(adj.size());
flags[start] |= START;
flags[finish] |= FINAL;
return g;
}
void
add_transitive_closure_helper(usize from, usize src, std::vector<Transition>& to_add, Graph& g)
{
auto& [adj, flags, _] = g;
if (flags[src] & VISITED)
return;
flags[src] |= VISITED;
for (auto [dest, symbol] : adj[src]) {
if (symbol == S_LAMBDA) {
to_add.emplace_back(dest, symbol);
flags[from] |= flags[dest] & FINAL;
add_transitive_closure_helper(from, dest, to_add, g);
}
}
}
void
add_transitive_closure(Graph& g)
{
auto& [adj, flags, _] = g;
std::vector<Transition> to_add;
for (usize src = 0; src < adj.size(); ++src) {
for (auto& f : flags)
f &= ~VISITED;
add_transitive_closure_helper(src, src, to_add, g);
adj[src].insert(adj[src].end(), to_add.begin(), to_add.end());
to_add.clear();
}
}
void
remove_lambdas(Graph& g)
{
auto& adj = g.adj;
for (usize u = 0; u < adj.size(); ++u) {
std::vector<Transition> to_add;
for (auto [v, to_v] : adj[u]) {
if (to_v != S_LAMBDA)
continue;
for (auto [w, to_w] : adj[v]) {
if (to_w != S_LAMBDA)
to_add.emplace_back(w, to_w);
}
}
adj[u].insert(adj[u].end(), to_add.begin(), to_add.end());
}
for (auto& ts : adj) {
auto lambdas = ranges::partition(ts, [](auto& t) { return t.symbol != S_LAMBDA; });
ts.erase(lambdas.begin(), lambdas.end());
ranges::sort(ts);
auto duplicates = ranges::unique(ts);
ts.erase(duplicates.begin(), duplicates.end());
}
}
Graph
to_dfa_graph(const Graph& nfa)
{
Graph dfa{};
if (nfa.adj.empty())
return dfa;
std::queue<std::vector<usize>> queue;
std::unordered_map<std::vector<usize>, usize> ids;
queue.push({nfa.start});
ids.insert({{nfa.start}, 0});
dfa.adj.emplace_back();
dfa.flags.emplace_back();
dfa.flags[0] |= START;
dfa.start = 0;
while (!queue.empty()) {
auto src_subset = std::move(queue.front());
queue.pop();
auto src_subset_id = ids.at(src_subset);
/* Check if this subset will become a final node */
for (auto src : src_subset)
dfa.flags[src_subset_id] |= nfa.flags[src] & FINAL;
/* Create edges from the source subset through each symbol */
for (char target_symbol : alphabet) {
std::unordered_set<usize> dest_subset;
for (auto src : src_subset) {
for (auto [dest, symbol] : nfa.adj[src]) {
if (symbol == target_symbol)
dest_subset.insert(dest);
}
}
if (dest_subset.empty())
continue;
auto dest_subset_id = dfa.adj.size();
auto dest_vec = std::vector(dest_subset.begin(), dest_subset.end());
auto [it, inserted] = ids.emplace(dest_vec, dest_subset_id);
/*
* If this subset has not been visited yet, give it an identifier
* and add it to the queue.
*/
if (inserted) {
dfa.adj.emplace_back();
dfa.flags.emplace_back();
queue.push(std::move(dest_vec));
} else {
dest_subset_id = it->second;
}
/* Create the edge from the source subset to the destination */
dfa.adj[src_subset_id].emplace_back(dest_subset_id, target_symbol);
}
}
return dfa;
}
void
print_components(const Graph& g, FILE* output)
{
auto& [adj, flags, start] = g;
auto size = adj.size();
/* Print states */
fprintf(output, "STATES = {");
bool first = true;
for (usize src = 0; src < size; ++src) {
fprintf(output, first ? "q%lu" : ", q%lu", src);
first = false;
}
fprintf(output, "}\n");
/* Print alphabet */
std::set<char> min_alphabet;
for (usize src = 0; src < size; ++src) {
for (auto& [_, symbol] : adj[src])
min_alphabet.insert(symbol);
}
fprintf(output, "SIGMA = {");
first = true;
for (auto it = min_alphabet.begin(); it != min_alphabet.end(); ++it) {
fprintf(output, first ? "%c" : ", %c", *it);
first = false;
}
fprintf(output, "}\n");
/* Print transitions */
fprintf(output, "TRANSITIONS:\n");
for (usize src = 0; src < size; ++src) {
for (auto [dest, symbol] : adj[src])
fprintf(output, "\tδ(q%lu, %c) = q%lu\n", src, symbol, dest);
}
/* Print start state */
fprintf(output, "START STATE = q%lu\n", g.start);
/* Print final states */
fprintf(output, "FINAL STATES = {");
first = true;
for (usize src = 0; src < size; ++src) {
if (flags[src] & FINAL) {
fprintf(output, first ? "q%lu" : ", q%lu", src);
first = false;
}
}
fprintf(output, "}\n");
}
void
set_attrs(void* obj, const AgobjAttrs& attrs)
{
if (attrs.label)
agsafeset(obj, (char*)"label", (char*)attrs.label, (char*)"");
if (attrs.color)
agsafeset(obj, (char*)"color", (char*)attrs.color, (char*)"");
if (attrs.font)
agsafeset(obj, (char*)"fontname", (char*)attrs.font, (char*)"");
if (attrs.style)
agsafeset(obj, (char*)"style", (char*)attrs.style, (char*)"");
if (attrs.rankdir)
agsafeset(obj, (char*)"rankdir", (char*)attrs.rankdir, (char*)"");
}
void
export_graph(const Graph& g, FILE* output, const std::string_view infix)
{
const auto& [adj, flags, _] = g;
const usize size = adj.size();
Agraph_t* graph = agopen((char*)"g", Agdirected, 0);
assert(graph);
set_attrs(graph, {.label = infix.data(), .font = FONT, .rankdir = "LR"});
std::vector<Agnode_t*> g_nodes(size, nullptr);
std::array<char, 4> lb = {};
for (usize src = 0; src < size; ++src) {
*std::to_chars(lb.data(), lb.data() + sizeof(lb) - 1, src).ptr = '\0';
auto node = agnode(graph, lb.data(), 1);
assert(node);
g_nodes[src] = node;
AgobjAttrs attrs;
switch (flags[src] & (START | FINAL)) {
case START | FINAL:
attrs = {.style = "wedged", .font = FONT, .color = START_FINAL_COLOR};
break;
case START:
attrs = {.style = "filled", .font = FONT, .color = START_COLOR};
break;
case FINAL:
attrs = {.style = "filled", .font = FONT, .color = FINAL_COLOR};
break;
default:
attrs = {.font = FONT};
break;
}
set_attrs(node, attrs);
}
for (usize src = 0; src < size; ++src) {
for (auto [dest, symbol] : adj[src]) {
lb = {symbol};
if (lb[0] == S_LAMBDA)
lb = LAMBDA_UTF;
auto edge = agedge(graph, g_nodes[src], g_nodes[dest], nullptr, 1);
assert(edge);
set_attrs(edge, {.label = lb.data(), .font = FONT});
}
}
GVC_t* context = gvContext();
assert(context);
gvLayout(context, graph, "dot");
gvRender(context, graph, "dot", output);
gvFreeLayout(context, graph);
agclose(graph);
}
void
usage()
{
/* clang-format off */
fprintf(
stderr,
"%s\n",
"USAGE:\n"
" rtd [FLAGS/OPTIONS] <regex>\n\n"
"FLAGS:\n"
" -h\n"
" Print help info.\n"
" -a\n"
" Set the alphabet of the regex as all alphanumericals.\n"
" -e\n"
" Export the graph in DOT language (by default, only the DFA components will be printed)\n\n"
"OPTIONS:\n"
" -s <alphabet>\n"
" Set the alphabet of the regex (only alphanumericals allowed).\n"
" -o <output_file>\n"
" Set the path at which the graph file will be written (default is stdout).");
/* clang-format on */
}
int
main(const int argc, char* argv[])
{
const char* output_path = nullptr;
bool all_alnum = false;
bool exp = false;
int opt;
while ((opt = getopt(argc, argv, "heas:o:")) != -1) {
switch (opt) {
case 'h':
usage();
return EXIT_FAILURE;
case 'e':
exp = true;
break;
case 'a':
all_alnum = true;
break;
case 's':
alphabet = optarg;
break;
case 'o':
output_path = optarg;
break;
default:
usage();
return EXIT_FAILURE;
}
}
if (all_alnum)
alphabet = ALL_ALPHANUMS;
if (alphabet.empty()) {
fprintf(stderr, "The alphabet can not be empty\n");
return EXIT_FAILURE;
}
if (optind >= argc) {
fprintf(stderr, "Missing <regex> argument\n\n");
usage();
return EXIT_FAILURE;
}
for (char c : alphabet) {
if (!std::isalnum(c)) {
fprintf(stderr, "The alphabet can only contain alphanumericals\n");
return EXIT_FAILURE;
}
}
/* Remove duplicates from alphabet input */
auto set = std::set<char>(alphabet.begin(), alphabet.end());
alphabet = std::string(set.begin(), set.end());
const std::string_view infix = argv[optind];
const auto with_concat_op = add_concatenation_op(infix);
const auto postfix = get_postfix(with_concat_op);
if (!postfix) {
fprintf(stderr, "Regex '%s' is invalid\n", infix.data());
usage();
return EXIT_FAILURE;
}
#ifdef RTD_DEBUG
fprintf(stderr,
"Infix: %s\nInfix with explicit concatenation operator: %s\nPostfix: %s\n",
infix.data(),
with_concat_op.data(),
postfix->data());
#endif
auto nfa_graph = get_nfa_graph(*postfix);
if (!nfa_graph) {
fprintf(stderr, "Failed to make NFA from regex\n");
usage();
return EXIT_FAILURE;
}
/* Transform λ-NFA to NFA without λ-transitions */
add_transitive_closure(*nfa_graph);
remove_lambdas(*nfa_graph);
auto dfa_graph = to_dfa_graph(*nfa_graph);
auto output = output_path ? fopen(output_path, "w") : stdout;
if (!output) {
perror("fopen");
return EXIT_FAILURE;
}
if (exp)
export_graph(dfa_graph, output, "\n\n" + std::string(infix));
else
print_components(dfa_graph, output);
}