-
Notifications
You must be signed in to change notification settings - Fork 0
/
tokens.py
68 lines (60 loc) · 1.04 KB
/
tokens.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
reserved = {
'solve' : 'SOLVE',
}
tokens = [
'PLUS',
'MINUS',
'DEQ',
'EQ',
'NE',
'GT',
'GE',
'LT',
'LE',
'LBRACKET',
'RBRACKET',
'LPAREN',
'RPAREN',
'LBRACE',
'RBRACE',
'COMMA',
'COLON',
'DOT',
'NUM',
'ID',
]
tokens += list(reserved.values())
t_PLUS = r'\+'
t_MINUS = r'-'
t_DEQ = r'=='
t_EQ = r'='
t_NE = r'!='
t_GT = r'>'
t_GE = r'>='
t_LT = r'<'
t_LE = r'<='
t_LBRACKET = r'\['
t_RBRACKET = r'\]'
t_LPAREN = r'\('
t_RPAREN = r'\)'
t_LBRACE = r'{'
t_RBRACE = r'}'
t_COMMA = r','
t_COLON = r':'
t_DOT = r'\.'
t_ignore_COMMENT = r'\#.*'
t_ignore = ' \t\r\f\v'
def t_NUM(t):
r'\d'
t.value = int(t.value)
return t
def t_ID(t):
r'[a-zA-Z_][a-zA-Z_0-9]*'
t.type = reserved.get(t.value, 'ID')
return t
def t_NEWLINE(t):
r'\n+'
t.lexer.lineno += len(t.value)
def t_error(t):
print(f'Unrecognized character "{t.value}" at line {t.lineno}')
t.lexer.skip(1)