-
Notifications
You must be signed in to change notification settings - Fork 0
/
Lexer.py
261 lines (177 loc) · 3.3 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
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
import ply.lex as lex
import ply.yacc as yacc
import sys
import os
tokens = [
'INT', # Done
'DOUBLE', # Done
'CHAR', # Done
'STRING', # Done
'TRUE', # Done
'FALSE', # Done
'EQUALS', # Done
'PLUS', # Done
'MINUS', # Done
'DIVIDE', # Done
'MULTIPLY', # Done
'POWER', # Done
'MODULUS', # Done
'INCREMENT', # Done
'DECREMENT', # Done
'LESSTHAN', # Done
'GREATERTHAN', # Done
'LESSTHANEQUAL', # Done
'GREATERTHANEQUAL', # Done
'NOTEQUAL', # Done
'DOUBLEEQUAL', # Done
'NOT', # Done
'AND', # Done
'OR', # Done
'SEMI', # Done
'LBRACE', # Done
'RBRACE', # Done
'LPAREN', # Done
'RPAREN', # Done
'PRINT', # Done
'IF', # Done
'ELSEIF', # Done
'ELSE', # Done
'DEFINT',
'DEFDOUBLE',
'DEFCHAR',
'DEFSTRING',
'DEFBOOL',
'FINDSTRING',
'FINDCHAR',
'COMMA',
'STRUCT',
'DOT',
]
states = (('eolcomment', 'exclusive'), ('delimitedcomment', 'exclusive'), )
def t_eolcomment(t):
r'//'
t.lexer.begin('eolcomment')
def t_eolcomment_end(t):
r'\n'
t.lexer.begin('INITIAL')
def t_delimitedcomment(t):
r'/\*'
t.lexer.begin('delimitedcomment')
def t_delimitedcomment_end(t):
r'\*/'
t.lexer.begin('INITIAL')
t_delimitedcomment_ignore = r'.'
t_eolcomment_ignore = r'.'
def t_eolcomment_error(t):
t.lexer.skip(1)
def t_delimitedcomment_error(t):
t.lexer.skip(1)
t_PLUS = r'\+'
t_MINUS = r'\-'
t_MULTIPLY = r'\*'
t_DIVIDE = r'\/'
t_POWER = r'\^'
t_MODULUS = r'\%'
t_EQUALS = r'\='
t_INCREMENT = r'\+\+'
t_DECREMENT = r'\-\-'
t_LESSTHAN = r'\<'
t_GREATERTHAN = r'\>'
t_NOT = r'\!'
t_AND = r'\&'
t_OR = r'\|'
t_LPAREN = r'\('
t_RPAREN = r'\)'
t_COMMA = r'\,'
t_DOT = r'\.'
t_ignore = ' \t\v\r'
def t_LESSTHANEQUAL(t):
r'<='
return t
def t_GREATERTHANEQUAL(t):
r'>='
return t
def t_NOTEQUAL(t):
r'!='
return t
def t_DOUBLEEQUAL(t):
r'=='
return t
def t_IF(t):
r'if'
return t
def t_ELSEIF(t):
r'elseif'
return t
def t_ELSE(t):
r'else'
return t
def t_PRINT(t):
r'print'
return t
def t_DEFINT(t):
r'int'
return t
def t_DEFDOUBLE(t):
r'double'
return t
def t_DEFCHAR(t):
r'char'
return t
def t_DEFSTRING(t):
r'string'
return t
def t_DEFBOOL(t):
r'bool'
return t
def t_STRUCT(t):
r'struct'
return t
def t_DOUBLE(t):
r'-?\d+\.\d+'
t.value = float(t.value)
return t
def t_INT(t):
r'-?\d+'
t.value = int(t.value)
return t
def t_TRUE(t):
r'(true)'
t.value = True
return t
def t_FALSE(t):
r'(false)'
t.value = False
return t
def t_STRING(t):
r'[a-zA-Z][a-zA-Z_0-9]+'
t.type = 'STRING'
return t
def t_CHAR(t):
r'[a-zA-Z]'
t.type = 'CHAR'
return t
def t_LBRACE(t):
r'{'
return t
def t_RBRACE(t):
r'}'
return t
def t_SEMI(t):
r';'
return t
def t_FINDSTRING(t):
r'"(.*?)"'
t.value = t.value[1:-1]
return t
def t_FINDCHAR(t):
r"'[a-zA-Z]'"
t.value = t.value[1:-1]
return t
def t_error(t):
print("Illegal character '{0}' at line {1}".format(t.value[0], t.lineno))
t.lexer.skip(1)
def t_newline(t):
r'\n+'
t.lexer.lineno += len(t.value)
lexer = lex.lex()