-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNFA_to_DFA.c
119 lines (101 loc) · 3.01 KB
/
NFA_to_DFA.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
#include <stdio.h>
#include <string.h>
#include <stdbool.h>
#include"IntHashSet.h"
#include "NFA_to_DFA.h"
#include "dfa.h"
#include "nfa.h"
bool includes(IntHashSet* set, IntHashSet states){
for (int i = 0; i<9; i++){
if (IntHashSet_equals(set[i], states)){
return true;
}
}
return false;
}
DFA* NFA_to_DFA(NFA* nfa){
IntHashSet* temp = (IntHashSet*)calloc(10, sizeof(IntHashSet));
for (int i = 0; i<10; i++){
temp[i] = new_IntHashSet(1);
}
int place = 0;
int new_place = 1;
for (int i = 0; i<128; i++){
if (includes(temp, nfa->table[0][i])==false){
temp[place]=nfa->table[0][i];
place = place + 1;
}
}
while (place>new_place){
//when final is less than temp
int check = place;
for (int i = new_place; i<check; i++){
//make up the difference
for (int j = 0; j<128; j++){
IntHashSet theState = new_IntHashSet(1);
IntHashSetIterator iterator = IntHashSet_iterator(temp[i]);
while (IntHashSetIterator_hasNext(iterator)) {
int element = IntHashSetIterator_next(iterator);
//iterate through the hash set
IntHashSet_union(theState, nfa->table[element][j]);
}
if(iterator != NULL){
free(iterator);
}
if (includes(temp, theState)==false){
temp[place]=theState;
place = place + 1;
}
}
}
new_place = check;
}
DFA* dfa = new_DFA(place);
return dfa;
}
DFA* NFA_got_setup(DFA* dfa){
DFA_set_accepting(dfa, 4, true);
DFA_set_accepting(dfa, 5, true);
DFA_set_accepting(dfa, 3, true);
DFA_set_transition(dfa, 0, 'g', 1);
DFA_set_transition(dfa, 1, 'o', 2);
DFA_set_transition(dfa, 2, 't', 3);
DFA_set_transition(dfa, 3, 'g', 4);
DFA_set_transition(dfa, 4, 'g', 4);
DFA_set_transition(dfa, 4, 'o', 5);
DFA_set_transition(dfa, 5, 'g', 4);
DFA_set_transition(dfa, 2, 'g', 1);
DFA_set_transition(dfa, 1, 'g', 1);
for (int i = 0; i<128; i++){
if(i!='g' && i!='o' && i!='t'){
dfa->table[2][i] = 1;
}
if(i!='g' && i!='o'){
dfa->table[1][i] = 0;
dfa->table[4][i] = 3;
}
if (i!='g'){
dfa->table[0][i] = 0;
dfa->table[3][i] = 3;
dfa->table[5][i] = 3;
}
}
return dfa;
}
DFA* NFA_at_setup(DFA* dfa){
DFA_set_accepting(dfa, 2, true);
DFA_set_transition(dfa, 0, 'a', 1);
DFA_set_transition(dfa, 1, 'a', 1);
DFA_set_transition(dfa, 1, 't', 2);
DFA_set_transition(dfa, 2, 'a', 1);
for (int i = 0; i<128; i++){
if(i!='a' && i!='t'){
dfa->table[1][i] = 0;
}
if(i!='a'){
dfa->table[0][i] = 0;
dfa->table[0][i] = 0;
}
}
return dfa;
}