-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paththompson_construction.c
More file actions
executable file
·385 lines (315 loc) · 12.1 KB
/
thompson_construction.c
File metadata and controls
executable file
·385 lines (315 loc) · 12.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
#include "thompson_construction.h"
int state_id = 0;
/* Reads input file and returns the string in it */
char* get_input(char *input_str) {
FILE *fp;
int length;
// open the file
fp = fopen(input_str, "r");
if (fp == NULL) {
perror("Failed opening file");
exit(-1);
}
// read string length
fscanf(fp, "%d", &length);
if (length <= 0) {
perror("Length cant be 0 or less");
return NULL;
}
// read the actual string
char *str = malloc(sizeof(char)*length+1);
fscanf(fp, "%s", str);
fclose(fp);
// set string terminator
str[length] = '\0';
// check if string has bad characters in it
int i=0;
char c;
for (i; i<length; i++) {
c = (char) str[i];
if ( (c>='A' && c<='Z') || (c>='a' && c<='z') || (c>='0' && c<='9') ||
c=='(' || c==')' || c=='|' || c=='.' || c=='*' || c=='#') {
continue;
}
perror("found an invalid character in the alphabet\n");
return NULL;
}
return str;
}
/* Write NFA to output file */
void write_output(NFA *nfa, char *output_str, int output_type, int output_order_type) {
FILE *fp;
int i;
// open the file
fp = fopen(output_str, "w");
if (fp == NULL) {
perror("Failed opening file");
return;
}
if (output_type == 0) {
// initial/final states
fprintf(fp, "%d %d\n", nfa->initial_state, nfa->final_state);
// states
for (i=0; i<nfa->states_no; i++) {
fprintf(fp, "%d ", nfa->states[i]);
}
fprintf(fp, "\n");
// transitions
if (output_order_type == 1) {
// compute ordered output
IntStack *s = init_int_stack(nfa->final_state);
topsort(nfa, s);
// pop one by one states in the stack
int i, state;
int stack_size = s->size;
for (i=0; i<stack_size; i++) {
state = int_stack_pop(s);
// foreach state, print transitions having it as source node
int j;
for (j=0; j<nfa->trans_no; j++) {
if (nfa->transitions[j]->src == state) {
Edge *e = nfa->transitions[j];
fprintf(fp, "%d %d %c\n", e->src, e->dst, e->val);
}
}
}
} else {
// print unordered output
for (i=0; i<nfa->trans_no; i++) {
Edge *e = nfa->transitions[i];
fprintf(fp, "%d %d %c\n", e->src, e->dst, e->val);
}
}
} else {
// prints to file the nfa
fprintf(fp, "========== OUTPUT NFA ==========\n");
fprintf(fp, "| Initial state: %d\n| Final state: %d\n", nfa->initial_state, nfa->final_state);
fprintf(fp, "| States: [ ");
for (i=0; i<nfa->states_no; i++) {
fprintf(fp, "%d ", nfa->states[i]);
}
fprintf(fp, "]\n");
fprintf(fp, "| Transitions: \n");
// transitions
if (output_order_type == 1) {
// compute ordered output
IntStack *s = init_int_stack(nfa->final_state);
topsort(nfa, s);
print_int_stack(s, 0);
// pop one by one states in the stack
int i, state;
int stack_size = s->size;
for (i=0; i<stack_size; i++) {
state = int_stack_pop(s);
// foreach state, print transitions having it as source node
int j;
for (j=0; j<nfa->trans_no; j++) {
if (nfa->transitions[j]->src == state) {
Edge *e = nfa->transitions[j];
fprintf(fp, "| %d -- %c --> %d\n", e->src, e->val, e->dst);
}
}
}
} else {
// print unordered output
for (i=0; i<nfa->trans_no; i++) {
Edge *e = nfa->transitions[i];
fprintf(fp, "| %d -- %c --> %d\n", e->src, e->val, e->dst);
}
}
fprintf(fp, "================================\n");
}
fclose(fp);
}
/* Given a string, parse it and through Thompson Construction builds an nfa */
NFA* nfa_build(char *reg_exp) {
NfaStack *nfa_stack = init_nfa_stack(strlen(reg_exp));
NFA *temp1 = malloc(sizeof(NFA)); temp1 = NULL;
NFA *temp2 = malloc(sizeof(NFA)); temp2 = NULL;
int i;
char symbol;
// iterate through the regular expression
for (i=0; i<strlen(reg_exp); i++) {
symbol = reg_exp[i];
if (symbol == '|') {
// double pop from the stack, apply union and push the result to stack
temp1 = nfa_stack_pop(nfa_stack);
temp2 = nfa_stack_pop(nfa_stack);
NFA *temp = nfa_union(temp2, temp1);
nfa_stack_push(nfa_stack, temp);
} else if (symbol == '.') {
// double pop from the stack, apply concatenation and push the result to stack
temp1 = nfa_stack_pop(nfa_stack);
temp2 = nfa_stack_pop(nfa_stack);
NFA *temp = nfa_concat(temp2, temp1);
nfa_stack_push(nfa_stack, temp);
} else if (symbol == '*') {
// double pop from the stack, apply kleene star and push the result to stack
temp1 = nfa_stack_pop(nfa_stack);
NFA *temp = nfa_kleene(temp1);
nfa_stack_push(nfa_stack, temp);
} else {
// create a symbol nfa and push it to the stack
NFA *temp = nfa_create(symbol);
nfa_stack_push(nfa_stack, temp);
}
}
// there's only one element remaining in the stack, which is our resulting nfa
return nfa_stack_pop(nfa_stack);
}
/* Creates an nfa starting given the transition symbol */
NFA* nfa_create(char symbol) {
// create NFA
NFA *_nfa = nfa(2, 1);
// ids for creating edge
int initial_id = state_id++;
int final_id = state_id++;
_nfa->states[0] = initial_id;
_nfa->states[1] = final_id;
// create edge between states
Edge *_edge = edge(initial_id, final_id, symbol);
// put stuff together
_nfa->transitions[0] = _edge;
_nfa->initial_state = initial_id;
_nfa->final_state = final_id;
}
/* Concatenates two nfas into a new one */
NFA* nfa_concat(NFA *nfa1, NFA *nfa2) {
// create the resulting NFA
int result_states_no = nfa1->states_no + nfa2->states_no - 1;
int result_trans_no = nfa1->trans_no + nfa2->trans_no;
NFA *result = nfa(result_states_no, result_trans_no);
// the merged state and those to merge
int merged_state = state_id++;
int final_state1 = nfa1->final_state;
int initial_state2 = nfa2->initial_state;
/* ===== STATES ===== */
// copy nfa states into result states
int i, j=0;
for (i=0; i<nfa1->states_no; i++) {
if (nfa1->states[i] == final_state1) {
continue; // don't copy the merged state
}
result->states[j++] = nfa1->states[i];
}
for (i=0; i<nfa2->states_no; i++) {
if (nfa2->states[i] == initial_state2) {
continue; // don't copy the merged state
}
result->states[j++] = nfa2->states[i];
}
// add merged state
result->states[j++] = merged_state;
/* ===== EDGES ===== */
// copy nfa transitions into result transitions
j=0;
for (i=0; i<nfa1->trans_no; i++) {
// if src/dst are the old final state, change them to the new one
if (nfa1->transitions[i]->src == final_state1) {
nfa1->transitions[i]->src = merged_state;
}
if (nfa1->transitions[i]->dst == final_state1) {
nfa1->transitions[i]->dst = merged_state;
}
result->transitions[j++] = nfa1->transitions[i];
}
for (i=0; i<nfa2->trans_no; i++) {
// if src/dst are the old initial state, change them to the new one
if (nfa2->transitions[i]->src == initial_state2) {
nfa2->transitions[i]->src = merged_state;
}
if (nfa2->transitions[i]->dst == initial_state2) {
nfa2->transitions[i]->dst = merged_state;
}
result->transitions[j++] = nfa2->transitions[i];
}
// result.initial is now nfa1.inital
result->initial_state = nfa1->initial_state;
// result.final is now nfa2.final
result->final_state = nfa2->final_state;
return result;
}
/* Performs union operation between nfa1 and nfa2 */
NFA* nfa_union(NFA *nfa1, NFA *nfa2) {
// creating the resulting nfa
int result_states_no = nfa1->states_no + nfa2->states_no + 2; // adding two more states
int result_trans_no = nfa1->trans_no + nfa2->trans_no+4; // adding four more transitions
NFA *result = nfa(result_states_no, result_trans_no);
/* ===== STATES ===== */
// copy nfa states into result states
int i, j=0;
for (i=0; i<nfa1->states_no; i++) {
result->states[j++] = nfa1->states[i];
}
for (i=0; i<nfa2->states_no; i++) {
result->states[j++] = nfa2->states[i];
}
// create new initial state for result nfa
result->states[j++] = state_id;
result->initial_state = state_id++;
// create new final state for result nfa
result->states[j++] = state_id;
result->final_state = state_id++;
/* ===== EDGES ===== */
// copy nfa transitions into result transitions
j=0;
for (i=0; i<nfa1->trans_no; i++) {
result->transitions[j++] = nfa1->transitions[i];
}
for (i=0; i<nfa2->trans_no; i++) {
result->transitions[j++] = nfa2->transitions[i];
}
// create 4 epsilon transitions
// - connect result.initial to nfa1.initial
Edge *e1 = edge(result->initial_state, nfa1->initial_state, '#');
result->transitions[j++] = e1;
// - connect result.initial to nfa2.initial
Edge *e2 = edge(result->initial_state, nfa2->initial_state, '#');
result->transitions[j++] = e2;
// - connect nfa1.final to result.final
Edge *e3 = edge(nfa1->final_state, result->final_state, '#');
result->transitions[j++] = e3;
// - connect nfa2.final to result.final
Edge *e4 = edge(nfa2->final_state, result->final_state, '#');
result->transitions[j++] = e4;
return result;
}
/* Performs kleene star operation on nfa */
NFA* nfa_kleene(NFA *_nfa) {
// creating the resulting nfa
int result_states_no = _nfa->states_no + 2; // adding two more states
int result_trans_no = _nfa->trans_no + 4; // adding four more transitions
NFA *result = nfa(result_states_no, result_trans_no);
/* ===== STATES ===== */
// copy _nfa states into result
int i, j=0;
for (i=0; i<_nfa->states_no; i++) {
result->states[j++] = _nfa->states[i];
}
// create new initial state for result
result->states[j++] = state_id;
result->initial_state = state_id++;
// create new final state for result
result->states[j++] = state_id;
result->final_state = state_id++;
/* ===== EDGES ===== */
// copy edges into result
j=0;
for (i=0; i<_nfa->trans_no; i++) {
result->transitions[j++] = _nfa->transitions[i];
}
// create 4 epsilon transitions
// - connect result.initial to nfa.initial
Edge *e1 = edge(result->initial_state, _nfa->initial_state, '#');
result->transitions[j++] = e1;
// - connect new.initial to new.final
Edge *e2 = edge(result->initial_state, result->final_state, '#');
result->transitions[j++] = e2;
// - connect nfa.final to nfa.inital
Edge *e3 = edge(_nfa->final_state, _nfa->initial_state, '#');
result->transitions[j++] = e3;
// - connect nfa.final to new.final
Edge *e4 = edge(_nfa->final_state, result->final_state, '#');
result->transitions[j++] = e4;
return result;
}