-
Notifications
You must be signed in to change notification settings - Fork 0
/
lexer.py
298 lines (219 loc) · 5.54 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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
# importing the required lex module of ply library
import ply.lex as lex
# defining tokens for the lexer
tokens = [
"LEFT_BRACE", # token for left brace '{'
"RIGHT_BRACE", # token for right brace '}'
"LEFT_PAREN", # token for left paranthesis '('
"RIGHT_PAREN", # token for right paranthesis ')'
"PRODUCT", # token for product operator '*'
"DIVIDE", # token for division operator '/'
"MODULUS", # token for remainder operator '%'
"PLUS", # token for addition operator '+'
"MINUS", # token for difference operator '-'
"DOUBLE", # token for floating point numbers
"INTEGER", # token for integer numbers [0-9] +ve and -ve
"STRING", # token for strings
"IDENTIFIER", # token for variable names
"SEMICOLON", # token for statement endings
"ASSIGN", # token for assignment operator
"COMMA",
"LT",
"GT",
"LTE",
"GTE",
"EQUALS",
"INCREMENT",
"DECREMENT",
"EXP",
"DOT",
"LEFT_BRACKET",
"RIGHT_BRACKET",
"QUOTE",
]
keywords = {
"if": "IF", # keyword for if statements
"else": "ELSE", # keyword for else
"and": "AND", # logical AND
"or": "OR", # logical OR
"not": "NOT", # logical NOT
"is": "IS", # equality
"return": "RETURN", # returning values
"in": "IN",
"put": "PUT",
"for": "FOR",
"to": "TO",
"step": "STEP",
"while": "WHILE",
"do": "DO",
"structure": "STRUCT",
"object": "OBJECT",
"break": "BREAK",
"skip": "SKIP",
"log": "LOG",
"subroutine": "FUNCTION",
"let": "LET",
"less": "LESS",
"greater": "GREATER",
"than": "THAN",
"int": "TYPE_INT",
"char": "TYPE_CHAR",
"double": "TYPE_DOUBLE",
"string": "TYPE_STRING",
"bool": "TYPE_BOOL",
"true": "TRUE",
"false": "FALSE",
}
tokens += list(keywords.values())
###########################################################
######### defining regular expressions for tokens #########
###########################################################
def t_QUOTE(token):
r"\' "
return token
def t_LEFT_BRACKET(token):
r"\["
return token
def t_RIGHT_BRACKET(token):
r"\]"
return token
def t_DOT(token):
r"\."
return token
def t_EXP(token):
r"\^"
return token
def t_INCREMENT(token):
r"\+\+"
return token
def t_DECREMENT(token):
r"--"
return token
def t_GTE(token):
r">="
return token
def t_LTE(token):
r"<="
return token
def t_GT(token):
r">"
return token
def t_LT(token):
r"<"
return token
def t_COMMA(token):
r","
return token
# token specification for semi colon
def t_SEMICOLON(token):
r";"
return token
# token specification for logical AND '&&'
def t_AND(token):
r"&&"
return token
# token specification for logical OR '||'
def t_OR(token):
r"\|\|"
return token
# token specification for unary logical NOT '!'
def t_NOT(token):
r"!"
return token
# token specification for equality '=='
def t_EQUALS(token):
r"=="
return token
# token specification for assignment operator '='
def t_ASSIGN(token):
r"(\+=)|(-=)|(\*=)|(\/=)|(=)"
return token
# token specification for left brace '{'
def t_LEFT_BRACE(token):
r"\{"
return token
# token specification for right brace '}'
def t_RIGHT_BRACE(token):
r"\}"
return token
# token specification for left paranthesis '('
def t_LEFT_PAREN(token):
r"\("
return token
# token specification for right paranthesis ')'
def t_RIGHT_PAREN(token):
r"\)"
return token
# token specification for product operator '*'
def t_PRODUCT(token):
r"\*"
return token
# token specification for division operator '/'
def t_DIVIDE(token):
r"/"
return token
# token specification for modulus (remainder) operator '%'
def t_MODULUS(token):
r"%"
return token
# token specification for addition operator '+'
def t_PLUS(token):
r"\+"
return token
# token specification for difference operator '-'
def t_MINUS(token):
r"-"
return token
# token specification for floating point numbers
def t_DOUBLE(token):
r"[0-9]+\.[0-9]+"
token.value = float(token.value)
return token
# token specification for integer numbers [0-9]
def t_INTEGER(token):
r"[0-9]+"
token.value = int(token.value)
return token
# token specification for strings
def t_STRING(token):
r'["]([^"\\\n]|\\.|\\\n)*["]'
token.value = str(token.value)
token.value = token.value[1:-1]
return token
# token specification for variable names and keywords
def t_IDENTIFIER(token):
r"[_A-Za-z][_A-Za-z0-9]*"
token.type = keywords.get(token.value, "IDENTIFIER")
return token
# token specification for newline characters
def t_newline(token):
r"\n+"
token.lexer.lineno += len(token.value)
# token for ignoring white space
t_ignore = " \t"
# token for error checking during lexing
def t_error(token):
print(
"\nInvalid token encountered: '"
+ str(token.value[0])
+ "' | at:["
+ str(token.lexer.lineno)
+ ","
+ str(token.lexer.lexpos)
+ "]"
)
token.lexer.skip(1)
###########################################################
######### End of token specializations ####################
###########################################################
lexer = lex.lex()
def main():
while True:
code = input("yapl >>> ")
if code == "quit" or code == "exit":
break
lexer.input(code)
for token in lexer:
print(token)
if __name__ == "__main__":
main()