-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlexer.py
More file actions
executable file
·114 lines (102 loc) · 2.56 KB
/
lexer.py
File metadata and controls
executable file
·114 lines (102 loc) · 2.56 KB
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
from ply import lex
class Lexer:
tokens = [
'ID', 'INTEGERNUMBER', 'FLOATNUMBER', 'INTEGER', 'FLOAT',
'BOOLEAN', 'FUNCTION', 'TRUE', 'FALSE', 'PRINT', 'RETURN',
'MAIN', 'IF', 'ELSE', 'ELSEIF', 'WHILE', 'ON', 'WHERE', 'FOR',
'AND', 'OR', 'NOT', 'IN', 'ASSIGN', 'SUM', 'SUB', 'MUL', 'DIV',
'MOD', 'GT', 'GE', 'LT', 'LE', 'EQ', 'NE', 'LCB', 'RCB',
'LRB', 'RRB', 'LSB', 'RSB', 'SEMICOLON', 'COLON', 'COMMA', 'ERROR'
]
reserved = {
# Conditional
'if': 'IF',
'then': 'THEN',
'else': 'ELSE',
'elseif': 'ELSEIF',
# Loops
'for': 'FOR',
'while': 'WHILE',
# Types
'int': 'INTEGER',
'float': 'FLOAT',
'bool': 'BOOLEAN',
# Other Keywords
'print': 'PRINT',
'True': 'TRUE',
'False': 'FALSE',
'return': 'RETURN',
'fun': 'FUNCTION',
'main': 'MAIN',
'on': 'ON',
'and': 'AND',
'or': 'OR',
'not': 'NOT',
'in': 'IN',
'where': 'WHERE'
}
# Colons
t_SEMICOLON = r';'
t_COLON = r':'
t_COMMA = r','
# Operators
t_MUL = r'\*'
t_DIV = r'/'
t_SUM = r'\+'
t_SUB = r'\-'
t_MOD = r'%'
t_ASSIGN = r'='
t_GT = r'>'
t_GE = r'>='
t_LT = r'<'
t_LE = r'<='
t_EQ = r'=='
t_NE = r'!='
# Brackets
t_LCB = r'{'
t_RCB = r'}'
t_LRB = r'\('
t_RRB = r'\)'
t_LSB = r'\['
t_RSB = r'\]'
def t_TRUE(self, t):
r'True'
return t
def t_FALSE(self, t):
r'False'
return t
def t_ERROR(self, t):
r"""([0-9]{10,})(\.[0-9]+)
|([0-9]{10,})
|([0-9]+)(\.[0-9]+){2,}
|([A-Z])+[a-zA-Z_0-9]+
|[0-9]+[a-z_A-Z][a-zA-Z_0-9]*
|[\+\-\%\/\*](\s*[\+\-\%\/\*]+)+
|ERROR
"""
return t
def t_newline(self, t):
r""""\n+"""
t.lexer.lineno += len(t.value)
t_ignore = '\n \t \r '
def t_FLOATNUMBER(self, t):
r"""[0-9]{1,9}\.[0-9]+"""
t.value = float(t.value)
return t
def t_INTEGERNUMBER(self, t):
r"""[0-9]{1,9}"""
t.value = int(t.value)
return t
def t_ID(self, t):
r"""[_a-z][a-zA-Z_0-9]*"""
if t.value in self.reserved:
t.type = self.reserved[t.value]
return t
def t_error(self, t):
raise Exception('Error at', t.value)
def build(self, **kwargs):
"""
build the lexer
"""
self.lexer = lex.lex(module=self, **kwargs)
return self.lexer