-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathnfa.ts
212 lines (181 loc) · 4.52 KB
/
nfa.ts
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
export interface IState {
isEnd: boolean;
transition: Object;
epsilonTransition: IState[];
}
export interface INfa {
start: IState;
end: IState;
}
/**
* State in Thompson's NFA can either have
- a single symbol transition to a state
or
- up to two epsilon transitions to another states
but not both.
*/
function createState(isEnd: boolean): IState {
return {
isEnd,
transition: {},
epsilonTransition: [],
};
}
function addEpsilonTransition(from: IState, to: IState): void {
from.epsilonTransition.push(to);
}
/**
* Thompson's NFA state can have only one transition to another state for a given symbol.
*/
function addTransition(from: IState, to: IState, symbol: string): void {
// @ts-ignore
from.transition[symbol] = to;
}
/**
* Construct an NFA that recognizes only the empty string.
* @returns start and end state
*/
function fromEpsilon(): INfa {
const start = createState(false);
const end = createState(true);
addEpsilonTransition(start, end);
return { start, end };
}
/**
* Construct an NFA that recognizes only a single character string.
* @param symbol string
* @returns
*/
function fromSymbol(symbol: string): INfa {
const start = createState(false);
const end = createState(true);
addTransition(start, end, symbol);
return { start, end };
}
/**
* Concatenating two NFA's
* @param first - Starting NFA
* @param second - Ending NFA
* @returns Concatenated NFA
*/
function concat(first: INfa, second: INfa): INfa {
addEpsilonTransition(first.end, second.start);
first.end.isEnd = false;
return {
start: first.start,
end: second.end,
};
}
/**
* Union of two NFAs
* @param first
* @param second
* @returns Unionized NFA
*/
function union(first: INfa, second: INfa): INfa {
const start = createState(false);
addEpsilonTransition(start, first.start);
addEpsilonTransition(start, second.start);
const end = createState(true);
addEpsilonTransition(first.end, end);
first.end.isEnd = false;
addEpsilonTransition(second.end, end);
second.end.isEnd = false;
return {
start,
end,
};
}
/**
* Kleene Closure
* @param first NFA
* @returns NFA
*/
function closure(first: INfa) {
const start = createState(false);
const end = createState(true);
addEpsilonTransition(start, end);
addEpsilonTransition(start, first.start);
addEpsilonTransition(first.end, end);
addEpsilonTransition(first.end, first.start);
first.end.isEnd = false;
return {
start,
end,
};
}
/**
* Converts a postfix regular expression into a Thompson NFA
* @param postFixExp string
* @returns NFA
*/
export function toNFA(postFixExp: string): INfa {
if (postFixExp === "") {
return fromEpsilon();
}
const stack: any[] = [];
for (const token of postFixExp) {
if (token === "*") {
stack.push(closure(stack.pop()));
} else if (token === "|") {
const right = stack.pop();
const left = stack.pop();
stack.push(union(left, right));
} else if (token === ".") {
const right = stack.pop();
const left = stack.pop();
stack.push(concat(left, right));
} else {
stack.push(fromSymbol(token));
}
}
return stack.pop();
}
/*
Process a string through an NFA by recurisively (depth-first) traversing all the possible paths until finding a matching one.
The NFA has N states, from each state it can go to at most N possible states, yet there might be at most 2^N possible paths,
therefore, worst case it'll end up going through all of them until it finds a match (or not), resulting in very slow runtimes.
*/
export function recursiveBacktrackingSearch(
state: IState,
visited: IState[],
input: string,
position: number
) {
if (visited.includes(state)) {
return false;
}
visited.push(state);
if (position === input.length) {
if (state.isEnd) {
return true;
}
if (
state.epsilonTransition.some((s) =>
recursiveBacktrackingSearch(s, visited, input, position)
)
) {
return true;
}
} else {
// @ts-ignore
const nextState: IState = state.transition[input[position]];
if (nextState) {
if (recursiveBacktrackingSearch(nextState, [], input, position + 1)) {
return true;
}
} else {
if (
state.epsilonTransition.some((s) =>
recursiveBacktrackingSearch(s, visited, input, position)
)
) {
return true;
}
}
return false;
}
}
export function recognize(nfa: INfa, word: string) {
return recursiveBacktrackingSearch(nfa.start, [], word, 0);
}