-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRegExp.py
228 lines (184 loc) · 6.29 KB
/
RegExp.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
from modules.color_print import print_blue, print_green, print_purple, print_red, print_yellow, ANSI_RED, ANSI_RESET
STAR = 'STAR'
CONCAT = 'CONCAT'
OR = 'OR'
QSNMRK = 'QSNMRK'
PLUS = 'PLUS'
class RegExp:
def __init__(self, exp_list, operators, star=STAR):
self.exp_list = exp_list
self.cat_list = []
self.star = star
self.operators = operators
self.post_list = []
def handle_exp_2(self):
exp_list = []
exp = self.exp_list
op = self.operators
while len(exp) > 1:
if len(exp) > 1:
x, y = exp[0], exp[1]
if y not in op:
if (x not in op) or (x == STAR) or (x == PLUS) or (x == QSNMRK) or (x == ")"):
exp_list.append(exp.pop(0))
exp_list.append("CONCAT")
else:
exp_list.append(exp.pop(0))
else:
# y is operator
if (x == STAR) or (x == PLUS) or (x == QSNMRK):
exp_list.append(exp.pop(0))
exp_list.append("CONCAT")
elif (x not in op) and y == '(':
exp_list.append(exp.pop(0))
exp_list.append("CONCAT")
else:
exp_list.append(exp.pop(0))
exp_list.append(exp.pop(0))
self.cat_list = exp_list
self.operators.add('CONCAT')
return exp_list
def handle_exp(self):
exp_list = []
exp = self.exp_list
op = self.operators
STAR = 'STAR'
CONCAT = 'CONCAT'
OR = "OR"
PLUS = "PLUS"
QSNMRK = "QSNMRK"
LBRKT = "("
RBRKT = ")"
while len(exp) > 1:
if len(exp) > 1:
x = exp[0]
y = exp[1]
if (x == STAR) or (x == PLUS) or (x == QSNMRK):
if (y == STAR) or (y == PLUS) or (y == QSNMRK) or (y == LBRKT):
exp_list.append(exp.pop(0))
exp_list.append("CONCAT")
elif y not in op:
exp_list.append(exp.pop(0))
exp_list.append("CONCAT")
else:
exp_list.append(exp.pop(0))
elif x == OR:
if y == LBRKT:
exp_list.append(exp.pop(0))
elif (y == STAR) or (y == PLUS) or (y == QSNMRK) or (y == OR) or (y == RBRKT):
print("Error!")
else:
exp_list.append(exp.pop(0))
elif x == LBRKT:
if (y == STAR) or (y == PLUS) or (y == QSNMRK) or (y == OR):
print("Error!")
else:
exp_list.append(exp.pop(0))
elif x == RBRKT:
if (y == LBRKT) or (y not in op):
exp_list.append(exp.pop(0))
exp_list.append("CONCAT")
else:
exp_list.append(exp.pop(0))
elif x not in op:
# x is character
if (y == LBRKT) or (y not in op):
exp_list.append(exp.pop(0))
exp_list.append("CONCAT")
else:
exp_list.append(exp.pop(0))
exp_list.append(exp.pop(0))
self.cat_list = exp_list
self.operators.add('CONCAT')
return exp_list
def get_postfix(self):
"""
opstack = []
output = []
new_ip = input.split(" ")
for i in new_ip:
if i in operand:
output.append(i)
elif i == "(":
opstack.push(i)
elif i == ")":
x = TOS(opstack)
while(x != ")":
x = opstack.pop(i)
if x !=")":
output.append(i)
elif i is operator:
x = TOS(opstack)
y = compare(x,i)
while y:
output.append(y)
x = TOS(opstack)
y = compare(x,i)
opstack.push(i)
"""
opstack = []
output = []
operators = self.operators
exp = self.cat_list
for i in exp:
if i == "(":
opstack.append(i)
elif i == ')':
# pop stack til pop = (
while opstack:
p = opstack.pop(-1)
if p == '(':
break
output.append(p)
elif i not in operators:
# if it is an operand
output.append(i)
else:
while opstack:
p_stack = self.get_precedence(opstack[-1])
p_current = self.get_precedence(i)
if p_current < p_stack:
break
output.append(opstack.pop(-1))
opstack.append(i)
while opstack:
output.append(opstack.pop(-1))
return output
def get_precedence(self, op):
STAR = "STAR"
PLUS = "PLUS"
QSTMRK = "QSTMRK"
CONCAT = "CONCAT"
OR = "OR"
if (op == STAR) or (op == PLUS) or (op == QSNMRK):
return 2
elif (op == CONCAT):
return 3
elif (op == OR):
return 4
else:
return 50
def compare(self, i, opstack):
if not opstack:
return True
return self.get_precedence(i) < self.get_precedence(opstack[-1])
def postfix_me(exp_dict, operators):
"""
consider removing operators (redundant)
"""
tuple_list = []
"""
for k, v in ((k, exp_dict[k]) for k in reversed(exp_dict)):
r = RegExp(v, operators, STAR)
exp2 = r.handle_exp()
print(exp2)
post = r.get_postfix()
new_dict[k] = post
"""
for key, value in exp_dict.items():
r = RegExp(value, operators, STAR)
r.handle_exp()
post = r.get_postfix()
tuple_list.insert(0, (key, post))
post_dict = dict(tuple_list)
return post_dict