-
Notifications
You must be signed in to change notification settings - Fork 22
/
parse.py
202 lines (167 loc) · 5.51 KB
/
parse.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
# regex engine in Python
# parser and classes
# xiayun.sun@gmail.com
# 06-JUL-2013
import pdb
class Token:
def __init__(self, name, value):
self.name = name
self.value = value
def __str__(self):
return self.name + ":" + self.value
class Lexer:
def __init__(self, pattern):
self.source = pattern
self.symbols = {'(':'LEFT_PAREN', ')':'RIGHT_PAREN', '*':'STAR', '|':'ALT', '\x08':'CONCAT', '+':'PLUS', '?':'QMARK'}
self.current = 0
self.length = len(self.source)
def get_token(self):
if self.current < self.length:
c = self.source[self.current]
self.current += 1
if c not in self.symbols.keys(): # CHAR
token = Token('CHAR', c)
else:
token = Token(self.symbols[c], c)
return token
else:
return Token('NONE', '')
class ParseError(Exception):pass
'''
Grammar for regex:
regex = exp $
exp = term [|] exp {push '|'}
| term
| empty?
term = factor term chain {add \x08}
| factor
factor = primary [*] star {push '*'}
| primary [+] plus {push '+'}
| primary [?] optional {push '?'}
| primary
primary = \( exp \)
| char literal {push char}
'''
class Parser:
def __init__(self, lexer):
self.lexer = lexer
self.tokens = []
self.lookahead = self.lexer.get_token()
def consume(self, name):
if self.lookahead.name == name:
self.lookahead = self.lexer.get_token()
elif self.lookahead.name != name:
raise ParseError
def parse(self):
self.exp()
return self.tokens
def exp(self):
self.term()
if self.lookahead.name == 'ALT':
t = self.lookahead
self.consume('ALT')
self.exp()
self.tokens.append(t)
def term(self):
self.factor()
if self.lookahead.value not in ')|':
self.term()
self.tokens.append(Token('CONCAT', '\x08'))
def factor(self):
self.primary()
if self.lookahead.name in ['STAR', 'PLUS', 'QMARK']:
self.tokens.append(self.lookahead)
self.consume(self.lookahead.name)
def primary(self):
if self.lookahead.name == 'LEFT_PAREN':
self.consume('LEFT_PAREN')
self.exp()
self.consume('RIGHT_PAREN')
elif self.lookahead.name == 'CHAR':
self.tokens.append(self.lookahead)
self.consume('CHAR')
class State:
def __init__(self, name):
self.epsilon = [] # epsilon-closure
self.transitions = {} # char : state
self.name = name
self.is_end = False
class NFA:
def __init__(self, start, end):
self.start = start
self.end = end # start and end states
end.is_end = True
def addstate(self, state, state_set): # add state + recursively add epsilon transitions
if state in state_set:
return
state_set.add(state)
for eps in state.epsilon:
self.addstate(eps, state_set)
def pretty_print(self):
'''
print using Graphviz
'''
pass
def match(self,s):
current_states = set()
self.addstate(self.start, current_states)
for c in s:
next_states = set()
for state in current_states:
if c in state.transitions.keys():
trans_state = state.transitions[c]
self.addstate(trans_state, next_states)
current_states = next_states
for s in current_states:
if s.is_end:
return True
return False
class Handler:
def __init__(self):
self.handlers = {'CHAR':self.handle_char, 'CONCAT':self.handle_concat,
'ALT':self.handle_alt, 'STAR':self.handle_rep,
'PLUS':self.handle_rep, 'QMARK':self.handle_qmark}
self.state_count = 0
def create_state(self):
self.state_count += 1
return State('s' + str(self.state_count))
def handle_char(self, t, nfa_stack):
s0 = self.create_state()
s1 = self.create_state()
s0.transitions[t.value] = s1
nfa = NFA(s0, s1)
nfa_stack.append(nfa)
def handle_concat(self, t, nfa_stack):
n2 = nfa_stack.pop()
n1 = nfa_stack.pop()
n1.end.is_end = False
n1.end.epsilon.append(n2.start)
nfa = NFA(n1.start, n2.end)
nfa_stack.append(nfa)
def handle_alt(self, t, nfa_stack):
n2 = nfa_stack.pop()
n1 = nfa_stack.pop()
s0 = self.create_state()
s0.epsilon = [n1.start, n2.start]
s3 = self.create_state()
n1.end.epsilon.append(s3)
n2.end.epsilon.append(s3)
n1.end.is_end = False
n2.end.is_end = False
nfa = NFA(s0, s3)
nfa_stack.append(nfa)
def handle_rep(self, t, nfa_stack):
n1 = nfa_stack.pop()
s0 = self.create_state()
s1 = self.create_state()
s0.epsilon = [n1.start]
if t.name == 'STAR':
s0.epsilon.append(s1)
n1.end.epsilon.extend([s1, n1.start])
n1.end.is_end = False
nfa = NFA(s0, s1)
nfa_stack.append(nfa)
def handle_qmark(self, t, nfa_stack):
n1 = nfa_stack.pop()
n1.start.epsilon.append(n1.end)
nfa_stack.append(n1)