-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmypl_parser.py
More file actions
384 lines (345 loc) · 14.5 KB
/
mypl_parser.py
File metadata and controls
384 lines (345 loc) · 14.5 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
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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
#!//usr/bin/python3
#
# Author: Vincent Lombardi
# Course: CPSC 326, Spring 2019
# Assignment: 4
# Description:
# this program takes in a lexer and uses it to tokenize
# commands from a file. The program then checks the tokens
# to see if they conform to mypl's grammar rules.
# --------------------------------------------------------
import mypl_error as error
import mypl_lexer as lexer
import mypl_token as token
import mypl_ast as ast
class Parser(object):
def __init__(self, lexer):
self.lexer = lexer
self.current_token = None
# starts analyzing the syntactical order of the program
def parse(self):
"""succeeds if program is syntactically well-formed"""
stmt_list_node = ast.StmtList()
self.__advance()
self.__stmts(stmt_list_node)
self.__eat(token.EOS, 'expecting end of file')
return stmt_list_node
# moves to the next character
def __advance(self):
self.current_token = self.lexer.next_token()
# checks if the next character is syntactically correct
# and advances if true. otherwise this function spits out and error
def __eat(self, tokentype, error_msg):
if self.current_token.tokentype == tokentype:
self.__advance()
else:
self.__error(error_msg)
# this function prints an error message containing
# the expected character, the character found instead
# of the expected character and the location of hte error
# in thj file
def __error(self, error_msg):
s = error_msg + ', found "' + self.current_token.lexeme + '" in parser'
l = self.current_token.line
c = self.current_token.column
raise error.MyPLError(s, l, c)
# Beginning of recursive descent functions
def __stmts(self, stmt_list_node):
""""<stmts> ::= <stmt> <stmts> | e"""
if self.current_token.tokentype != token.EOS:
self.__stmt(stmt_list_node)
self.__stmts(stmt_list_node)
def __stmt(self, stmt_list_node):
"""<stmt> ::= <sdecl> | <fdecl> | <bstmt>"""
if self.current_token.tokentype == token.STRUCTTYPE:
self.__advance()
self.__sdecl(stmt_list_node)
elif self.current_token.tokentype == token.FUN:
self.__advance()
self.__fdecl(stmt_list_node)
else:
stmt_list_node.stmts.append(self.__bstmt())
def __sdecl(self, stmt_list_node):
# 〈sdecl〉::= STRUCTTYPE ID〈vdecls〉END
var_list = []
struct_node = ast.StructDeclStmt()
struct_node.struct_id = self.current_token
self.__eat(token.ID, "expecting an ID")
self.__vdecls(var_list)
struct_node.var_decls = var_list
stmt_list_node.stmts.append(struct_node)
self.__eat(token.END, "expecting end")
def __fdecl(self, stmt_list_node):
fun_node = ast.FunDeclStmt()
fun_node.stmt_list = ast.StmtList()
if self.current_token.tokentype == token.NIL:
fun_node.return_type = self.current_token
self.__advance()
else:
fun_node.return_type = self.__type()
fun_node.fun_name = self.current_token
self.__eat(token.ID, "expecting an ID")
self.__eat(token.LPAREN, "expecting '('")
fun_node.params = self.__params()
self.__eat(token.RPAREN, "expecting ')'")
self.__bstmts(fun_node.stmt_list)
self.__eat(token.END, "expecting end")
stmt_list_node.stmts.append(fun_node) # check
def __bstmts(self, temp_stmt_list):
# this function will run unless it finds one of these tokens
tokenrel = [token.EOS, token.END, token.ELSE, token.ELIF]
if self.current_token.tokentype not in tokenrel:
temp_stmt_list.stmts.append(self.__bstmt())
self.__bstmts(temp_stmt_list)
def __bstmt(self):
if self.current_token.tokentype == token.VAR:
self.__advance()
return self.__vdecl()
elif self.current_token.tokentype == token.SET:
self.__advance()
return self.__assign()
elif self.current_token.tokentype == token.IF:
self.__advance()
return self.__cond()
elif self.current_token.tokentype == token.WHILE:
self.__advance()
return self.__while()
elif self.current_token.tokentype == token.RETURN:
self.__advance()
return self.__exit()
else:
temp_expr = self.__expr()
self.__eat(token.SEMICOLON, "expecting ';'")
return temp_expr
def __cond(self):
if_node = ast.IfStmt()
basic_if_node = ast.BasicIf()
basic_if_node.bool_expr = ast.BoolExpr()
basic_if_node.stmt_list = ast.StmtList()
self.__bexpr(basic_if_node.bool_expr)
self.__eat(token.THEN, "expecting then")
self.__bstmts(basic_if_node.stmt_list)
if_node.if_part = basic_if_node
self.__condt(if_node)
self.__eat(token.END, "expecting end")
return if_node
def __condt(self, if_node):
if_node.else_stmts = ast.StmtList()
basic_if_node = ast.BasicIf()
basic_if_node.bool_expr = ast.BoolExpr()
if self.current_token.tokentype == token.ELIF:
basic_if_node.stmt_list = ast.StmtList()
self.__advance()
self.__bexpr(basic_if_node.bool_expr)
self.__eat(token.THEN, "expecting then")
self.__bstmts(basic_if_node.stmt_list)
if_node.elseifs.append(basic_if_node)
self.__condt(if_node)
elif self.current_token.tokentype == token.ELSE:
if_node.has_else = True
self.__advance()
self.__bstmts(if_node.else_stmts)
def __bexpr(self, bexpr_node):
if self.current_token.tokentype == token.NOT:
bexpr_node.negated = True
self.__advance()
self.__bexpr(bexpr_node)
self.__bexprt(bexpr_node)
elif self.current_token.tokentype == token.LPAREN:
bexpr_node.first_expr = ast.BoolExpr()
self.__advance()
# in this specific instance bexpr_node.first_expr must be a Bool expr
self.__bexpr(bexpr_node.first_expr)
self.__eat(token.RPAREN, "expecting ')'")
self.__bconnct(bexpr_node)
else:
bexpr_node.first_expr = self.__expr()
self.__bexprt(bexpr_node)
def __bexprt(self, bexpr_node):
boolrel = [token.EQUAL, token.LESS_THAN,
token.LESS_THAN_EQUAL, token.GREATER_THAN_EQUAL,
token.GREATER_THAN, token.NOT_EQUAL]
if self.current_token.tokentype in boolrel:
bexpr_node.bool_rel = self.current_token
self.__advance()
bexpr_node.second_expr = self.__expr()
self.__bconnct(bexpr_node)
else:
self.__bconnct(bexpr_node)
def __bconnct(self, bexpr_node):
temp_bool_expr = ast.BoolExpr()
if (self.current_token.tokentype == token.AND
or self.current_token.tokentype == token.OR):
bexpr_node.bool_connector = self.current_token
self.__advance()
self.__bexpr(temp_bool_expr)
bexpr_node.rest = temp_bool_expr
def __while(self):
while_node = ast.WhileStmt()
while_node.bool_expr = ast.BoolExpr()
self.__bexpr(while_node.bool_expr)
while_node.stmt_list = ast.StmtList()
self.__eat(token.DO, "expecting do")
self.__bstmts(while_node.stmt_list)
self.__eat(token.END, "expecting end")
return while_node
def __exit(self):
return_node = ast.ReturnStmt()
if self.current_token.tokentype != token.SEMICOLON:
return_node.return_expr = self.__expr()
return_node.return_token = self.current_token
self.__eat(token.SEMICOLON, "expecting ';'")
return return_node
def __vdecls(self, var_list):
if self.current_token.tokentype == token.VAR:
self.__advance()
var_list.append(self.__vdecl())
self.__vdecls(var_list)
def __vdecl(self):
var_decl_node = ast.VarDeclStmt()
var_decl_node.var_id = self.current_token
self.__eat(token.ID, "expecting ID")
var_decl_node.var_type = self.__tdecl()
self.__eat(token.ASSIGN, "expecting '='")
var_decl_node.var_expr = self.__expr()
self.__eat(token.SEMICOLON, "expecting ';'")
return var_decl_node
def __tdecl(self):
token_type = token.NIL
if self.current_token.tokentype == token.COLON:
self.__advance()
token_type = self.__type()
return token_type
def __expr(self):
simple_expr_node = ast.SimpleExpr()
if self.current_token.tokentype == token.LPAREN:
self.__advance()
simple_expr_node.term = self.__expr()
self.__eat(token.RPAREN, "expecting ')'")
else:
simple_expr_node.term = self.__rvalue()
mathrels = [token.PLUS, token.MINUS,
token.DIVIDE, token.MULTIPLY, token.MODULO]
if self.current_token.tokentype in mathrels:
complex_expr_node = ast.ComplexExpr()
complex_expr_node.first_operand = simple_expr_node
complex_expr_node.math_rel = self.current_token
self.__advance()
complex_expr_node.rest = self.__expr()
return complex_expr_node
return simple_expr_node
def __rvalue(self):
# this is the one exception I made to advancing
# before calling the function
simple_rval_node = ast.SimpleRValue()
if self.current_token.tokentype == token.NIL:
simple_rval_node.val = self.current_token
self.__advance()
return simple_rval_node
elif self.current_token.tokentype == token.NEW:
self.__advance()
new_rval_node = ast.NewRValue()
new_rval_node.struct_type = self.current_token
self.__eat(token.ID, "expecting an ID")
return new_rval_node
elif self.current_token.tokentype == token.STRINGVAL:
simple_rval_node.val = self.current_token
self.__advance()
return simple_rval_node
elif self.current_token.tokentype == token.INTVAL:
simple_rval_node.val = self.current_token
self.__advance()
return simple_rval_node
elif self.current_token.tokentype == token.BOOLVAL:
simple_rval_node.val = self.current_token
self.__advance()
return simple_rval_node
elif self.current_token.tokentype == token.FLOATVAL:
simple_rval_node.val = self.current_token
self.__advance()
return simple_rval_node
else:
return self.idrval()
def idrval(self):
id_node = ast.IDRvalue()
call_rvalue_node = ast.CallRValue()
id_node.path.append(self.current_token)
self.__eat(token.ID, "expecting an ID")
if self.current_token.tokentype == token.DOT:
while self.current_token.tokentype == token.DOT:
self.__advance()
id_node.path.append(self.current_token)
self.__eat(token.ID, "expecting an ID")
return id_node
elif self.current_token.tokentype == token.LPAREN:
call_rvalue_node.fun = id_node.path[0]
self.__advance()
if self.current_token.tokentype != token.RPAREN:
self.__expr_list(call_rvalue_node)
self.__eat(token.RPAREN, "expecting ')'")
return call_rvalue_node
return id_node
def __expr_list(self, call_rvalue_node):
call_rvalue_node.args.append(self.__expr())
while self.current_token.tokentype == token.COMMA:
self.__advance()
call_rvalue_node.args.append(self.__expr())
def __type(self):
temp_token = ''
if self.current_token.tokentype == token.ID:
temp_token = self.current_token
self.__advance()
return temp_token
elif self.current_token.tokentype == token.INTTYPE:
temp_token = self.current_token
self.__advance()
return temp_token
elif self.current_token.tokentype == token.FLOATTYPE:
temp_token = self.current_token
self.__advance()
return temp_token
elif self.current_token.tokentype == token.BOOLTYPE:
temp_token = self.current_token
self.__advance()
return temp_token
elif self.current_token.tokentype == token.STRINGTYPE:
temp_token = self.current_token
self.__advance()
return temp_token
else:
self.__error("expecting type")
return temp_token
def __params(self):
param_list = []
param_node = ast.FunParam()
if self.current_token.tokentype == token.ID:
param_node.param_name = self.current_token
self.__advance()
self.__eat(token.COLON, "expecting ':'")
param_node.param_type = self.__type()
param_list.append(param_node)
while self.current_token.tokentype == token.COMMA:
param_node = ast.FunParam()
self.__advance()
param_node.param_name = self.current_token
self.__eat(token.ID, "expecting an ID")
self.__eat(token.COLON, "expecting ':'")
param_node.param_type = self.__type()
param_list.append(param_node)
return param_list
def __assign(self):
assign_node = ast.AssignStmt()
assign_node.lhs = self.__lvalue()
self.__eat(token.ASSIGN, "expecting '='")
assign_node.rhs = self.__expr()
self.__eat(token.SEMICOLON, "expecting ';'")
return assign_node
def __lvalue(self):
lval_node = ast.LValue()
lval_node.path.append(self.current_token)
self.__eat(token.ID, "expecting an ID")
while self.current_token.tokentype == token.DOT:
self.__advance()
lval_node.path.append(self.current_token)
self.__eat(token.ID, "expecting an ID")
return lval_node