-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsynth.c
More file actions
62 lines (51 loc) · 1.65 KB
/
synth.c
File metadata and controls
62 lines (51 loc) · 1.65 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
#include "ltre.h"
#include <stdio.h>
#include <stdlib.h>
// steal implementation details. this probably has undefined behavior
struct dstate {
struct dstate *transitions[256];
bool accepting, terminating;
};
bool run(struct dstate *dfa) {
// if all outbound transitions are terminating, return. otherwise, if exactly
// one outbound transition is non-terminating, follow it. otherwise, more than
// one outbound transition is non-terminating, so let the user disambiguate.
// interactive use works best with `stty -icanon -echo -nl`
for (int chr = 0;; dfa = dfa->transitions[chr]) {
if (putchar(chr) == EOF)
break;
for (chr = 0; chr < 256; chr++)
if (!dfa->transitions[chr]->terminating)
goto found;
break;
found:
for (int c = chr + 1; c < 256; c++)
if (!dfa->transitions[c]->terminating)
goto ambiguous;
continue;
ambiguous:
if ((chr = getchar()) == EOF)
break;
// if (dfa->transitions[chr]->terminating)
// goto ambiguous;
}
return dfa->accepting;
}
int main(int argc, char **argv) {
if (argc != 2)
fprintf(stderr, "Usage: synth <pattern>\n"), exit(EXIT_FAILURE);
char *pattern = argv[1];
char *error = NULL, *loc = pattern;
struct regex *regex = ltre_parse(&loc, &error);
if (error)
fprintf(stderr, "parse error: %s at pattern[%zu] near '%.16s'\n", error,
loc - pattern, loc),
exit(EXIT_FAILURE);
struct dstate *dfa = ltre_determinize(regex);
dfa_mark(dfa); // mark terminating states. faster than `dfa_minimize`
// while (1)
// puts(run(dfa) ? "\naccept" : "\nreject");
bool accept = run(dfa);
dfa_free(dfa);
return !accept;
}