-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmy_lexer.py
79 lines (67 loc) · 1.45 KB
/
my_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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
from ply.lex import lex
# define reserved word
reserved = {
'if': 'IF',
'else': 'ELSE',
'then': 'THEN',
'rec' : 'REC'
}
# All tokens must be named in advance.
tokens = (
# lambda
"LAMBDA",
# constants
'NAT', 'ID',
# binary operator
'PLUS', 'MINUS', 'TIMES', 'DIVIDE', 'MOD',
# conparison
'LT', 'LE', 'GT', 'GE', 'EQ', 'NEQ',
# conditional
'IF', 'THEN', 'ELSE',
# recursive function
'REC'
)
# Ignored characters
t_ignore = ' \t'
# literals
literals = ['(', ')', '.']
# Token matching rules are written as regexs
t_LAMBDA = r'\\'
t_PLUS = r'\+'
t_MINUS = r'-'
t_TIMES = r'\*'
t_DIVIDE = r'/'
t_MOD = r'%'
t_LE = r'<='
t_LT = r'<'
t_GE = r'>='
t_GT = r'>'
t_EQ = r'=='
t_NEQ = r'!='
# A function can be used if there is an associated action.
# Write the matching regex in the docstring.
def t_NAT(t):
r'\d+'
t.value = int(t.value)
return t
def t_ID(t):
r'[a-zA-Z_][a-zA-Z_0-9]*'
# 检查是不是关键词,如果不是,那么默认返回,NAME,
t.type = reserved.get(t.value, 'ID')
return t
# Error handler for illegal characters
def t_error(t):
print(f'Illegal character {t.value[0]!r}')
t.lexer.skip(1)
lexer = lex()
# Build & Test
if __name__ == '__main__':
lexer = lex()
while True:
s = input(">>>")
lexer.input(s)
while True:
tok = lexer.token()
if not tok:
break
print(tok)