-
Notifications
You must be signed in to change notification settings - Fork 4
/
q1.py
246 lines (188 loc) · 6 KB
/
q1.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
# Conversion from Regex to NFA
import json
import sys
non_symbols = ['+', '*', '.', '(', ')']
nfa = {}
class charType:
SYMBOL = 1
CONCAT = 2
UNION = 3
KLEENE = 4
class NFAState:
def __init__(self):
self.next_state = {}
class ExpressionTree:
def __init__(self, charType, value=None):
self.charType = charType
self.value = value
self.left = None
self.right = None
def make_exp_tree(regexp):
stack = []
for c in regexp:
if c == "+":
z = ExpressionTree(charType.UNION)
z.right = stack.pop()
z.left = stack.pop()
stack.append(z)
elif c == ".":
z = ExpressionTree(charType.CONCAT)
z.right = stack.pop()
z.left = stack.pop()
stack.append(z)
elif c == "*":
z = ExpressionTree(charType.KLEENE)
z.left = stack.pop()
stack.append(z)
elif c == "(" or c == ")":
continue
else:
stack.append(ExpressionTree(charType.SYMBOL, c))
return stack[0]
def compPrecedence(a, b):
p = ["+", ".", "*"]
return p.index(a) > p.index(b)
def compute_regex(exp_t):
# returns E-NFA
if exp_t.charType == charType.CONCAT:
return do_concat(exp_t)
elif exp_t.charType == charType.UNION:
return do_union(exp_t)
elif exp_t.charType == charType.KLEENE:
return do_kleene_star(exp_t)
else:
return eval_symbol(exp_t)
def eval_symbol(exp_t):
start = NFAState()
end = NFAState()
start.next_state[exp_t.value] = [end]
return start, end
def do_concat(exp_t):
left_nfa = compute_regex(exp_t.left)
right_nfa = compute_regex(exp_t.right)
left_nfa[1].next_state['$'] = [right_nfa[0]]
return left_nfa[0], right_nfa[1]
def do_union(exp_t):
start = NFAState()
end = NFAState()
first_nfa = compute_regex(exp_t.left)
second_nfa = compute_regex(exp_t.right)
start.next_state['$'] = [first_nfa[0], second_nfa[0]]
first_nfa[1].next_state['$'] = [end]
second_nfa[1].next_state['$'] = [end]
return start, end
def do_kleene_star(exp_t):
start = NFAState()
end = NFAState()
starred_nfa = compute_regex(exp_t.left)
start.next_state['$'] = [starred_nfa[0], end]
starred_nfa[1].next_state['$'] = [starred_nfa[0], end]
return start, end
def arrange_transitions(state, states_done, symbol_table):
global nfa
if state in states_done:
return
states_done.append(state)
for symbol in list(state.next_state):
if symbol not in nfa['letters']:
nfa['letters'].append(symbol)
for ns in state.next_state[symbol]:
if ns not in symbol_table:
symbol_table[ns] = sorted(symbol_table.values())[-1] + 1
q_state = "Q" + str(symbol_table[ns])
nfa['states'].append(q_state)
nfa['transition_function'].append(["Q" + str(symbol_table[state]), symbol, "Q" + str(symbol_table[ns])])
for ns in state.next_state[symbol]:
arrange_transitions(ns, states_done, symbol_table)
def notation_to_num(str):
return int(str[1:])
# def final_st_dfs(st):
# global nfa
# for val in nfa['transition_function']:
# if val[0] == st and val[1] == "$" and val[2] not in nfa["final_states"]:
# nfa["final_states"].append(val[2])
# final_st_dfs(val[2])
def final_st_dfs():
global nfa
for st in nfa["states"]:
count = 0
for val in nfa['transition_function']:
if val[0] == st and val[2] != st:
count += 1
if count == 0 and st not in nfa["final_states"]:
nfa["final_states"].append(st)
def arrange_nfa(fa):
global nfa
nfa['states'] = []
nfa['letters'] = []
nfa['transition_function'] = []
nfa['start_states'] = []
nfa['final_states'] = []
q_1 = "Q" + str(1)
nfa['states'].append(q_1)
arrange_transitions(fa[0], [], {fa[0] : 1})
st_num = [notation_to_num(i) for i in nfa['states']]
nfa["start_states"].append("Q1")
# nfa["final_states"].append("Q" + str(sorted(st_num)[-1]))
# final_st_dfs(nfa["final_states"][0])
final_st_dfs()
def add_concat(regex):
global non_symbols
l = len(regex)
res = []
for i in range(l - 1):
res.append(regex[i])
if regex[i] not in non_symbols:
if regex[i + 1] not in non_symbols or regex[i + 1] == '(':
res += '.'
if regex[i] == ')' and regex[i + 1] == '(':
res += '.'
if regex[i] == '*' and regex[i + 1] == '(':
res += '.'
if regex[i] == '*' and regex[i + 1] not in non_symbols:
res += '.'
if regex[i] == ')' and regex[i + 1] not in non_symbols:
res += '.'
res += regex[l - 1]
return res
def compute_postfix(regexp):
stk = []
res = ""
for c in regexp:
if c not in non_symbols or c == "*":
res += c
elif c == ")":
while len(stk) > 0 and stk[-1] != "(":
res += stk.pop()
stk.pop()
elif c == "(":
stk.append(c)
elif len(stk) == 0 or stk[-1] == "(" or compPrecedence(c, stk[-1]):
stk.append(c)
else:
while len(stk) > 0 and stk[-1] != "(" and not compPrecedence(c, stk[-1]):
res += stk.pop()
stk.append(c)
while len(stk) > 0:
res += stk.pop()
return res
def polish_regex(regex):
reg = add_concat(regex)
regg = compute_postfix(reg)
return regg
def load_regex():
with open(sys.argv[1], 'r') as inpjson:
regex = json.loads(inpjson.read())
return regex
def output_nfa():
global nfa
with open(sys.argv[2], 'w') as outjson:
outjson.write(json.dumps(nfa, indent = 4))
if __name__ == "__main__":
r = load_regex()
reg = r['regex']
pr = polish_regex(reg)
et = make_exp_tree(pr)
fa = compute_regex(et)
arrange_nfa(fa)
output_nfa()