-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmyLex.py
91 lines (71 loc) · 1.22 KB
/
myLex.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
import ply.lex as lex
reserved = {
'head' : 'HEAD',
'endhead' : 'ENDHEAD',
'end' : 'END',
'if' : 'IF',
'else' : 'ELSE',
'while' : 'WHILE',
'do' : 'DO',
'print' : 'PRINT',
'string' : "STRING",
'input' : 'INPUT'
}
tokens = (
'NUM',
'ID',
'EQ',
'NEQ',
'MOREEQ',
'LESSEQ'
) + tuple(reserved.values())
literals = ['(',')','{','}','[',']','+','-','*','/','%','=','>','<','!','%',',','&','|','"']
t_EQ = r'=='
t_NEQ = r'!='
t_MOREEQ = r'>='
t_LESSEQ = r'<='
def t_HEAD(t):
r'HEAD'
return t
def t_ENDHEAD(t):
r'ENDHEAD'
return t
def t_PRINT(t):
r'print'
return t
def t_STRING(t):
r'\"[^"]*\" '
return t
def t_INPUT(t):
r'input'
return t
def t_IF(t):
r'if'
return t
def t_ELSE(t):
r'else'
return t
def t_WHILE(t):
r'while'
return t
def t_DO(t):
r'do'
return t
def t_END(t):
r'END'
return t
def t_NUM(t):
r'-?\d+'
t.value = int(t.value)
return t
def t_ID(t):
r'[a-zA-Z0-9]+'
return t
def t_newline(t):
r'\n+'
t.lexer.lineno += len(t.value)
def t_error(t):
print("Illegal character '%s'" % t.value[0])
t.lexer.skip(1)
t_ignore = ' \r\t'
lexer = lex.lex()