-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathregular.js
More file actions
382 lines (341 loc) · 8.11 KB
/
regular.js
File metadata and controls
382 lines (341 loc) · 8.11 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
const fs = require("fs");
// let fileName = "substring.json";
// let parsedFile = JSON.parse(fs.readFileSync(fileName, "utf8"));
// console.log(parsedFile);
//
// console.log(EvaluateNFA(parsedFile));
function EvaluateDFA(d) {
let state = d["initial_state"];
const input = d["input"].split("");
input.forEach(inputChar => {
state = d["delta"][state][inputChar];
});
return (d["accept_states"].indexOf(state) != -1);
}
const E_DEPTH = 10;
function EvaluateNFA(n) {
let states = [n["initial_state"]];
const input = n["input"].split("");
input.forEach(inputChar => {
let statebuff = [];
for (let i = 0; i < E_DEPTH; i++) {
statebuff = [];
states.forEach(st => {
const e = n["delta"][st]["e"];
if (e && !statebuff.includes(e) && !states.includes(e)) {
statebuff = statebuff.concat(e);
}
});
//states = states.concat(statebuff);
statebuff.forEach(i => {
if (!states.includes(i)) {
states.push(i);
}
});
}
statebuff = [];
// st
//console.log(inputChar + " | " + states);
states.forEach(st => {
const buf = n["delta"][st][inputChar];
if (buf) statebuff = statebuff.concat(buf);
});
states = statebuff;
});
for (let i = 0; i < E_DEPTH; i++) {
statebuff = [];
//console.log(states);
states.forEach(st => {
const e = n["delta"][st]["e"];
if (e && !statebuff.includes(e) && !states.includes(e)) {
statebuff = statebuff.concat(e)
}
});
//states = states.concat(statebuff);
statebuff.forEach(i => {
if (!states.includes(i)) {
states.push(i);
}
});
}
//console.log("===");
//console.log(states);
return n["accept_states"].some(r => states.includes(r));
}
function from_concat(regexstr) {
let nfa = {
"initial_state": [0],
"states": regexstr.length + 1,
"delta": {
"0": {
"e": [1]
}
}
}
let staten = 1
regexstr.split("").forEach(character => {
let newState = {}
newState[character] = [staten+1]
nfa["delta"][staten] = newState;
staten += 1;
})
nfa["accept_states"] = [staten];
return nfa;
}
const alpha = []
for (let i = 48; i <= 57; i++) {
alpha.push(String.fromCharCode(i));
}
for (let i = 65; i <= 90; i++) {
alpha.push(String.fromCharCode(i));
}
for (let i = 97; i <= 122; i++) {
alpha.push(String.fromCharCode(i));
}
const res = {
"+": [1, "l"],
"&": [2, "l"],
"*": [3, "l"]
}
const prec = function (op) {
return res[op] ? res[op] : 0;
};
const notBasic = ["*", "+", "&", "(", ")"];
const ops = ["+", "*", "&"];
function blankNFA() {
let nfa = {
"initial_state": [1],
"states": 2,
"alphabet": alpha,
"accept_states": [0],
"delta": {
"0": {
"e": [1]
},
}
};
return nfa;
}
function regexToPolish(regex) {
let tokens = regex.split("");
//console.log(tokens);
let output = [];
let opstack = [];
let buffer = [];
for (let i = 0; i < tokens.length; i++) {
let curr = tokens[i];
let peek = opstack[opstack.length-1];
if (!notBasic.includes(curr)) {
// basic
// buffer.push(curr);
// ext = next token
let ext = tokens[i+1];
// if ext exists and its not a basic char
if (ext) {
if (ops.includes(ext)) {
// + or *
if (buffer.length > 0) {
output.push(buffer.join(""));
buffer = []
}
}
if (notBasic.includes(ext)) {
buffer.push(curr)
output.push(buffer.join(""));
buffer = []
} else {
buffer.push(curr)
}
} else if (!ext) {
buffer.push(curr)
output.push(buffer.join(""));
}
} else if (ops.includes(curr)) {
// operator
let p = prec(curr);
let topp = prec(peek);
while (((topp[0] > p[0]) || (topp[0] == p[0] && topp[1] == "l")) && peek != "(") {
output.push(opstack.pop());
// re-peek
peek = opstack[opstack.length-1];
topp = prec(peek);
}
opstack.push(curr)
} else if (curr == "(") {
// left
opstack.push(curr);
} else if (curr == ")") {
// right
while (peek != "(") {
output.push(opstack.pop());
peek = opstack[opstack.length-1];
}
if (peek == "(") {
opstack.pop(); // discard
}
}
}
while (opstack.length > 0) {
output.push(opstack.pop());
}
return output
}
function RegexToNFA2(regex, implicit) {
console.log(regex);
let stack
if (implicit) {
stack = regexToPolish(implicitToExplicitConcat(regex));
} else {
stack = regexToPolish(regex);
}
console.log(stack);
return regexRecursive(stack);
}
function baseNFA(r, st) {
let nfa = {
"initial_state": [st],
"states": 4,
"accept_states": [st+3],
"delta": {
}
};
for (let i = 0; i < 4; i++) {
nfa["delta"][st+i] = {};
}
nfa["delta"][st]["e"] = [st+1];
nfa["delta"][st+1][r] = [st+2];
nfa["delta"][st+2]["e"] = [st+3];
nfa["delta"][st+3] = {};
staten += 4;
return nfa;
}
function kleen(base) {
let end = base["accept_states"].slice();
let start = base["initial_state"].slice();
let newDelta = base["delta"];
// consider just linking the start and end up with epsilons directly, instead
// of trying to add another couple states.
//
if (newDelta[start[0]]["e"]) {
newDelta[start[0]]["e"].push(end[0]);
} else {
newDelta[start[0]]["e"] = [end[0]];
}
if (newDelta[end[0]]["e"]) {
newDelta[end[0]]["e"].push(start[0]);
} else {
newDelta[end[0]]["e"] = [start[0]];
}
let nfa = {
"initial_state": start,
"states": base["states"],
"accept_states": end,
"delta": newDelta,
}
return nfa;
}
function prepend(base, prefix) {
let nfa = {
"initial_state": prefix.initial_state,
"states": base.states + prefix.states,
"accept_states": base.accept_states,
"delta": {
}
};
let newDelta = prefix["delta"];
let keys = Object.keys(base["delta"]);
for (let i = 0; i < keys.length; i++) {
newDelta[keys[i]] = base["delta"][keys[i]];
}
if (newDelta[prefix["accept_states"][0]]["e"]) {
newDelta[prefix["accept_states"][0]]["e"].push(base["initial_state"][0]);
} else {
newDelta[prefix["accept_states"][0]]["e"] = [base["initial_state"][0]];
}
nfa["delta"] = newDelta;
return nfa;
}
function union(l1, l2) {
let nfa = {
"initial_state": l1["initial_state"],
"states": l1["states"] + l2["states"],
"accept_states": l1["accept_states"],
"delta": {}
}
let newDelta = l1["delta"]
let keys = Object.keys(l2["delta"]);
for (let obj in keys) {
newDelta[keys[obj]] = l2["delta"][keys[obj]];
}
if (newDelta[l1["initial_state"][0]]["e"]) {
newDelta[l1["initial_state"][0]]["e"].push(l2["initial_state"]);
} else {
newDelta[l1["initial_state"][0]]["e"] = [l2["initial_state"]];
}
//console.log(l2["accept_states"])
if (newDelta[l2["accept_states"]]["e"]) {
newDelta[l2["accept_states"]]["e"].push(l1["accept_states"])
} else {
newDelta[l2["accept_states"]]["e"] = l1["accept_states"]
}
nfa["delta"] = newDelta;
return nfa;
}
var staten = 0
function regexRecursive(tokenStack) {
//base case
//recursive case
console.log(tokenStack);
console.log(tokenStack.length);
let curr = tokenStack.pop();
console.log(tokenStack)
if (curr == "*") {
return kleen(regexRecursive(tokenStack));
} else if (curr == "&") {
let l1 = regexRecursive(tokenStack);
let l2 = regexRecursive(tokenStack);
return prepend(l1, l2);
} else if (curr == "+") {
let l1 = regexRecursive(tokenStack);
let l2 = regexRecursive(tokenStack);
return union(l1, l2);
} else {
return baseNFA(curr, staten);
}
}
function implicitToExplicitConcat(regex) {
console.log(regex)
let tokens = regex.split("");
let copy = tokens.slice();
let offset = 0;
for (let i = 0; i < tokens.length-1; i++) {
let curr = tokens[i];
let peek = tokens[i+1];
if (peek != "*" && peek != "+" && peek != "&" && peek != ")" && curr != "+" && curr != "(") {
copy.splice(i + 1 + offset++, 0, "&");
}
}
return copy.join("");
}
function logNFA(nfa) {
console.log("Initial state: \t" + nfa["initial_state"]);
console.log("Accept states: \t" + nfa["accept_states"]);
console.log("Total states: \t" + nfa["states"]);
let keys = Object.keys(nfa["delta"]);
keys.forEach((k) => {
let obj = nfa["delta"][k];
Object.keys(obj).forEach(k2 => {
console.log(k + " | " + k2 + " | " + obj[k2]);
})
})
}
let b = RegexToNFA2("(hello)+(home)", true);
b["input"] = "helo";
console.log(b);
logNFA(b);
console.log("---");
console.log(EvaluateNFA(b));
module.exports = {
EvaluateDFA,
EvaluateNFA
}