-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathScanner.py
311 lines (229 loc) · 7.75 KB
/
Scanner.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
import re
from modules.color_print import print_blue, print_green, print_purple, print_red, print_yellow, ANSI_RED, ANSI_RESET
from itertools import chain
from copy import copy, deepcopy
class Scanner:
def __init__(self, lexical_path):
self.lex_path = lexical_path
self.punctuations = set()
self.keywords = set()
self.RE = dict()
self.RD = dict()
self.buffer = ""
self.input_list = []
self.program_list = []
self.operators = {'(', ')', 'STAR', 'OR', 'PLUS','CONCAT'}
def analaze_lex(self):
self.get_buffer()
self.handle_file()
self.handle_lexical()
self.list_rules()
self.sep_RD()
def handle_file(self):
self.buffer = self.buffer.replace("\\L", '𝛆')
self.buffer = self.buffer.replace("\+", 'plusop')
self.buffer = self.buffer.replace("\*", 'mulop')
self.buffer = self.buffer.replace("+", 'PLUS')
self.buffer = self.buffer.replace("*", 'STAR')
self.buffer = self.buffer.replace("|", 'OR')
self.buffer = self.buffer.replace("plusop", '+')
self.buffer = self.buffer.replace("mulop", '*')
self.buffer = self.buffer.replace("\\", '')
self.buffer = self.buffer.replace('a-z',"( " + generate_equivalent_range("a-z") + " )")
self.buffer = self.buffer.replace('A-Z',"( " + generate_equivalent_range("A-Z") + " )")
self.buffer = self.buffer.replace('0-9',"( " + generate_equivalent_range("0-9") + " )")
self.input_list = self.buffer.split('\n')
def get_RD_list(self):
return get_value_list(self.RD)
def get_RE_list(self):
return get_value_list(self.RE)
def sep_RD(self):
# TODO: reduce complexity
for k, v in self.RD.items():
a = v
for i in v:
if i not in self.RE:
if i not in self.operators:
if len(i) > 1:
new_i = list(i)
a = list(chain.from_iterable(new_i if item == i else [item] for item in a))
self.RD[k] = a
def expand_rd(self, r):
rd_temp = copy(self.RD)
# TODO: reduce complexity
# three nested loops are horrible
for i in range(1,r):
for k,_ in rd_temp.items():
for key, value in self.RE.items():
a = rd_temp[k]
a = list(chain.from_iterable(value if item == key else [item] for item in a))
rd_temp[k] = a
self.expanded_rd = rd_temp
def handle_lexical(self):
"""
handles lexical inputs
returns Punctuation set
Keywords set
REs dict
RDs dict
"""
p,k,re,rd = sort_file(self.input_list)
RDs = {}
REs = {}
pn = set()
kw = set()
for i in p:
pn.update(handle_punctuations(i))
for i in k:
kw.update(handle_keyword(i))
for i in rd:
r = handle_rd(i)
RDs[r[0]] = r[1]
for i in re:
r = handle_re(i)
REs[r[0]] = r[1]
self.punctuations = pn
self.keywords = kw
self.RD = RDs
self.RE = REs
def postfix_keyword_punc(self):
kw_pn = self.keywords.union(self.punctuations)
kw_pn = intersperse(list(kw_pn) ,"OR")
# TODO: check the reason for removing & appending
kw_pn.remove("OR")
kw_pn.append("OR")
return kw_pn
def get_buffer(self):
file = open(self.lex_path)
self.buffer = file.read().replace("\n", "\n")
file.close()
self.buffer = self.buffer.strip()
@staticmethod
def read_file(path):
file = open(path)
buffer = file.read().replace("\n", "\n")
file.close()
buffer = buffer.strip()
return buffer
def read_program_file(self, path):
buffer = self.read_file(path)
buffer = re.sub('\s+',' ',buffer)
buffer = buffer.split(' ')
self.program_list = buffer
return buffer
def list_rules(self):
"""
make REs & RDs as lists
ex:
L = x | l --> ['x','|', 'l']
return modified RE and RD
"""
for key, value in self.RE.items():
self.RE[key] = value.split(" ")
for key, value in self.RD.items():
self.RD[key] = value.split(" ")
def sort_file(input_list):
"""
sort it to RE, RD, Keywords and Punctuations
"""
punctuations = []
keywords = []
RDs = []
REs = []
# TODO: make this more dynamic
for i in input_list:
if i.strip().startswith("{"):
keywords.append(i.strip())
elif i.strip().startswith("["):
punctuations.append(i.strip())
else:
x = re.search(r"[a-zA-Z]+[0-9]*:", i.strip())
if x:
RDs.append(i.strip())
else:
x = re.search(r"[a-zA-Z]+[0-9]* =", i.strip())
if x:
REs.append(i.strip())
return punctuations, keywords, REs, RDs
def generate_equivalent_range(str_input):
"generate ranges in ReExp"
# TODO: make range detection dynamic
if str_input == "a-z":
range_s = 'a'
range_e = 'z'
y = range_s
for j in range(ord(range_s)+1, ord(range_e)+2):
y += " OR " + chr(j)
elif str_input == "A-Z":
range_s = 'A'
range_e = 'Z'
y = range_s
for j in range(ord(range_s)+1, ord(range_e)+2):
y += " OR " + chr(j)
elif str_input == "0-9":
range_s = '0'
range_e = '9'
y = range_s
for j in range(ord(range_s)+1, ord(range_e)+2):
y += " OR " + chr(j)
return y
def handle_keyword(input_list):
# TODO: make it dymaic later
input_list = input_list.replace("{",'')
input_list = input_list.replace("}",'')
input_list = input_list.split(" ")
input_list = [i.strip() for i in input_list]
return set(input_list)
def handle_punctuations(input_list):
# TODO: make it dymaic later
input_list = input_list.replace("[",'')
input_list = input_list.replace("]",'')
input_list = input_list.split(" ")
input_list = [i.strip() for i in input_list]
return set(input_list)
def handle_rd(input_list):
input_list = input_list.strip(" ")
input_list = input_list.split(":", 1)
input_list = [i.strip() for i in input_list]
return input_list
def handle_re(input_list):
input_list = input_list.strip(" ")
input_list = input_list.split("=", 1)
input_list = [i.strip() for i in input_list]
return input_list
def list_rules(RE, RD):
"""
make REs & RDs as lists
ex:
L = x | l --> ['x','|', 'l']
return modified RE and RD
"""
for key, value in RE.items():
RE[key] = value.split(" ")
for key, value in RD.items():
RD[key] = value.split(" ")
return RE, RD
def intersperse(lst, item):
result = [item] * (len(lst) * 2 - 1)
result[0::2] = lst
return result
def get_value_list(the_dict):
""" coverts the dict into list of values
(with brackets for merging
"""
values = []
for _, value in the_dict.items():
value = add_brackets(value)
values.append(value)
return values
def add_brackets(the_list):
LBRKT = "("
RBRKT = ")"
if len(the_list) > 1:
the_list.insert(0,LBRKT)
the_list.append(RBRKT)
return the_list
def flatten_list(the_list):
flat_list = []
flat_list = [item for sublist in the_list for item in sublist]
return flat_list