-
Notifications
You must be signed in to change notification settings - Fork 0
/
lexer.py
49 lines (41 loc) · 794 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
reserved = {
'print' : 'PRINT'
}
tokens = [
'INT',
'ADD',
'SUB',
'NAME',
'LPAREN',
'RPAREN',
'NL',
'EQ'
] + list(reserved.values())
t_ignore = ' \t'
t_ignore_COMMENT = r'\#[^\n\r]*'
def t_error(t):
print 'illegal character: %s' % t.value[0]
def t_INT(t):
r'\d+'
try:
t.value = int(t.value)
except ValueError:
print 'integer value too large', t.value
t.value = 0
return t
def t_NL(t):
r'[\n\r]+'
t.lexer.lineno += t.value.count('\n')
t.lexer.lineno += t.value.count('\r')
return t
def t_NAME(t):
r'[a-zA-Z_]\w*'
t.type = reserved.get(t.value, 'NAME')
return t
t_ADD = r'\+'
t_SUB = r'\-'
t_LPAREN = r'\('
t_RPAREN = r'\)'
t_EQ = r'='
import ply.lex as lex
lex.lex()