-
Notifications
You must be signed in to change notification settings - Fork 10
/
parser.py
executable file
·531 lines (445 loc) · 17.8 KB
/
parser.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
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
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
#!/usr/bin/env python3
# -----------------------------------------------------------------------------
# parser.py
#
# Author: Ahmad Alhour (aalhour.com).
# Date: July 13th, 2016.
# Description: The Parser module. Implements syntax analysis and parsing rules
# of the COOL CFG.
# -----------------------------------------------------------------------------
import ply.yacc as yacc
import pycoolc.ast as AST
from pycoolc.lexer import make_lexer
class PyCoolParser(object):
"""
PyCoolParser class.
Responsible for Syntax Analysis of COOL Programs. Implements the LALR(1) parsing algorithm.
To use the parser, create an object from it and then call the parse() method passing in COOL Program(s) code as a
string.
PyCoolParser provides the following public APIs:
* build(): Builds the parser.
* parse(): Parses a COOL program source code passed as a string.
"""
def __init__(self,
build_parser=True,
debug=False,
write_tables=True,
optimize=True,
outputdir="",
yacctab="pycoolc.yacctab",
debuglog=None,
errorlog=None):
"""
Initializer.
:param debug: Debug mode flag.
:param optimize: Optimize mode flag.
:param outputdir: Output directory of parser output; by default the .out file goes in the same directory.
:param debuglog: Debug log file path; by default parser prints to stderr.
:param errorlog: Error log file path; by default parser print to stderr.
:param build_parser: If this is set to True the internal parser will be built right after initialization,
which makes it convenient for direct use. If it's set to False, then an empty parser instance will be
initialized and the parser object will have to be built by calling parser.build() after initialization.
Example:
parser = PyCoolParser(build_parser=False)
...
parser.build()
parser.parse(...)
...
:return: None
"""
# Initialize self.parser and self.tokens to None
self.tokens = None
self.lexer = None
self.parser = None
self.error_list = []
# Save Flags - PRIVATE PROPERTIES
self._debug = debug
self._write_tables = write_tables
self._optimize = optimize
self._outputdir = outputdir
self._yacctab = yacctab
self._debuglog = debuglog
self._errorlog = errorlog
# Build parser if build_parser flag is set to True
if build_parser is True:
self.build(debug=debug, write_tables=write_tables, optimize=optimize, outputdir=outputdir,
yacctab=yacctab, debuglog=debuglog, errorlog=errorlog)
# ################################# PRIVATE ########################################
# ################################ PRECEDENCE RULES ################################
# precedence rules
precedence = (
('right', 'ASSIGN'),
('right', 'NOT'),
('nonassoc', 'LTEQ', 'LT', 'EQ'),
('left', 'PLUS', 'MINUS'),
('left', 'MULTIPLY', 'DIVIDE'),
('right', 'ISVOID'),
('right', 'INT_COMP'),
('left', 'AT'),
('left', 'DOT')
)
# ################### START OF FORMAL GRAMMAR RULES DECLARATION ####################
def p_program(self, parse):
"""
program : class_list
"""
parse[0] = AST.Program(classes=parse[1])
def p_class_list(self, parse):
"""
class_list : class_list class SEMICOLON
| class SEMICOLON
"""
if len(parse) == 3:
parse[0] = (parse[1],)
else:
parse[0] = parse[1] + (parse[2],)
def p_class(self, parse):
"""
class : CLASS TYPE LBRACE features_list_opt RBRACE
"""
parse[0] = AST.Class(name=parse[2], parent="Object", features=parse[4])
def p_class_inherits(self, parse):
"""
class : CLASS TYPE INHERITS TYPE LBRACE features_list_opt RBRACE
"""
parse[0] = AST.Class(name=parse[2], parent=parse[4], features=parse[6])
def p_feature_list_opt(self, parse):
"""
features_list_opt : features_list
| empty
"""
parse[0] = tuple() if parse.slice[1].type == "empty" else parse[1]
def p_feature_list(self, parse):
"""
features_list : features_list feature SEMICOLON
| feature SEMICOLON
"""
if len(parse) == 3:
parse[0] = (parse[1],)
else:
parse[0] = parse[1] + (parse[2],)
def p_feature_method(self, parse):
"""
feature : ID LPAREN formal_params_list RPAREN COLON TYPE LBRACE expression RBRACE
"""
parse[0] = AST.ClassMethod(name=parse[1], formal_params=parse[3], return_type=parse[6], body=parse[8])
def p_feature_method_no_formals(self, parse):
"""
feature : ID LPAREN RPAREN COLON TYPE LBRACE expression RBRACE
"""
parse[0] = AST.ClassMethod(name=parse[1], formal_params=tuple(), return_type=parse[5], body=parse[7])
def p_feature_attr_initialized(self, parse):
"""
feature : ID COLON TYPE ASSIGN expression
"""
parse[0] = AST.ClassAttribute(name=parse[1], attr_type=parse[3], init_expr=parse[5])
def p_feature_attr(self, parse):
"""
feature : ID COLON TYPE
"""
parse[0] = AST.ClassAttribute(name=parse[1], attr_type=parse[3], init_expr=None)
def p_formal_list_many(self, parse):
"""
formal_params_list : formal_params_list COMMA formal_param
| formal_param
"""
if len(parse) == 2:
parse[0] = (parse[1],)
else:
parse[0] = parse[1] + (parse[3],)
def p_formal(self, parse):
"""
formal_param : ID COLON TYPE
"""
parse[0] = AST.FormalParameter(name=parse[1], param_type=parse[3])
def p_expression_object_identifier(self, parse):
"""
expression : ID
"""
parse[0] = AST.Object(name=parse[1])
def p_expression_integer_constant(self, parse):
"""
expression : INTEGER
"""
parse[0] = AST.Integer(content=parse[1])
def p_expression_boolean_constant(self, parse):
"""
expression : BOOLEAN
"""
parse[0] = AST.Boolean(content=parse[1])
def p_expression_string_constant(self, parse):
"""
expression : STRING
"""
parse[0] = AST.String(content=parse[1])
def p_expr_self(self, parse):
"""
expression : SELF
"""
parse[0] = AST.Self()
def p_expression_block(self, parse):
"""
expression : LBRACE block_list RBRACE
"""
parse[0] = AST.Block(expr_list=parse[2])
def p_block_list(self, parse):
"""
block_list : block_list expression SEMICOLON
| expression SEMICOLON
"""
if len(parse) == 3:
parse[0] = (parse[1],)
else:
parse[0] = parse[1] + (parse[2],)
def p_expression_assignment(self, parse):
"""
expression : ID ASSIGN expression
"""
parse[0] = AST.Assignment(AST.Object(name=parse[1]), expr=parse[3])
# ######################### METHODS DISPATCH ######################################
def p_expression_dispatch(self, parse):
"""
expression : expression DOT ID LPAREN arguments_list_opt RPAREN
"""
parse[0] = AST.DynamicDispatch(instance=parse[1], method=parse[3], arguments=parse[5])
def p_arguments_list_opt(self, parse):
"""
arguments_list_opt : arguments_list
| empty
"""
parse[0] = tuple() if parse.slice[1].type == "empty" else parse[1]
def p_arguments_list(self, parse):
"""
arguments_list : arguments_list COMMA expression
| expression
"""
if len(parse) == 2:
parse[0] = (parse[1],)
else:
parse[0] = parse[1] + (parse[3],)
def p_expression_static_dispatch(self, parse):
"""
expression : expression AT TYPE DOT ID LPAREN arguments_list_opt RPAREN
"""
parse[0] = AST.StaticDispatch(instance=parse[1], dispatch_type=parse[3], method=parse[5], arguments=parse[7])
def p_expression_self_dispatch(self, parse):
"""
expression : ID LPAREN arguments_list_opt RPAREN
"""
parse[0] = AST.DynamicDispatch(instance=AST.Self(), method=parse[1], arguments=parse[3])
# ######################### PARENTHESIZED, MATH & COMPARISONS #####################
def p_expression_math_operations(self, parse):
"""
expression : expression PLUS expression
| expression MINUS expression
| expression MULTIPLY expression
| expression DIVIDE expression
"""
if parse[2] == '+':
parse[0] = AST.Addition(first=parse[1], second=parse[3])
elif parse[2] == '-':
parse[0] = AST.Subtraction(first=parse[1], second=parse[3])
elif parse[2] == '*':
parse[0] = AST.Multiplication(first=parse[1], second=parse[3])
elif parse[2] == '/':
parse[0] = AST.Division(first=parse[1], second=parse[3])
def p_expression_math_comparisons(self, parse):
"""
expression : expression LT expression
| expression LTEQ expression
| expression EQ expression
"""
if parse[2] == '<':
parse[0] = AST.LessThan(first=parse[1], second=parse[3])
elif parse[2] == '<=':
parse[0] = AST.LessThanOrEqual(first=parse[1], second=parse[3])
elif parse[2] == '=':
parse[0] = AST.Equal(first=parse[1], second=parse[3])
def p_expression_with_parenthesis(self, parse):
"""
expression : LPAREN expression RPAREN
"""
parse[0] = parse[2]
# ######################### CONTROL FLOW EXPRESSIONS ##############################
def p_expression_if_conditional(self, parse):
"""
expression : IF expression THEN expression ELSE expression FI
"""
parse[0] = AST.If(predicate=parse[2], then_body=parse[4], else_body=parse[6])
def p_expression_while_loop(self, parse):
"""
expression : WHILE expression LOOP expression POOL
"""
parse[0] = AST.WhileLoop(predicate=parse[2], body=parse[4])
# ######################### LET EXPRESSIONS ########################################
def p_expression_let(self, parse):
"""
expression : let_expression
"""
parse[0] = parse[1]
def p_expression_let_simple(self, parse):
"""
let_expression : LET ID COLON TYPE IN expression
| nested_lets COMMA LET ID COLON TYPE
"""
parse[0] = AST.Let(instance=parse[2], return_type=parse[4], init_expr=None, body=parse[6])
def p_expression_let_initialized(self, parse):
"""
let_expression : LET ID COLON TYPE ASSIGN expression IN expression
| nested_lets COMMA LET ID COLON TYPE ASSIGN expression
"""
parse[0] = AST.Let(instance=parse[2], return_type=parse[4], init_expr=parse[6], body=parse[8])
def p_inner_lets_simple(self, parse):
"""
nested_lets : ID COLON TYPE IN expression
| nested_lets COMMA ID COLON TYPE
"""
parse[0] = AST.Let(instance=parse[1], return_type=parse[3], init_expr=None, body=parse[5])
def p_inner_lets_initialized(self, parse):
"""
nested_lets : ID COLON TYPE ASSIGN expression IN expression
| nested_lets COMMA ID COLON TYPE ASSIGN expression
"""
parse[0] = AST.Let(instance=parse[1], return_type=parse[3], init_expr=parse[5], body=parse[7])
# ######################### CASE EXPRESSION ########################################
def p_expression_case(self, parse):
"""
expression : CASE expression OF actions_list ESAC
"""
parse[0] = AST.Case(expr=parse[2], actions=parse[4])
def p_actions_list(self, parse):
"""
actions_list : actions_list action
| action
"""
if len(parse) == 2:
parse[0] = (parse[1],)
else:
parse[0] = parse[1] + (parse[2],)
def p_action_expr(self, parse):
"""
action : ID COLON TYPE ARROW expression SEMICOLON
"""
parse[0] = (parse[1], parse[3], parse[5])
# ######################### UNARY OPERATIONS #######################################
def p_expression_new(self, parse):
"""
expression : NEW TYPE
"""
parse[0] = AST.NewObject(parse[2])
def p_expression_isvoid(self, parse):
"""
expression : ISVOID expression
"""
parse[0] = AST.IsVoid(parse[2])
def p_expression_integer_complement(self, parse):
"""
expression : INT_COMP expression
"""
parse[0] = AST.IntegerComplement(parse[2])
def p_expression_boolean_complement(self, parse):
"""
expression : NOT expression
"""
parse[0] = AST.BooleanComplement(parse[2])
# ######################### THE EMPTY PRODUCTION ###################################
def p_empty(self, parse):
"""
empty :
"""
parse[0] = None
# ######################### PARSE ERROR HANDLER ####################################
def p_error(self, parse):
"""
Error rule for Syntax Errors handling and reporting.
"""
if parse is None:
print("Error! Unexpected end of input!")
else:
error = "Syntax error! Line: {}, position: {}, character: {}, type: {}".format(
parse.lineno, parse.lexpos, parse.value, parse.type)
self.error_list.append(error)
self.parser.errok()
# ################### END OF FORMAL GRAMMAR RULES SPECIFICATION ####################
# ################################# PUBLIC #######################################
def build(self, **kwargs):
"""
Builds the PyCoolParser instance with yaac.yaac() by binding the lexer object and its tokens list in the
current instance scope.
:param kwargs: yaac.yaac() config parameters, complete list:
* debug: Debug mode flag.
* optimize: Optimize mode flag.
* debuglog: Debug log file path; by default parser prints to stderr.
* errorlog: Error log file path; by default parser print to stderr.
* outputdir: Output directory of parsing output; by default the .out file goes in the same directory.
:return: None
"""
# Parse the parameters
if kwargs is None or len(kwargs) == 0:
debug, write_tables, optimize, outputdir, yacctab, debuglog, errorlog = \
self._debug, self._write_tables, self._optimize, self._outputdir, self._yacctab, self._debuglog, \
self._errorlog
else:
debug = kwargs.get("debug", self._debug)
write_tables = kwargs.get("write_tables", self._write_tables)
optimize = kwargs.get("optimize", self._optimize)
outputdir = kwargs.get("outputdir", self._outputdir)
yacctab = kwargs.get("yacctab", self._yacctab)
debuglog = kwargs.get("debuglog", self._debuglog)
errorlog = kwargs.get("errorlog", self._errorlog)
# Build PyCoolLexer
self.lexer = make_lexer(debug=debug, optimize=optimize, outputdir=outputdir, debuglog=debuglog,
errorlog=errorlog)
# Expose tokens collections to this instance scope
self.tokens = self.lexer.tokens
# Build yacc parser
self.parser = yacc.yacc(module=self, write_tables=write_tables, debug=debug, optimize=optimize,
outputdir=outputdir, tabmodule=yacctab, debuglog=debuglog, errorlog=errorlog)
def parse(self, program_source_code: str) -> AST.Program:
"""
Parses a COOL program source code passed as a string.
:param program_source_code: string.
:return: Parser output.
"""
if self.parser is None:
raise ValueError("Parser was not build, try building it first with the build() method.")
return self.parser.parse(program_source_code)
# -----------------------------------------------------------------------------
#
# Parser as a Standalone Python Program
# Usage: ./parser.py cool_program.cl
#
# -----------------------------------------------------------------------------
def make_parser(**kwargs) -> PyCoolParser:
"""
Utility function.
:return: PyCoolParser object.
"""
a_parser = PyCoolParser(**kwargs)
a_parser.build()
return a_parser
if __name__ == '__main__':
import sys
parser = make_parser()
if len(sys.argv) > 1:
if not str(sys.argv[1]).endswith(".cl"):
print("Cool program source code files must end with .cl extension.")
print("Usage: ./parser.py program.cl")
exit()
input_file = sys.argv[1]
with open(input_file, encoding="utf-8") as file:
cool_program_code = file.read()
parse_result = parser.parse(cool_program_code)
print(parse_result)
else:
print("PyCOOLC Parser: Interactive Mode.\r\n")
while True:
try:
s = input('COOL >>> ')
except EOFError:
break
if not s:
continue
result = parser.parse(s)
if result is not None:
print(result)