-
Notifications
You must be signed in to change notification settings - Fork 0
/
RegexProcessing.py
executable file
·713 lines (644 loc) · 27.5 KB
/
RegexProcessing.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
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
""" Static Class to clean and standardize regular expressions """
from DFA import DFA
from NFA import NFA
class Regexer:
"""A static class used to preprocess regular expressions"""
@staticmethod
def fill_operators(inp: str):
"""Fills a given regular expression with & for a definitive and more easy processing
For example, ab will be converted to a&b
Parameters
----------
inp : str
The input regular expression to be filled
Returns
-------
str
filled regular expression
"""
filled = ''
char_array = [ch for ch in inp]
it = iter(range(len(char_array) - 1))
# list of operators, between which will not be filled
operator_exemptions = ["&", "|", "*"]
escaped = False
for i in it:
cur_char = char_array[i]
filled += cur_char
# finds escaped characters with \ or !
if (cur_char == '\\' or cur_char == '!') and not escaped:
escaped = True
continue
next_char = char_array[i + 1]
# get escaped inverted character class (e.g. ![^abc]) and skip it
if escaped and char_array[i - 1] == '!' and cur_char == '[':
while next_char != ']' or (next_char == ']' and cur_char == '\\'):
filled += next_char
next(it)
i += 1
cur_char = char_array[i]
next_char = char_array[i + 1]
# add a & if a normal character follows and is not a operator
if escaped and next_char not in operator_exemptions:
escaped = False
# don't add & between character and closing brackets
if next_char != ')' and next_char != ']':
filled += '&'
continue
# ignore escaped characters
elif escaped:
escaped = False
continue
# if current and next char are not operators, add &
if cur_char not in operator_exemptions and next_char not in operator_exemptions:
# don't add & between character and opening or closing brackets
if cur_char == '(' or next_char == ')':
continue
filled += '&'
continue
# but do add a & between * and character
elif cur_char == '*' and next_char not in operator_exemptions:
# don't add & between character and opening or closing brackets
if cur_char == '(' or next_char == ')':
continue
filled += '&'
continue
filled += char_array[-1]
return filled
@staticmethod
def get_between(begin, end):
"""Gets all characters between begin and end
For example, get_between('d', 'f') returns {d, e, f}
Parameters
----------
begin : str
The starting character
end : str
The last character
Returns
-------
set
set of characters between 'begin' and 'end'
"""
num_begin = ord(begin)
num_end = ord(end)
# end is larger than begin, cannot create such a range
if num_begin > num_end:
raise Exception("Illegal character class!")
temp_set = set()
for i in range(num_begin, num_end + 1):
temp_set.add(chr(i))
return temp_set
@staticmethod
def get_characters_by_command(command):
"""Generates a character set for a regex command
Parameters
----------
command : str
the command to be converted
Returns
-------
str
character set for command
"""
char_set = set()
# not representable --> add as command
if command in ['s', 'S', 'D', 'W']:
char_set.add('!' + command)
# get all numbers
elif command == 'd':
char_set = Regexer.get_between('0', '9')
# get all numbers, letters and _
elif command == 'w':
char_set = Regexer.get_between('a', 'z')
char_set = char_set.union(Regexer.get_between('A', 'Z'))
char_set = char_set.union(Regexer.get_between('0', '9'))
char_set.add('_')
return char_set
@staticmethod
def generate_character_class(char_class):
"""Splits a character class into an or-string
This can be something like [a-c] which will be converted to (a|b|c)
Parameters
----------
char_class : str
The class of characters to be concatenated
Returns
-------
str
repeated qualifier
"""
or_set = set()
char_array = [ch for ch in char_class]
it = iter(range(len(char_array)))
# in the inversion case (e.g [^abc]), add special command for future comparison
if char_array[0] == '^':
return '![' + char_class + ']'
for i in it:
if (i + 1) < len(char_array):
next_char = char_array[i + 1]
# check for escaping
if char_array[i] == '\\':
# generates characters for special commands
if next_char in ['d', 'D', 's', 'S', 'w', 'W']:
or_set = or_set.union(Regexer.get_characters_by_command(next_char))
# ignores \e i.e. our epsilon
elif next_char != 'e':
# escape special characters
if char_array[i] in ['.', '^', '$', '+', '?', '{', '}', '[', ']', '\\', '*', '|', '(', ')']:
or_set.add('\\' + next_char)
else:
or_set.add(next_char)
next(it)
# detected a range and gets all characters between
elif next_char == '-' and not (i + 2) == len(char_array):
or_set = or_set.union(Regexer.get_between(char_array[i], char_array[i + 2]))
next(it)
next(it)
i += 2
else:
# replace space with \s
if char_array[i] == ' ':
or_set.add('!s')
else:
# escape special character
if char_array[i] in ['.', '^', '$', '+', '?', '{', '}', '[', ']', '\\', '*', '|', '(', ')']:
or_set.add('\\' + char_array[i])
else:
or_set.add(char_array[i])
else:
if not char_array[i] == ' ':
# escape special character
if char_array[i] in ['.', '^', '$', '+', '?', '{', '}', '[', ']', '\\', '*', '|', '(', ')']:
or_set.add('\\' + char_array[i])
else:
or_set.add(char_array[i])
else:
# replace space with \s
or_set.add('!s')
or_string = '('
while len(or_set) > 0:
or_string += or_set.pop()
if len(or_set) > 0:
or_string += '|'
or_string += ')'
return or_string
@staticmethod
def generate_repeated_qualifier(limits: str, previous_char):
"""Generates a repeated qualifier for 'previous_char'
This can be something liek a{2,3} which will be converted to (aa|aaa)
Parameters
----------
limits : str
The amount of repetion. Must be a singular number, a number followed by a comma or vice verca
or two numbers seperated by a comma
previous_char: str
The string that will be repeated
Returns
-------
str
repeated qualifier
"""
# singular number, repeat input n times
if not limits.__contains__(','):
repetitions = int(limits)
tmp_string = ''
for i in range(repetitions):
tmp_string += previous_char
return tmp_string
else:
# split on ,
split = limits.split(',')
lower = split[0]
upper = split[1]
infinite = False
# catches {,n}
if len(lower) == 0:
lower = 0
else:
lower = int(lower)
# catches {n,}
if len(upper) != 0:
upper = int(upper)
else:
infinite = True
if not infinite and lower > upper:
raise Exception("Lower bound is higher than upper bound!")
tmp_string = '('
# generate repetitions from lower to upper
if not infinite:
for i in range(upper - lower + 1):
if i == 0 and lower == 0:
tmp_string += r'\e'
for j in range(lower + i):
tmp_string += previous_char
if i < upper - lower:
tmp_string += '|'
# generate lower repetions and add a repetition with a star
else:
repetitions = int(lower)
for i in range(repetitions + 1):
tmp_string += previous_char
tmp_string += '*'
tmp_string += ')'
return tmp_string
@staticmethod
def escape(inp: str, strict=False):
"""Escapes all Python specific regex attributes.
Converts character classes (e.g. [a-z]), commands (e.g. \\\\d), etc.
Parameters
----------
inp : str
Unescaped regular expression, which will be escaped for further preprocessing
strict: bool
Whether to throw an error on an potential incorrectly transformed regex
Returns
-------
str
escaped regular expression
"""
# copy input, as we will trim and edit regex
regex = inp
escaped_string = ''
# '*', '|', '(' and ')' are meta characters, but are not needed to processed in a special way
meta_char = ['.', '^', '$', '+', '?', '{', '}', '[', ']', '\\']
command_char = ['d', 'D', 's', 'S', 'w', 'W']
escaped = False
# keeps track of open bracket(s) and last added characters for potential repetition
brackets = []
bracket_tracker = ''
last_bracket = ''
open_bracket = False
last_chars = ""
no_guarantee = False
# split input for iteration
char_array = [ch for ch in regex]
it = iter(range(len(char_array)))
for i in it:
char = char_array[i]
# backslash indicates escape of next char
if char == '\\':
escaped = True
continue
# current char has been escaped by previous backslash
elif escaped:
# is a command
if char in command_char:
# ! is our added special 'command' character for commands, that can not easily be represented
if char == 's':
escaped_string += '!s'
last_chars = '!s'
else:
translations = {
'd': '0-9',
's': r'\t\n\r\f\v',
'w': 'a-zA-Z0-9_',
'D': '^0-9',
'S': r'^\t\n\r\f\v',
'W': '^a-zA-Z0-9_'
}
# generate character class according to expansion of above dictionary
character_class = Regexer.generate_character_class(translations[char])
last_chars = character_class
escaped_string += character_class
# check for special character (i.e. e which signifies an epsilon (added manually, not python-esque)
else:
# ! is our added special 'command' character for commands, that can not easily be represented
if char in ['e']:
escaped_string += '!e'
last_chars = '!e'
# copy escape character, for possible \. or similar
else:
escaped_string += '\\'
escaped_string += char
last_chars = '\\' + char
if char in meta_char:
regex = regex.replace(char, '', 1)
escaped = False
# spaces cannot be handled by future NFA creation. We use or version of \s instead
elif char == ' ':
escaped_string += '!s'
last_chars = '!s'
# as we use ! as a special character, we need to escape occurences
elif char == '!':
escaped_string += '\\!'
last_chars = '\\!'
# any char, that is not special
elif char not in meta_char:
escaped_string += char
last_chars = char
if char == '(':
# if a bracket is open, store current bracket and begin a new one
if open_bracket:
brackets.append(bracket_tracker)
bracket_tracker = ''
open_bracket = True
if char == ')':
# if a bracket closes and other open brackets exist, add bracket to last open bracket
if len(brackets) > 0:
tmp = bracket_tracker
bracket_tracker = brackets.pop() + tmp
last_bracket = tmp + ')'
# else close bracket and continue with execution
else:
bracket_tracker += ')'
last_bracket = bracket_tracker
last_chars = bracket_tracker
open_bracket = False
# if an unescaped character class begins, split on ] and pass result to generation
elif char == '[':
x = regex.split(']', 1)
regex = x[1]
char_class = x[0].split('[')[1]
character_class = Regexer.generate_character_class(char_class)
last_chars = character_class
escaped_string += character_class
to_skip = len(char_class) + 1
# skip all following chars of the character class
for j in range(to_skip):
next(it)
# if an unescaped repetion qualifier begins, split on } and generate repeated qualifier
elif char == '{':
x = regex.split('}', 1)
regex = x[1]
limits = x[0].split('{')[1]
# remove previously added last_chars, as these will be readded by qualifier generation
escaped_string = escaped_string[0:-len(last_chars)]
# if there is an open bracket, remove last_chars from bracket_tracker as well
if open_bracket:
bracket_tracker = bracket_tracker[0:-len(last_chars)]
repetition = last_chars
repeated_qualifier = Regexer.generate_repeated_qualifier(limits, repetition)
last_chars = repeated_qualifier
escaped_string += repeated_qualifier
to_skip = len(limits) + 1
# skip all following chars of the repeated qualifier
for j in range(to_skip):
next(it)
# convert s+ to ss* and watch for potential open or immediately closed brackets
elif char == '+':
if last_chars != "":
if last_chars[-1] == ')' or open_bracket:
if len(last_chars) == 1:
last_chars = last_bracket
bracket_tracker += last_chars + '*'
escaped_string += last_chars + '*'
last_chars = ""
else:
escaped_string += char_array[i - 1] + '*'
# escape . (match all) with our command !
elif char == '.':
escaped_string += '!.'
last_chars = '!.'
# convert a? to (epsilon | a)
elif char == '?':
if i < len(char_array) - 1:
# skip lookahead assertions, but set no_guarantee to True, as this is not an exact transformation
if char_array[i + 1] == '!' or char_array[i + 1] == '=' or char_array[i + 1] == ':':
# ignore if lookahead assertion is not correct e.g., a?=b is not correct, while a(?=b) is
if char_array[i-1] == '(':
next(it)
if not char_array[i + 1] == ':':
no_guarantee = True
if strict:
raise Exception("Can't convert lookahead assertion with the formal defintion.")
continue
# check if last char was a singular closing bracket
if last_chars[-1] == ')' and len(last_chars) == 1:
last_chars = last_bracket
# shorten bracket_tracker as the last_chars will be added again
bracket_tracker = bracket_tracker[0:-len(last_chars)]
# check if a bracket is still open
elif open_bracket and not last_chars[-1] == ')':
bracket_tracker = bracket_tracker[0:-len(last_chars)]
# last_chars is a completed bracket
elif last_chars[-1] == ')':
bracket_tracker = bracket_tracker[0:-len(last_chars)]
escaped_string = escaped_string[0:-len(last_chars)]
# generate epsilon | a string
epsilon_or = r'(!e|' + last_chars + ')'
last_chars = epsilon_or
escaped_string += epsilon_or
# if ^ or $ exists, no guarantee can be given and use may be illegal
elif char == '^':
no_guarantee = True
if strict:
raise Exception("Can't convert beginning of line match (^).")
if not i == 0:
raise Exception("Illegal use of ^! Not at the beginning of the line.")
elif char == '$':
no_guarantee = True
if strict:
raise Exception("Can't convert end of line match ($).")
if not i == (len(char_array) - 1):
raise Exception("Illegal use of $! Not at the end of the line.")
# update bracket_tracker, if a bracket is open
if open_bracket:
bracket_tracker += last_chars
return escaped_string, no_guarantee
@staticmethod
def shunting_yard(regex, operators=None):
"""Converts a given regular expression into shunting yard form
The given regular expression needs to be escaped and have filled operators (i.e. & or | between all
characters). The shunting yard algorithm transforms a given expression from infix-notation to
postfix-notation. For example, a|b becomes ab|. See also:
https://gregorycernera.medium.com/converting-regular-expressions-to-postfix-notation-with-the-shunting-yard-algorithm-63d22ea1cf88
https://en.wikipedia.org/wiki/Shunting_yard_algorithm
Parameters
----------
regex : str
the regular expression to be transformed
operators: list
optional operator list
Returns
-------
str
transformed regular expression
"""
if operators is None:
operators = ['*', '&', '|']
# output stack
output = []
# operator stack
operator = []
escaped = False
command = False
# tracks a current capture class
capture_class = ''
for char in regex:
# check for non-escaped command symbols
if char == '!' and not escaped:
command = True
capture_class = '!'
output.append('!')
elif command:
# add command and potentially start capturing capture_class
if len(capture_class) == 1:
if not char == '[':
output.append(char)
command = False
capture_class = ''
continue
output.append('[')
capture_class += char
else:
# append finished capture class
if char == ']':
output.append(']')
command = False
capture_class = ''
# add to capture class
else:
output.append(char)
capture_class += char
# add escaped char
elif char == '\\' and not escaped:
output.append(char)
escaped = True
# append simple char or command that has been escpaed
elif (char not in operators and char != '(' and char != ')') or escaped:
output.append(char)
escaped = False
# handle operators
elif char in operators:
# empty operator stack => add operator to stack
if len(operator) == 0:
operator.append(char)
continue
while len(operator) > 0:
# always add after opening bracket
if operator[-1] == '(':
operator.append(char)
break
# pop all operators while precedence of stack operators is lower or equal
elif operators.index(operator[-1]) <= operators.index(char):
temp = operator.pop()
output.append(temp)
if len(operator) == 0:
operator.append(char)
break
# else append operator to operator stack
else:
operator.append(char)
break
# always add opening bracket to operator stack
elif char == '(':
operator.append(char)
# pop all operators, until corresponding opening bracket is found
elif char == ')':
while 1:
top = operator.pop()
if top == '(':
break
else:
output.append(top)
# remaining operator stack should be added in reverse order (i.e. order of appearance)
operator.reverse()
for op in operator:
output.append(op)
return ''.join(output)
@staticmethod
def regex(regex, steps=False, strict=False):
"""Converts a given Python-style regex string into a formal-esque regular expression definition.
Parameters
----------
regex : str
The regular expression string to be processed.
steps: bool
Whether to print sub-step results
Returns
-------
[str, bool]
[filled, no_guarantee] where 'filled' is the converted regular expression
and 'no_guarantee' indicates whether some assumptions had to be made
"""
[escaped, no_guarantee] = Regexer.escape(regex, strict)
filled = Regexer.fill_operators(escaped)
if steps:
print("Escaped string: ", escaped)
print("Operator-filled string: ", filled, "\n")
return filled, no_guarantee
@staticmethod
def full_shunting_yard(regex, steps=False, strict=False):
"""Converts a given Python-style regex string into a formal-esque regular expression definition in
Shunting-Yard form.
Parameters
----------
regex : str
The regular expression string to be processed.
steps: bool
Whether to print sub-step results
strict: bool
Whether to throw an error on an potential incorrectly transformed regex
Returns
-------
[str, bool]
[normalized, no_guarantee] where 'normalized' is the converted regular expression
and 'no_guarantee' indicates whether some assumptions had to be made
"""
[escaped, no_guarantee] = Regexer.escape(regex, strict)
filled = Regexer.fill_operators(escaped)
normalized = Regexer.shunting_yard(filled)
if steps:
print("Escaped string: ", escaped)
print("Operator-filled string: ", filled)
print("Shunting-yard string: ", normalized, "\n")
return normalized, no_guarantee
@staticmethod
def create_nfa(regex, steps=False, strict=False):
"""Converts a given Python-style regex string into a formal-esque regular expression definition.
The converted expression is then converted to an NFA.
Parameters
----------
regex : str
The regular expression string to be processed.
steps: bool
Whether to print sub-step results
strict: bool
Whether to throw an error on an potential incorrectly transformed regex
Returns
-------
[Automaton, bool]
[nfa, no_guarantee] where 'nfa' is the created NFA
and 'no_guarantee' indicates whether some assumptions had to be made
"""
[escaped, no_guarantee] = Regexer.escape(regex, strict)
filled = Regexer.fill_operators(escaped)
normalized = Regexer.shunting_yard(filled)
nfa = NFA(normalized)
if steps:
print("Escaped string: ", escaped)
print("Operator-filled string: ", filled)
print("Shunting-yard string: ", normalized, "\n")
return nfa, no_guarantee
@staticmethod
def create_dfa(regex, steps=False, strict=False):
"""Converts a given Python-style regex string into a formal-esque regular expression definition.
The converted expression is then converted to an DFA.
Parameters
----------
regex : str
The regular expression string to be processed.
steps: bool
Whether to print sub-step results
strict: bool
Whether to throw an error on an potential incorrectly transformed regex
Returns
-------
[Automaton, bool]
[dfa, no_guarantee] where 'dfa' is the created DFA
and 'no_guarantee' indicates whether some assumptions had to be made
"""
[escaped, no_guarantee] = Regexer.escape(regex, strict)
filled = Regexer.fill_operators(escaped)
normalized = Regexer.shunting_yard(filled)
nfa = NFA(normalized)
dfa = DFA(nfa.get_nfa())
if steps:
print("Escaped string: ", escaped)
print("Operator-filled string: ", filled)
print("Shunting-yard string: ", normalized)
print("NFA:")
nfa.print_nfa()
print("\n------------------\n")
return dfa, no_guarantee