-
Notifications
You must be signed in to change notification settings - Fork 0
/
lexer.py
65 lines (53 loc) · 904 Bytes
/
lexer.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
from ply import lex
reserved = {
'and': 'AND',
'as': 'AS',
'between': 'BETWEEN',
'from': 'FROM',
'in': 'IN',
'is': 'IS',
'like': 'LIKE',
'not': 'NOT',
'null': 'NULL',
'or': 'OR',
'select': 'SELECT',
'where': 'WHERE'
}
tokens = [
'COMMA',
'DOT',
'EQ',
'GE',
'GT',
'INTEGER',
'LE',
'LP',
'LT',
'NAME',
'NE',
'RP',
'SEMICOLON',
'STRING'
] + list(reserved.values())
t_COMMA = ','
t_DOT = '\.'
t_EQ = '='
t_GE = '>='
t_GT = '>'
t_INTEGER = '[0-9]+'
t_LE = '<='
t_LP = '\('
t_LT = '<'
t_NE = '!='
t_RP = '\)'
t_SEMICOLON = ';'
t_STRING = '\'(?:[^\']|\'\')*\''
def t_NAME(t):
"""[a-zA-Z_][a-zA-Z0-9_]*"""
t.type = reserved.get(t.value.lower(), 'NAME')
return t
t_ignore = ' \t\n'
def t_error(t):
print("Illegal character '%s'" % t.value[0])
t.lexer.skip(1)
lexer = lex.lex()