-
Notifications
You must be signed in to change notification settings - Fork 1
/
Scanner.py
49 lines (44 loc) · 1.02 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
class Scanner:
LP = 'LeftP'
RP = 'RightP'
STAR = 'Star'
ALTER = 'Alter'
WS = 'WhiteSpace'
TAB = 'Tab'
NL = 'Newline'
CHAR = 'Char'
DOT = 'Dot'
CAT = 'Cat'
BS = 'BackSlash'
token_exprs = [
('(', LP),
(')', RP),
('*', STAR),
('|', ALTER),
('.', DOT),
(' ', WS),
('\\', BS),
('\t', CHAR),
('\n', CHAR),
# ('\d', CHAR),
# ('\D', CHAR),
# ('\w', CHAR),
# ('\W', CHAR),
('abcdefghijklmnopqrstuvwxyz'
'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
'0123456789', CHAR)
]
def __init__(self, inputs):
self.inputs = inputs
self.tokens = []
def lex(self):
for w in self.inputs:
for t in self.token_exprs:
if w in t[0]:
self.tokens.append((w, t[1]))
def syms(self):
res = set()
for val, tag in self.tokens:
if tag == self.CHAR:
res.add(val)
return res