-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathre2lexer.py
229 lines (192 loc) · 3.87 KB
/
re2lexer.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
import ply.lex as lex
# For more information on ply look at https://www.dabeaz.com/ply/ply.html
# List of token names. This is always required
tokens = (
'ANYCHAR',
'WHITESPACE',
'WHITESPACE_COMPLEMENTED',
'DIGIT',
'DIGIT_COMPLEMENTED',
'ALPHANUMERIC',
'ALPHANUMERIC_COMPLEMENTED',
'ESCAPED',
'BEL',
'ESC',
'F_FEED',
'L_FEED',
'CARRIAGE_RET',
'TAB',
'DOLLAR',
'HEXA',
'PLUS',
'TIMES',
'OPT',
'OR',
'LPAR',
'RPAR',
'LSPAR',
'RSPAR',
'LCPAR',
'RCPAR',
'MINUS',
'HAT',
'COMMA',
'NUM',
'CHAR',
)
def t_DIGIT(t):
r'\\d'
return t
def t_DIGIT_COMPLEMENTED(t):
r'\\D'
return t
def t_ALPHANUMERIC(t):
r'\\w'
return t
def t_ALPHANUMERIC_COMPLEMENTED(t):
r'\\W'
return t
# Regular expression rules for simple tokens
# From docs: " When building the master regular expression, rules are added in the following order:
# 1) All tokens defined by functions are added in the same order as they appear in the lexer file.
# 2) Tokens defined by strings are added next by sorting them in order of decreasing
# regular expression length (longer expressions are added first).
# Without this ordering, it can be difficult to correctly match certain types of
# tokens. For example, if you wanted to have separate tokens for "=" and "==",
# you need to make sure that "==" is checked first. By sorting regular expressions
# in order of decreasing length, this problem is solved for rules defined as strings.
# ***For functions, the order can be explicitly controlled since rules appearing first are checked first*** "
# THIS AVOIDS AMBIGUITY!!
def t_ESCAPED(t):
r'\\[\\*+()?.\[\]\{\}\-\^1]'
#Adds the possibility to escape special characters.
# Note that \ has to be escaped as well inside python re so \\ stands for \
# consequently the re matches a \ followed by one of the following: '\','*','+','(',')','?','.','[',']','-', or ^ .
# Moreover note that type has been changed to 'CHAR'
t.type = 'CHAR'
t.value = t.value[1:]
return t
def t_BEL(t):
r'\\a' # BEL
t.type = 'HEXA'
t.value = 7
return t
def t_ESC(t):
r'\\e' # ESCAPE
t.type = 'HEXA'
t.value = 27
return t
def t_F_FEED(t):
r'\\f' #FORM FEED
t.type = 'HEXA'
t.value = 12
return t
def t_L_FEED(t):
r'\\n' #LINE FEED
t.type = 'HEXA'
t.value = 10
return t
def t_CARRIAGE_RET(t):
r'\\r' #CARRIAGE RETURN
t.type = 'HEXA'
t.value = 14
return t
def t_TAB(t):
r'\\t' #TAB
t.type = 'HEXA'
t.value = 9
return t
def t_VERITCAL_TAB(t):
r'\\v' #VERTICAL TAB
t.type = 'HEXA'
t.value = 9
return t
def t_DOLLAR(t):
r'\$'
return t
def t_WHITESPACE(t):
r'\\s'
return t
def t_WHITESPACE_COMPLEMENTED(t):
r'\\S'
return t
def t_LPAR(t):
r'\('
return t
def t_RPAR(t):
r'\)'
return t
def t_LSPAR(t):
r'\['
return t
def t_RSPAR(t):
r'\]'
return t
def t_LCPAR(t):
r'\{'
return t
def t_RCPAR(t):
r'\}'
return t
def t_MINUS(t):
r'-'
return t
def t_HAT(t):
r'\^'
return t
def t_OPT (t):
r'\?'
return t
def t_OR (t):
r'\|'
return t
def t_ANYCHAR(t):
r'\.'
return t
def t_PLUS(t):
r'\+'
return t
def t_TIMES(t):
r'\*'
return t
def t_HEXA( t):
r'\\x([0-9A-Fa-f][0-9A-Fa-f])?'
#print(t.value[2:])
if len(t.value) == 2:
t.value = 0
else :
t.value = int(t.value[2:], 16)
return t
def t_COMMA(t):
r','
return t
def t_NUM(t):
r'([1-9][0-9]*)|0'
return t
def t_CHAR( t):
r'[\x20-\x7F]'
#Because Ascii printable character range is \u0020 - \u007f
t.value = t.value.encode('utf-8')[0]
return t
# Error handling rule
def t_error( t):
print("Illegal character '%s'" % t.value[0], 'pos', t)
raise Exception('Regex format error')
## Build the lexer
#def build( **kwargs):
# self.lexer = lex.lex(module=self, **kwargs)
#
## Test it output
#def test(self, data):
# self.lexer.input(data)
# for tok in self.lexer:
# print(tok)
#
lexer = lex.lex()
if __name__ == "__main__":
data = "((a|b)*c?e)+"
# Give the lexer some input
lexer.input(data)
#the lexer will tokenize the inputs
for tok in lexer:
print(tok)