-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlexer.py
66 lines (56 loc) · 1.26 KB
/
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
import ply.lex as lex
num_errors = 0
reserved = {
'and' : 'AND',
'do' : 'DO',
'else' : 'ELSE',
'while' : 'WHILE',
'then' : 'THEN',
'end' : 'END',
'if' : 'IF',
'var' : 'VAR',
'or' : 'OR',
'not' : 'NOT'
}
# List of token names
tokens = [
'ID', 'NUMBER', 'PLUS', 'MINUS', 'TIMES', 'DIVIDE',
'EQUAL', 'NOTEQUAL', 'LESSEQUAL', 'GREATEREQUAL',
'LESS', 'GREATER', 'ASSIGN', 'LPAREN', 'RPAREN',
'SEMICOLON', 'COMMA'
] + list(reserved.values())
t_PLUS = r'\+'
t_MINUS = r'-'
t_TIMES = r'\*'
t_DIVIDE = r'/'
t_EQUAL = r'=='
t_NOTEQUAL = r'~='
t_LESSEQUAL = r'<='
t_GREATEREQUAL = r'>='
t_LESS = r'<'
t_GREATER = r'>'
t_ASSIGN = r'='
t_LPAREN = r'\('
t_RPAREN = r'\)'
t_SEMICOLON = r';'
t_COMMA = r','
# Regular expressions rules
def t_ID(t):
r'[a-zA-Z_][a-zA-Z_0-9]*'
t.type = reserved.get(t.value, 'ID')
return t
def t_NUMBER(t):
r'\d+'
t.value = int(t.value)
return t
t_ignore = ' \t'
def t_newline(t):
r'\n+'
t.lexer.lineno += len(t.value)
def t_error(t):
global num_errors
print "Ilegal character '%s' at line: %d" % (t.value[0], t.lexer.lineno)
num_errors += 1
t.lexer.skip(1)
# Building lexer
lexer = lex.lex()