/
regdx.c
202 lines (202 loc) · 5.87 KB
/
regdx.c
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
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <stdbool.h>
#include <stdint.h>
#include <ctype.h>
enum type { UNUSED, LIT, MARK, EMPTY, NONE, INF, SEQ, OR };
struct reg {
enum type type;
union {
struct { struct reg *head, *tail; };
struct { int ch, len; };
};
bool null;
struct reg *next[256];
uint64_t mask;
int id;
bool done;
};
void dprint(struct reg *r) {
switch(r->type) {
case NONE: printf("None"); return;
case EMPTY: printf("Empty()"); return;
case MARK: printf("Mark(%i)", r->ch); return;
case LIT: printf("Lit(%i, %i)", r->ch, r->len); return;
case INF: printf("Inf("); dprint(r->head); printf(")"); return;
case SEQ: printf("Seq("); dprint(r->head); printf(", "); dprint(r->tail); printf(")"); return;
case OR: printf("Or("); dprint(r->head); printf(", "); dprint(r->tail); printf(")"); return;
case UNUSED: printf("U"); return;
}
printf("@");
}
#define fatal(ARGS...) (fprintf(stderr, "FATAL: "), fprintf(stderr, ARGS), fprintf(stderr, "\n"), exit(-1))
#define CACHE_SIZE (512)
struct reg *insert1(struct reg *cache, enum type type, struct reg *body, bool null, uint64_t mask) {
for(int i = 0; i < CACHE_SIZE; i++)
if(cache[i].type == UNUSED) {
cache[i].type = type;
cache[i].head = body;
cache[i].null = null;
cache[i].mask = mask;
return &cache[i];
} else if(cache[i].head == body)
return &cache[i];
fatal("out of single nodes");
}
struct reg *insert2(struct reg *cache, enum type type, struct reg *head, struct reg *tail, bool null, uint64_t mask) {
for(int i = 0; i < CACHE_SIZE; i++)
if(cache[i].type == UNUSED) {
cache[i].type = type;
cache[i].head = head;
cache[i].tail = tail;
cache[i].null = null;
cache[i].mask = mask;
return &cache[i];
} else if(cache[i].head == head && cache[i].tail == tail)
return &cache[i];
fatal("out of double nodes");
}
struct reg *Lit(int ch, int len) {
static struct reg cache[CACHE_SIZE];
for(int i = 0; i < CACHE_SIZE; i++)
if(cache[i].type == UNUSED) {
cache[i].type = LIT;
cache[i].ch = ch;
cache[i].len = len;
cache[i].null = false;
return &cache[i];
} else if(cache[i].ch == ch && cache[i].len == len)
return &cache[i];
fatal("out of lit nodes");
}
struct reg *Mark(int n) {
static struct reg cache[64];
cache[n].type = MARK;
cache[n].ch = n;
cache[n].null = true;
cache[n].mask = 1 << n;
return &cache[n];
}
struct reg *Empty() {
static struct reg cache;
cache.type = EMPTY;
cache.null = true;
return &cache;
}
struct reg *None() {
static struct reg cache;
cache.type = NONE;
cache.null = false;
return &cache;
}
struct reg *Inf(struct reg *body) {
if(body->type == EMPTY) return Empty();
if(body->type == NONE) return Empty();
if(body->type == INF) return body;
static struct reg cache[CACHE_SIZE];
return insert1(cache, INF, body, true, body->mask);
}
struct reg *Seq(struct reg *head, struct reg *tail) {
if(tail->type == SEQ) return Seq(Seq(head, tail->head), tail->tail);
if(head->type == EMPTY) return tail;
if(tail->type == EMPTY) return head;
if(head->type == NONE) return None();
if(tail->type == NONE) return None();
static struct reg cache[CACHE_SIZE];
return insert2(cache, SEQ, head, tail, head->null && tail->null, head->null ? head->mask | tail->mask : head->mask);
}
struct reg *Or(struct reg *head, struct reg *tail) {
if(head == tail) return head;
if(head->type == OR) return Or(head->head, Or(head->tail, tail));
if(head->type == NONE) return tail;
if(tail->type == NONE) return head;
static struct reg cache[CACHE_SIZE];
return insert2(cache, OR, head, tail, head->null || tail->null, head->mask | tail->mask);
}
struct reg *derive(int ch, struct reg *r) {
if(r->next[ch] == NULL)
switch(r->type) {
case LIT: r->next[ch] = (ch >= r->ch && ch < r->ch + r->len) ? Empty() : None(); break;
case MARK: r->next[ch] = None(); break;
case EMPTY: r->next[ch] = None(); break;
case NONE: r->next[ch] = None(); break;
case INF: r->next[ch] = Seq(derive(ch, r->head), r); break;
case SEQ:
r->next[ch] = Seq(derive(ch, r->head), r->tail);
if(r->head->null)
r->next[ch] = Or(r->next[ch], derive(ch, r->tail));
break;
case OR: r->next[ch] = Or(derive(ch, r->head), derive(ch, r->tail)); break;
case UNUSED: fatal("deriving UNUSED node"); break;
}
return r->next[ch];
}
void explore(struct reg *r) {
dprint(r);
printf("\n");
for(int ch = 0; ch < 256; ch++)
if(r->next[ch] == NULL)
explore(derive(ch, r));
}
void label(struct reg *r) {
static int id = 1;
if(r->id > 0) return;
r->id = id++;
for(int ch = 0; ch < 256; ch++)
label(derive(ch, r));
}
void dfa(struct reg *r) {
if(r->done) return;
r->done = true;
/*printf("{%lu,{", r->mask);
for(int ch = 0; ch < 256; ch++)
printf("%i,", derive(ch, r)->id - 1);
printf("}}, // ");*/
printf("ST_%i: // ", r->id - 1);
dprint(r);
printf("\n");
if(r == None()) {
printf("stuck();\n\n");
return;
}
for(int i = 0; i < 64; i++)
if((r->mask >> i) & 1)
printf("mask(%i);\n", i);
printf("ch = getchar();\n");
for(int ch = 0; ch < 256; ch++)
if(derive(ch, r) != derive(0, r))
printf("if(ch == %i) goto ST_%i;\n", ch, derive(ch, r)->id - 1);
printf("goto ST_%i;\n\n", derive(0, r)->id - 1);
for(int ch = 0; ch < 256; ch++)
dfa(derive(ch, r));
}
int main(int argc, char *argv[]) {
if(argc != 2) {
fprintf(stderr, "usage: %s <text to match>\n", argv[0]);
return -1;
}
reg = argv[1];
struct reg *r = regex();
explore(r);
label(r);
printf("#include <stdio.h>\n");
printf("int main() {\n");
printf("int ch;\n\n");
dfa(r);
printf("}\n");
/*printf("#include <stdio.h>\n");
printf("struct{int done;int next[256];}table[]={\n");
dfa(r);
printf("};\n");
printf("int main(int argc, char *argv[]) {\n");
printf("\tint st = 0, ch;\n");
printf("\twhile(ch = *argv[1]++)\n");
printf("\t\tst = table[st].next[ch];\n");
printf("\treturn table[st].done;\n");
printf("}\n");*/
return 0;
}
//("([^"]|\\.)*"`string)
//((/\*(.*\*/.*)!\*/|)`comment)
//\[^?-?]?