-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconvertorRegexp2NFA.c
280 lines (230 loc) · 7.96 KB
/
convertorRegexp2NFA.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
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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>
#include "datastructs.h"
#include "lexer.h"
#include "parser.h"
#include "convertorRegexp2NFA.h"
#define HASHTABLE_BASIS 17
#define DEF_ACT_MAX_SIZE 15
#define DEF_SYM_MAX_SIZE 15
#define DEF_STA_MAX_SIZE 50
/************************* Regexp to NFA convertor *****************************/
static int actionMaxSize;
static int stateMaxSize;
static int currentFinalState;
static int prognosticFlag;
TrStNFA* newTrStNFA(int state){
TrStNFA* newState = malloc(sizeof(TrStNFA));
newState->state = state;
newState->altState = NULL;
return newState;
}
void addTrStNFA(int state, int symbol, int newState){
if(transitionTableNFA[state * symbolMaxSize + symbol] == NULL){
//printf("addTrStNFA1 %i %i %c\n", state, newState, indexSymbolArray[symbol]);
transitionTableNFA[state * symbolMaxSize + symbol] = newTrStNFA(newState);
} else {
//printf("addTrStNFA2 %i %i %c\n", state, newState, indexSymbolArray[symbol]);
TrStNFA* trSt = transitionTableNFA[state * symbolMaxSize + symbol];
while(trSt->altState != NULL){
if(trSt->state == newState) return;
//printf("addTrStNFA3 %%i %i %c\n", state, newState, indexSymbolArray[symbol]);
trSt = trSt->altState;
}
trSt->altState = newTrStNFA(newState);
}
}
void reallocTransitionTableNFA(int oldStateMaxSize, int oldSymbolMaxSize){
//printf("reallocTrStTable\n");
TrStNFA** newTransitionTableNFA = malloc(symbolMaxSize * stateMaxSize * sizeof(TrStNFA*));
for(int i = 0; i < stateMaxSize; i++){
for(int j = 0; j < symbolMaxSize; j++){
if(i < oldStateMaxSize && j < oldSymbolMaxSize){
newTransitionTableNFA[i * symbolMaxSize + j] = transitionTableNFA[i * oldSymbolMaxSize + j];
} else {
newTransitionTableNFA[i * symbolMaxSize + j] = NULL;
}
}
}
free(transitionTableNFA);
transitionTableNFA = newTransitionTableNFA;
}
int increaseSymbolCounter(){
symbolCounter++;
if(symbolCounter >= symbolMaxSize){
int oldSymbolMaxSize = symbolMaxSize;
symbolMaxSize *= 2;
reallocTransitionTableNFA(stateMaxSize, oldSymbolMaxSize);
}
return symbolCounter;
}
int increaseStateCounter(){
stateCounter++;
if(stateCounter >= stateMaxSize){
int oldStateMaxSize = stateMaxSize;
stateMaxSize *= 2;
reallocTransitionTableNFA(oldStateMaxSize, symbolMaxSize);
int* newFinalState = malloc(stateMaxSize * sizeof(int));
for(int i = 0; i < oldStateMaxSize; i++) newFinalState[i] = finalState[i];
for(int i = oldStateMaxSize; i < stateMaxSize; i++) newFinalState[i] = -1;
free(finalState);
finalState = newFinalState;
int** newPrognosticState = malloc(stateMaxSize * sizeof(int));
for(int i = 0; i < oldStateMaxSize; i++) newPrognosticState[i] = prognosticState[i];
for(int i = oldStateMaxSize; i < stateMaxSize; i++) newPrognosticState[i] = NULL;
free(prognosticState);
prognosticState = newPrognosticState;
}
return stateCounter;
}
int increaseActionCounter(){
actionCounter++;
if(actionCounter >= actionMaxSize){
int oldActionMaxSize = actionMaxSize;
actionMaxSize *= 2;
char** newAction = malloc(sizeof(char*)*actionMaxSize);
for(int i = 0; i < oldActionMaxSize; i++) newAction[i] = action[i];
for(int i = oldActionMaxSize; i < actionMaxSize; i++) newAction[i] = NULL;
free(action);
action = newAction;
int* newPrognosticFinalState = malloc(sizeof(int)*actionMaxSize);
for(int i = 0; i < oldActionMaxSize; i++) newPrognosticFinalState[i] = prognosticFinalState[i];
for(int i = oldActionMaxSize; i < actionMaxSize; i++) newPrognosticFinalState[i] = -1;
free(prognosticFinalState);
prognosticFinalState = newPrognosticFinalState;
}
return stateCounter;
}
void regexp2NFA(Node*);
void prognostic2NFA(Node* node){
//printf("positiveClosure2NFA\n");
regexp2NFA(node->chNode1);
int fstChNode2State = stateCounter;
addTrStNFA(fstChNode2State, 1, increaseStateCounter());
prognosticFlag = 1;
regexp2NFA(node->chNode2);
prognosticFlag = 0;
}
void positiveClosure2NFA(Node* node){
//printf("positiveClosure2NFA\n");
int startState = stateCounter;
regexp2NFA(node->chNode1);
int lstChNodeState = stateCounter;
addTrStNFA(lstChNodeState, 0, startState);
currentFinalState = lstChNodeState;
}
void kleeneClosure2NFA(Node* node){
//printf("kleeneClosure2NFA\n");
int startState = stateCounter;
int scdState = increaseStateCounter();
addTrStNFA(startState, 0, scdState);
regexp2NFA(node->chNode1);
int lstChNodeState = stateCounter;
addTrStNFA(lstChNodeState, 0, scdState);
increaseStateCounter();
addTrStNFA(lstChNodeState, 0, stateCounter);
addTrStNFA(startState, 0, stateCounter);
currentFinalState = stateCounter;
}
void concat2NFA(Node* node){
//printf("concat2NFA\n");
regexp2NFA(node->chNode1);
regexp2NFA(node->chNode2);
currentFinalState = stateCounter;
}
void union2NFA(Node* node){
//printf("union2NFA\n");
int startState = stateCounter;
int fstChNode1State = increaseStateCounter();
regexp2NFA(node->chNode1);
int lstChNode1State = stateCounter;
int fstChNode2State = increaseStateCounter();
regexp2NFA(node->chNode2);
int lstChNode2State = stateCounter;
addTrStNFA(startState, 0, fstChNode1State);
addTrStNFA(startState, 0, fstChNode2State);
increaseStateCounter();
addTrStNFA(lstChNode1State, 0, stateCounter);
addTrStNFA(lstChNode2State, 0, stateCounter);
currentFinalState = stateCounter;
}
void symbol2NFA(Node* node){
//printf("symbol2NFA\n");
int symbol = node->token->tag;
if(symbolIndexArray[symbol] == -1){
symbolIndexArray[symbol] = increaseSymbolCounter();
indexSymbolArray[symbolCounter] = symbol;
}
int startState = stateCounter;
increaseStateCounter();
addTrStNFA(startState, symbolIndexArray[symbol], stateCounter);
if(prognosticFlag) prognosticState[stateCounter] = &prognosticFinalState[actionCounter];
currentFinalState = stateCounter;
}
void regexp2NFA(Node* node){
//printf("regexp2NFA\n");
switch(node->tag){
case SYMBOL: symbol2NFA(node); break;
case CONCAT: concat2NFA(node); break;
case UNION: union2NFA(node); break;
case KLEENE_CLOSURE: kleeneClosure2NFA(node); break;
case POSITIVE_CLOSURE: positiveClosure2NFA(node); break;
case PROGNOSTIC: prognostic2NFA(node); break;
}
}
void convertorRegexp2NFAInitialize(){
symbolMaxSize = DEF_SYM_MAX_SIZE;
stateMaxSize = DEF_STA_MAX_SIZE;
actionMaxSize = DEF_ACT_MAX_SIZE;
for(int i = 0; i < 256; i++) symbolIndexArray[i] = -1;
for(int i = 0; i < 258; i++) indexSymbolArray[i] = -1;
indexSymbolArray[0] = E_TRANS;
increaseSymbolCounter();
indexSymbolArray[1] = E_TRANS;
transitionTableNFA = malloc(symbolMaxSize * stateMaxSize * sizeof(TrStNFA*));
finalState = malloc(stateMaxSize * sizeof(int));
prognosticState = malloc(stateMaxSize * sizeof(int));
for(int i = 0; i < stateMaxSize * symbolMaxSize; i++) transitionTableNFA[i] = NULL;
for(int i = 0; i < stateMaxSize; i++) finalState[i] = -1;
for(int i = 0; i < stateMaxSize; i++) prognosticState[i] = NULL;
action = malloc(sizeof(char*) * DEF_ACT_MAX_SIZE);
prognosticFinalState = malloc(sizeof(int) * DEF_ACT_MAX_SIZE);
}
TrStNFA** convertRegexp2NFA(NodeAction* nodeAction){
addTrStNFA(0, 0, ++stateCounter);
regexp2NFA(nodeAction->node);
finalState[currentFinalState] = actionCounter;
action[actionCounter] = nodeAction->action;
prognosticFinalState[actionCounter] = currentFinalState;
increaseActionCounter();
return transitionTableNFA;
}
/********************** NFA printing *****************************/
void printNFA(TrStNFA** trStNFA){
TrStNFA* tmpTrStNFA;
printf(" ");
for(int i = 0; i <= symbolCounter; i++){
printf("%c ", indexSymbolArray[i]);
}
printf("\n");
for(int i = 0; i <= stateCounter; i++){
printf("%i ", i);
for(int j = 0; j <= symbolCounter; j++){
if( trStNFA[i * symbolMaxSize + j] == NULL ){
printf("N ");
continue;
}
tmpTrStNFA = trStNFA[i * symbolMaxSize + j];
printf("{");
while(tmpTrStNFA->altState != NULL){
printf("%i ", tmpTrStNFA->state);
tmpTrStNFA = tmpTrStNFA->altState;
}
printf("%i", tmpTrStNFA->state);
printf("} ");
}
printf("\n");
}
}