-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathequiv.c
More file actions
46 lines (39 loc) · 1.17 KB
/
equiv.c
File metadata and controls
46 lines (39 loc) · 1.17 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
#include "ltre.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(void) {
size_t len = 0, cap = 256;
char *line = malloc(cap);
for (; !feof(stdin); len = 0) {
for (int c; c = fgetc(stdin), c != EOF && c != '\n'; line[len++] = c)
len + 1 == cap ? line = realloc(line, cap *= 2) : 0;
line[len] = '\0';
if (ferror(stdin))
perror("fgetc"), exit(EXIT_FAILURE);
if (feof(stdin))
break;
char *sep = memchr(line, '\t', len);
if (sep == NULL) {
fprintf(stderr, "format error: could not find tab separator\n");
continue;
}
*sep = '\0';
char *error = NULL, *loc = line;
struct regex *regex1 = ltre_parse(&loc, &error);
if (error)
goto parse_error;
loc = sep + 1;
struct regex *regex2 = ltre_parse(&loc, &error);
if (error) {
parse_error:
fprintf(stderr, "parse error: %s at pattern[%zu] near '%.16s'\n", error,
loc - line, loc);
continue;
}
struct dstate *dfa1 = ltre_compile(regex1), *dfa2 = ltre_compile(regex2);
puts(dfa_equivalent(dfa1, dfa2) ? "equivalent" : "not equivalent");
dfa_free(dfa1), dfa_free(dfa2);
}
free(line);
}