-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTask_1
172 lines (134 loc) · 4.33 KB
/
Task_1
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
class ParseError(Exception):
pass
#Набор входных параметров
class ReX:
def __init__(self, name, value):
self.name = name
self.value = value
def __str__(self):
return self.name + ":" + self.value
#Scanner для возвращения токенов из входных регулярных выражений
class Scanner:
def __init__(self, inp_pattern):
self.regexpr = inp_pattern
self.symbols = {'(':'LEFT_PAREN', ')':'RIGHT_PAREN', '*':'STAR', '|':'ALT', '\x08':'CONCAT'}
self.current = 0
self.length = len(self.regexpr)
self.alphabet = ["a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"]
def get_token(self):
if self.current < self.length:
c = self.regexpr[self.current]
self.current += 1
if c not in self.symbols.keys():
if c not in self.alphabet:
raise ParseError
else:
token = ReX('CHAR', c)
else:
token = ReX(self.symbols[c], c)
return token
else:
return ReX('NONE', '')
def show_pattern(self):
print(self.regexpr)
#Создает список токенов из регулярного выражения
class Parser:
def __init__(self, scanner):
self.scanner = scanner
self.token_list = []
self.lookahead_pointer = self.scanner.get_token()
def shift(self, name):
if self.lookahead_pointer.name == name:
self.lookahead_pointer = self.scanner.get_token()
elif self.lookahead_pointer.name != name:
raise ParseError
def parse(self):
self.check_union()
return self.token_list
def check_union(self):
self.check_concat()
if self.lookahead_pointer.name == 'ALT':
t = self.lookahead_pointer
self.shift('ALT')
self.check_union()
self.token_list.append(t)
def check_concat(self):
self.check_closure()
if self.lookahead_pointer.value not in ')|':
self.check_concat()
self.token_list.append(ReX('CONCAT', '\x08'))
def check_closure(self):
self.check_char()
if self.lookahead_pointer.name in ['STAR']:
self.token_list.append(self.lookahead_pointer)
self.shift(self.lookahead_pointer.name)
def check_char(self):
if self.lookahead_pointer.name == 'LEFT_PAREN':
self.shift('LEFT_PAREN')
self.check_union()
self.shift('RIGHT_PAREN')
elif self.lookahead_pointer.name == 'CHAR':
self.token_list.append(self.lookahead_pointer)
self.shift('CHAR')
def show_token_list(self):
print("Token List: ", end="")
for elem in self.token_list:
print(elem.__str__(), end=" ")
#Тестовые примеры:
def test_1():
regexp = "a|b*"
my_scan = Scanner(regexp)
my_parser = Parser(my_scan)
my_parser.parse()
my_scan.show_pattern()
my_parser.show_token_list()
print("\n\n")
def test_2():
regexp = ""
my_scan = Scanner(regexp)
my_parser = Parser(my_scan)
my_parser.parse()
my_scan.show_pattern()
my_parser.show_token_list()
print("\n\n")
def test_3():
regexp = "(cd*|bha)*jk"
my_scan = Scanner(regexp)
my_parser = Parser(my_scan)
my_parser.parse()
my_scan.show_pattern()
my_parser.show_token_list()
print("\n\n")
def test_4():
regexp = "ytut(a|g)*lk"
my_scan = Scanner(regexp)
my_parser = Parser(my_scan)
my_parser.parse()
my_scan.show_pattern()
my_parser.show_token_list()
print("\n\n")
def test_5():
try:
regexp = "01('=34567)"
my_scan = Scanner(regexp)
my_parser = Parser(my_scan)
my_parser.parse()
my_scan.show_pattern()
my_parser.show_token_list()
print("\n\n")
except ParseError:
print("test 5: Incorrect regexp")
def test_6():
regexp = "(z*)"
my_scan = Scanner(regexp)
my_parser = Parser(my_scan)
my_parser.parse()
my_scan.show_pattern()
my_parser.show_token_list()
print("\n\n")
test_1()
test_2()
test_3()
test_4()
test_5()
test_6()