-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.py
391 lines (312 loc) · 10.7 KB
/
main.py
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
386
387
388
389
390
391
#!/usr/bin/env python
import sys
import pickle
from antlr4 import *
from ReGexLexer import ReGexLexer
from ReGexParser import ReGexParser
from ReGexListener import ReGexListener
from regular_expression import RegularExpression
from regex import RegEx
from nfa import NFA
from dfa import DFA
EMPTY_SET = 0
EMPTY_STRING = 1
SYMBOL = 2
STAR = 3
CONCATENATION = 4
ALTERNATION = 5
_SIMPLE_TYPES = {EMPTY_SET, EMPTY_STRING, SYMBOL}
def alphabet():
#create ER equivalent to "."
pool = "bcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
e = RegularExpression(SYMBOL, "a")
for c in pool:
e = RegularExpression(ALTERNATION, e, RegularExpression(SYMBOL, c))
return e
def rangeAlphabet(expression, tuple):
#create ER equivalent to apparition of exp "tuple" of times
e = expression
rez = RegularExpression(EMPTY_STRING)
#exact interval : minlimit_interval == maxlimit_interval
if tuple[0] == tuple[1]:
rez = expression
for i in range (1, tuple[0]):
e = RegularExpression(CONCATENATION, e, expression)
return e
#min interval : minlimit_interval == -1
elif tuple[0] == -1:
rez = RegularExpression(ALTERNATION, rez, e)
#create concatenation of exp by maxlim_interval times, starting from minlim_interval
for i in range (1, tuple[1]):
e = RegularExpression(CONCATENATION, e, expression)
rez = RegularExpression(ALTERNATION, rez, e)
#max interval : max_limit interval == -1
elif tuple[1] == -1:
#create concatenation of exp by maxlim_interval times, starting from minlim_interval
for i in range (1, tuple[0]):
e = RegularExpression(CONCATENATION, e, expression)
rez = RegularExpression(CONCATENATION, e, RegularExpression(STAR,expression))
#normal interval
else:
#create concatenation of exp by maxlim_interval times, starting from minlim_interval
for i in range (1, tuple[0]):
e = RegularExpression(CONCATENATION, e, expression)
rez = e
#add concatenation cases to reunion
for i in range (tuple[0], tuple[1]):
e = RegularExpression(CONCATENATION, e, expression)
rez = RegularExpression(ALTERNATION, rez, e)
return rez
def setAlph(interval):
#create ER equivalent to reunion of chars in set, or in the range sugested by tuples of chars
pool = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
fst = 0
for elem in interval:
#first elem of range case
if fst == 0:
fst = fst + 1
#elem in range of chars
if type(elem) is tuple:
e = RegularExpression(SYMBOL, elem[0])
for c in range(pool.find(elem[0]) + 1, pool.find(elem[1]) + 1):
e = RegularExpression(ALTERNATION, e, RegularExpression(SYMBOL, pool[c]))
#elem is one char
else:
e = RegularExpression(SYMBOL, elem)
else:
#elem in range of chars
if type(elem) is tuple:
for c in range(pool.find(elem[0]), pool.find(elem[1]) + 1):
e = RegularExpression(ALTERNATION, e, RegularExpression(SYMBOL, pool[c]))
#elem is one char
else:
e = RegularExpression(ALTERNATION, e, RegularExpression(SYMBOL, elem))
return e
def converRegToEr(expression):
#EMPTY_STRING
if expression.type == 0:
return RegularExpression(EMPTY_STRING)
#SYMBOL_SIMPLE
if expression.type == 1:
return RegularExpression(SYMBOL, expression.symbol)
#SYMBOL_ANY
if expression.type == 2:
return alphabet()
#SYMBOL_SET
if expression.type == 3:
return setAlph(expression.symbol_set)
#MAYBE
if expression.type == 4:
return RegularExpression(ALTERNATION,RegularExpression(EMPTY_STRING), converRegToEr(expression.lhs))
#STAR
if expression.type == 5:
return RegularExpression(STAR, converRegToEr(expression.lhs))
#PLUS
if expression.type == 6:
return RegularExpression(CONCATENATION,converRegToEr(expression.lhs), RegularExpression(STAR, converRegToEr(expression.lhs)))
#RANGE
if expression.type == 7:
return rangeAlphabet(converRegToEr(expression.lhs), expression.range)
#CONCATENATION
if expression.type == 8:
return RegularExpression(CONCATENATION, converRegToEr(expression.lhs), converRegToEr(expression.rhs))
#ALTERNATION
elif expression.type == 9:
return RegularExpression(ALTERNATION, converRegToEr(expression.lhs), converRegToEr(expression.rhs))
def rename_states(target, reference):
off = max(reference.states) + 1
target.start_state += off
target.states = set(map(lambda s: s + off, target.states))
target.final_states = set(map(lambda s: s + off, target.final_states))
new_delta = {}
for (state, symbol), next_states in target.delta.items():
new_next_states = set(map(lambda s: s + off, next_states))
new_delta[(state + off, symbol)] = new_next_states
target.delta = new_delta
def new_states(*nfas):
state = 0
for nfa in nfas:
m = max(nfa.states)
if m >= state:
state = m + 1
return state, state + 1
def reToNfa(expression):
#build nfa from expression
alfabet = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
#base case : empty set
if expression.type == EMPTY_SET:
return NFA(alfabet, {0, 1}, 0, {1}, {})
#base case : empty string
elif expression.type == EMPTY_STRING:
return NFA(alfabet, {0, 1}, 0, {1}, {(0, "ε"): {1}})
#base case : one char
elif expression.type == SYMBOL:
return NFA(alfabet, {0, 1}, 0, {1}, {(0, expression.symbol ): {1}})
#case concatenate
elif expression.type == CONCATENATION:
lhs_nfa = reToNfa(expression.lhs)
rhs_nfa = reToNfa(expression.rhs)
rename_states(lhs_nfa, rhs_nfa)
st1, st2 = new_states(lhs_nfa, rhs_nfa)
automata = {(st1, "ε"): {lhs_nfa.start_state},
(list(rhs_nfa.final_states)[0], "ε"): {st2},
(list(lhs_nfa.final_states)[0], "ε"): {rhs_nfa.start_state}}
automata.update(lhs_nfa.delta)
automata.update(rhs_nfa.delta)
return NFA(alfabet, {st1, st2} | lhs_nfa.states | rhs_nfa.states, st1, {st2}, automata)
#case alternate
elif expression.type == ALTERNATION:
lhs_nfa = reToNfa(expression.lhs)
rhs_nfa = reToNfa(expression.rhs)
rename_states(lhs_nfa, rhs_nfa)
st1, st2 = new_states(lhs_nfa, rhs_nfa)
automata = {(st1, "ε"): {lhs_nfa.start_state, rhs_nfa.start_state},
(list(rhs_nfa.final_states)[0], "ε"): {st2},
(list(lhs_nfa.final_states)[0], "ε"): {st2}}
automata.update(lhs_nfa.delta)
automata.update(rhs_nfa.delta)
return NFA(alfabet, {st1, st2} | lhs_nfa.states | rhs_nfa.states, st1, {st2}, automata)
#case Kleene Star
elif expression.type == STAR:
lhs_nfa = reToNfa(expression.lhs)
st1, st2 = new_states(lhs_nfa)
automata = {(st1, "ε"): {lhs_nfa.start_state, st2},
(list(lhs_nfa.final_states)[0], "ε"): {lhs_nfa.start_state, st2}}
automata.update(lhs_nfa.delta)
return NFA(alfabet, {st1, st2} | lhs_nfa.states, st1, {st2}, automata)
def getEpsilonClosuresState(s, nfa):
#return ε-Closures for state "s"
statesEpsilon = set()
#create visited vector to avoid getting stuck in loops
visited = [False for i in range(0, len(nfa.states) + 1)]
stack = []
stack.append(s)
while(len(stack)):
s = stack[-1]
stack.pop()
#check to see if state was already visited
if(not visited[s]):
visited[s] = True
statesEpsilon.add(s)
#check states reachable through "ε" transitions from new state
for elem in nfa.delta.keys():
if elem[0] == s:
if elem[1] == "ε":
nodes = nfa.delta.get(elem)
for node in nodes:
if(not visited[node]):
stack.append(node)
return statesEpsilon
def getEpsilonClosuresStates(setStates, nfa):
#return union of ε-Closures for states that are in set
statesEpsilon = set()
for s in setStates:
temp = getEpsilonClosuresState(s, nfa)
statesEpsilon.update(temp)
return tuple(list(statesEpsilon))
def move(delta, T, char):
#create tuple of states reachable from T by c transitions
statesReachable = set()
for state in T:
for elem in delta.keys():
if elem[0] == state:
if elem[1] == char:
nodes = delta.get(elem)
for node in nodes:
statesReachable.add(node)
return tuple(list(statesReachable))
def check(word, dfa):
#check if word is acceped by dfa
state = dfa.start_state
#check to find transitions from current state by current char
for j in range(0, len(word) - 1):
c = word[j]
found = 0
for elem in dfa.delta.keys():
if elem[0] == state:
if elem[1] == c:
found = 1
temp = dfa.delta.get((state, c))
if found == 0:
return False
else:
state = temp
#check to see if we've ended on a final state or not
if state in dfa.final_states:
return True
else:
return False
def nfaToDfa(nfa):
statesDfa = set()
unmarked = set()
neWDelta = {}
newFinalStates = set()
#get ε closures for start_state
eS = getEpsilonClosuresState(nfa.start_state, nfa)
#set eS as the start state for the DFA
newStartState = tuple(list(eS))
#add eS to states of DFA
statesDfa.add(tuple(list(eS)))
#set eS as unmarked by adding it to the set
unmarked.add(tuple(list(eS)))
#append state to stack
stack = []
stack.append(tuple(list(eS)))
while(len(stack)):
T = stack[-1]
stack.pop()
#check to see if state is visited
if T in unmarked:
unmarked.remove(T)
for char in nfa.alphabet:
#get "ε"-Closure for each state of set
S = getEpsilonClosuresStates(move(nfa.delta, T, char), nfa)
if len(S) != 0 :
if S not in statesDfa:
statesDfa.add(tuple(list(S)))
unmarked.add(tuple(list(S)))
stack.append(tuple(list(S)))
neWDelta[(T, char)] = S
#create final states for dfa
for S in statesDfa:
for st in S:
if st in nfa.final_states:
newFinalStates.add(S)
break
return DFA(nfa.alphabet, statesDfa, newStartState, newFinalStates, neWDelta)
if __name__ == "__main__":
valid = (len(sys.argv) == 4 and sys.argv[1] in ["RAW", "TDA"]) or \
(len(sys.argv) == 3 and sys.argv[1] == "PARSE")
if not valid:
sys.stderr.write(
"Usage:\n"
"\tpython3 main.py RAW <regex-str> <words-file>\n"
"\tOR\n"
"\tpython3 main.py TDA <tda-file> <words-file>\n"
"\tOR\n"
"\tpython3 main.py PARSE <regex-str>\n"
)
sys.exit(1)
if sys.argv[1] == "TDA":
tda_file = sys.argv[2]
with open(tda_file, "rb") as fin:
parsed_regex = pickle.loads(fin.read())
else:
regex_string = sys.argv[2]
tree = ReGexParser(CommonTokenStream(ReGexLexer(InputStream(regex_string)))).exp()
parsed_regex = (ReGexListener()).exitExp(tree)
if sys.argv[1] == "PARSE":
print(str(parsed_regex))
sys.exit(0)
#convert to ER
regularExpression = converRegToEr(parsed_regex)
#construct NFA fro ER
nfaFromEr = reToNfa(regularExpression)
#convert NFA to DFA
dfaFromEr = nfaToDfa(nfaFromEr)
with open(sys.argv[3], "r") as fin:
content = fin.readlines()
for word in content:
#check each word from file
print(check(word, dfaFromEr))
pass