forked from xenteros/ZDPYTpol9
-
Notifications
You must be signed in to change notification settings - Fork 0
/
302-custom-regex-parser.py
74 lines (59 loc) · 2.15 KB
/
302-custom-regex-parser.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
"""
Napisz własny silnik, który będzie w stanie sprawdzić, czy dany napis pasuje do wyrażenia regularnego.
Trochę ustaleń:
1. Nie robimy maszynki do wyszukiwania pasujących napisów w długim tekście, a maszynkę do sprawdzania
czy dany napis w całości pasuje do wyrażenia
2. Ograniczamy się do następujących elementów składni wyrażenia regularnego:
* wzorce dosłowne (np. a, b, abc, foo)
* modyfikatory liczby wystąpień: + i *
[*]2 klasy znaków: [a-z] oraz [A-Z]
"""
#todo gdzieś trzeba zrobić sprawdzenie czy wierzchołek sukcesu się świeci
class Node:
def __init__(self, green):
self.transitions = dict()
self.activated = False
self.is_green = green
def activate(self):
self.activated = True
def deactivate(self):
self.activated = False
def transition(self, letter):
pass
#todo
def add_transition(self, letter, node): # trzeba się zastanowić, czy node jest tutaj potrzebny czy nie
pass
#todo
def split_regex(regex):
splits = []
non_spliting = ['+', "*"]
inside_brackets = False
for i in range(len(regex)):
if inside_brackets:
if regex[i] == ']':
splits.append(i+1)
inside_brackets = False
continue
if i < len(regex) - 1 and regex[i+1] in non_spliting:
continue
if regex[i] == '[':
inside_brackets = True
else:
splits.append(i+1)
groups = []
beginning = 0
for split in splits:
group = regex[beginning:split]
groups.append(group)
beginning = split
return groups
def create_automata(regex):
pass
#todo sprawdzenie czy regex jest poprawny - zawiera tylko te symbole na które się zgodziliśmy w poleceniu
#todo pocięcie regexa na wierzchołki (transitions) ab+c -> [a, b+, c]
#todo stworzenie całego automatu z odpowiednimi wierzchołkami
if __name__ == '__main__':
test = ['abc', 'ab+c', 'ab[a-z][A-Z]d']
print(split_regex(test[1]))
# for t in test:
# print(t, split_regex(t))