-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcoolparser.py
1265 lines (1071 loc) · 45.6 KB
/
coolparser.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
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import sys
import shlex
import cool_lexer
from cool_lexer import Token
# init global vars
token = Token('', '', 0, 0, '')
classes = [] # keeps track of the summaries for each parsed class so far
class_methods = [] # keeps track of methods while parsing a class
errors = [] # keeps track of errors
FOLLOW = {} # stores the FOLLOW sets for each nonterminal
FIRST = {} # stores the union of the FIRST+ sets for each nonterminal's productions
def next_token(lexer):
try:
matched_token = cool_lexer.next_token(lexer)
return matched_token
except cool_lexer.LexError as le:
error = "Lexical error: unrecognized token '{0}' on line {1}, column {2}.\n".format(le.invalid_token.val,
le.invalid_token.line_no,
le.invalid_token.column_index + 1)
error += le.invalid_token.line
error += "\n"
for index, char in enumerate(le.invalid_token.line):
if index == le.invalid_token.column_index:
error += "^"
break
elif char == "\t":
error += "\t"
else:
error += " "
errors.append(error)
return next_token(lexer) # keep trying until a valid token is found
def peek_token(lexer):
try:
matched_token = cool_lexer.peek_token(lexer)
return matched_token
except cool_lexer.LexError as le:
error = "Lexical error: unrecognized token '{0}' on line {1}, column {2}.\n".format(le.invalid_token.val,
le.invalid_token.line_no,
le.invalid_token.column_index + 1)
error += le.invalid_token.line
error += "\n"
for index, char in enumerate(le.invalid_token.line):
if index == le.invalid_token.column_index:
error += "^"
break
elif char == "\t":
error += "\t"
else:
error += " "
errors.append(error)
return next_token(lexer) # keep trying until a valid token is found
def parse(filename):
with open(filename) as f:
lexer = shlex.shlex(f)
# split only on newlines
lexer.whitespace = '\r\n'
lexer.whitespace_split = True
# disable shlex quote behaviour
lexer.quotes = ''
global token
token = next_token(lexer)
if program(lexer) and len(errors) == 0:
print("No errors found")
for class_summary in classes:
print(class_summary)
else:
print("Errors found")
for error in errors:
print(error, end="\n\n")
def error_msg(incorrect_token, *expected_tokens):
error = "Parse error: unexpected token '{0}' on line {1}, column {2}. Expected one of: \n ".format(incorrect_token.val,
incorrect_token.line_no,
incorrect_token.column_index + 1)
for expected_token in expected_tokens:
error += expected_token + ", "
error = error[:-2] + "\n" # remove last comma
error += incorrect_token.line
error += "\n"
for index, char in enumerate(incorrect_token.line):
if index == incorrect_token.column_index:
error += "^"
break
elif char == "\t":
error += "\t"
else:
error += " "
return error
def recover(lexer, follow, first=None):
global token
while token.name not in first and token.name not in follow and token.name != "eof":
token = next_token(lexer)
if token.name in first:
# continue trying to parse the nt
return True
elif token.name in follow:
# return from this nt and go to parent
return False
elif token.name == "eof":
# ret false? haven't recovered?
# in case eof is in the follow set, it will be discovered in the previous elif.
return False
def match(lexer, follow, *terminals):
global token
for terminal in terminals:
if token.name == terminal:
return True
# no terminal matched.
error = error_msg(token, *terminals)
errors.append(error)
# recover
return recover(lexer, follow, first=terminals)
def recover_first(lexer, first, follow):
error = error_msg(token, *first)
errors.append(error)
return recover(lexer, follow, first=first)
def is_in_follow(nonterminal):
return token.name in FOLLOW[nonterminal]
def recover_multiple_first_plus(lexer, nonterminal):
# in case token doesn't match any of the first+ sets, discard tokens until reach either a token in the first+
# sets, follow set or eof
if not recover_first(lexer, FIRST[nonterminal], FOLLOW[nonterminal]):
# if FOLLOW return true, thus parent can resume. if eof, then return false
return is_in_follow(nonterminal)
else:
# if the recover reached a token from one of the FIRST+ sets, run the function again and return the results
return globals()[nonterminal](lexer) # call the nonterminal function based on its name
# use return value of false from a nonterminal to indicate that eof has been reached (prematurely).
# thus, if match
FIRST['program'] = ['class']
FOLLOW['program'] = ['eof']
def program(lexer):
"""
*** FIRST: class
*** FOLLOW: eof
PROGRAM -> CLASS ; PROGRAM_REST $$ FIRST_PLUS = class
"""
global token
# for single first plus rules also need to do: if token.name == class. FOR ERR HANDLING
if _class(lexer):
if not match(lexer, FOLLOW['program'], ";"):
return is_in_follow('program')
else:
# successful match
token = next_token(lexer)
if program_rest(lexer):
return True
return False
FIRST['program_rest'] = ['class', 'eof']
FOLLOW['program_rest'] = ['eof']
def program_rest(lexer):
"""
*** FIRST: class | eps
*** FOLLOW: eof
PROGRAM_REST -> PROGRAM $$ FIRST_PLUS = class
| eps $$ FIRST_PLUS = eof
"""
global token
if token.name == "class":
return program(lexer)
elif token.name in FOLLOW['program_rest']:
return True
else:
return recover_multiple_first_plus(lexer, 'program_rest')
FIRST['class'] = ['class']
FOLLOW['class'] = [';']
def _class(lexer):
"""
*** FIRST: class
*** FOLLOW: ;
CLASS -> class type_id CLASS_TYPE { FEATURE_LIST } $$ FIRST_PLUS = class
"""
global token
if not match(lexer, FOLLOW['class'], "class"):
# if FOLLOW reached, parent can continue
return is_in_follow('class')
else:
# successful match
token = next_token(lexer)
if not match(lexer, FOLLOW['class'], "type_id"):
return is_in_follow('class')
else:
class_name = token.val
class_methods.clear()
token = next_token(lexer)
if class_type(lexer):
if not match(lexer, FOLLOW['class'], "{"):
return is_in_follow('class')
else:
token = next_token(lexer)
if feature_list(lexer):
if not match(lexer, FOLLOW['class'], "}"):
return is_in_follow('class')
else:
token = next_token(lexer)
# generate class summary
class_summary = class_name + ": "
for method in class_methods:
class_summary += method + ", "
# slice last comma off and add to classes
classes.append(class_summary[:-2])
return True
return False
FIRST['class_type'] = ['inherits', '{']
FOLLOW['class_type'] = ['{']
def class_type(lexer):
"""
*** FIRST: inherits | eps
*** FOLLOW: {
CLASS_TYPE -> inherits type_id $$ FIRST_PLUS = inherits
| eps $$ FIRST_PLUS = {
"""
global token
if token.name == "inherits":
token = next_token(lexer)
if not match(lexer, FOLLOW['class_type'], "type_id"):
return is_in_follow('class_type')
else:
token = next_token(lexer)
return True
elif token.name in FOLLOW['class_type']:
return True
else:
return recover_multiple_first_plus(lexer, 'class_type')
FIRST['feature_list'] = ['object_id', '}']
FOLLOW['feature_list'] = ['}']
def feature_list(lexer):
"""
* FIRST: object_id | eps
* FOLLOW: }
FEATURE_LIST -> FEATURE ; FEATURE_LIST $$ FIRST_PLUS = object_id
| eps $$ FIRST_PLUS = }
"""
global token
if token.name == "object_id":
if feature(lexer):
if not match(lexer, FOLLOW['feature_list'], ";"):
return is_in_follow('feature_list')
else:
token = next_token(lexer)
return feature_list(lexer)
elif token.name in FOLLOW['feature_list']:
return True
else:
return recover_multiple_first_plus(lexer, 'feature_list')
return False
FIRST['feature'] = ['object_id']
FOLLOW['feature'] = [';']
def feature(lexer):
"""
* FIRST: object_id
* FOLLOW: ;
FEATURE -> object_id FEATURE_REST $$ FIRST_PLUS = object_id
"""
global token
if not match(lexer, FOLLOW['feature'], "object_id"):
return is_in_follow('feature')
else:
feature_id = token.val
token = next_token(lexer)
is_valid_feature_rest, feature_type = feature_rest(lexer)
if feature_type == "method":
class_methods.append(feature_id)
return is_valid_feature_rest
FIRST['feature_rest'] = ['(', ':']
FOLLOW['feature_rest'] = [';']
def feature_rest(lexer):
"""
* FIRST: ( | :
* FOLLOW: ;
FEATURE_REST -> ( OPT_FEATURE_ARGS ) : type_id { EXPR } $$ FIRST_PLUS = (
| : type_id OPT_EXPR_ASSIGNMENT $$ FIRST_PLUS = :
"""
global token
if token.name == "(":
token = next_token(lexer)
if opt_feature_args(lexer):
if not match(lexer, FOLLOW['feature_rest'], ")"):
return is_in_follow('feature_rest'), "" # need to return tuple since this used to pass method name back
else:
token = next_token(lexer)
if not match(lexer, FOLLOW['feature_rest'], ":"):
return is_in_follow('feature_rest'), ""
else:
token = next_token(lexer)
if not match(lexer, FOLLOW['feature_rest'], "type_id"):
return is_in_follow('feature_rest'), ""
else:
token = next_token(lexer)
if not match(lexer, FOLLOW['feature_rest'], "{"):
return is_in_follow('feature_rest'), ""
else:
token = next_token(lexer)
if expr(lexer):
if not match(lexer, FOLLOW['feature_rest'], "}"):
return is_in_follow('feature_rest'), ""
else:
token = next_token(lexer)
return True, "method"
elif token.name == ":":
token = next_token(lexer)
if not match(lexer, FOLLOW['feature_rest'], "type_id"):
return is_in_follow('feature_rest'), ""
else:
token = next_token(lexer)
return opt_expr_assignment(lexer), "attribute"
else:
return recover_multiple_first_plus(lexer, 'feature_rest'), ""
return False
FIRST['opt_feature_args'] = ['object_id', ')']
FOLLOW['opt_feature_args'] = [')']
def opt_feature_args(lexer):
"""
* FIRST: object_id | eps
* FOLLOW: )
OPT_FEATURE_ARGS -> FEATURE_ARGS $$ FIRST_PLUS = object_id
| eps $$ FIRST_PLUS = )
"""
global token
if token.name == "object_id":
return feature_args(lexer)
elif token.name in FOLLOW['opt_feature_args']:
return True
else:
return recover_multiple_first_plus(lexer, 'opt_feature_args')
FIRST['feature_args'] = ['object_id']
FOLLOW['feature_args'] = [')']
def feature_args(lexer):
"""
* FIRST: object_id
* FOLLOW: )
FEATURE_ARGS -> FORMAL MORE_FEATURE_ARGS $$ FIRST_PLUS = object_id
"""
global token
if formal(lexer):
return more_feature_args(lexer)
return False
FIRST['more_feature_args'] = [',', ')']
FOLLOW['more_feature_args'] = [')']
def more_feature_args(lexer):
"""
* FIRST: , | eps
* FOLLOW: )
MORE_FEATURE_ARGS -> , FORMAL MORE_FEATURE_ARGS $$ FIRST_PLUS = ,
| eps $$ FIRST_PLUS = )
"""
global token
if token.name == ",":
token = next_token(lexer)
if formal(lexer):
return more_feature_args(lexer)
elif token.name in FOLLOW['more_feature_args']:
return True
else:
return recover_multiple_first_plus(lexer, 'more_feature_args')
return False
FIRST['opt_expr_assignment'] = ['<-', ';', ',', 'in']
FOLLOW['opt_expr_assignment'] = [';', ',', 'in']
def opt_expr_assignment(lexer):
"""
* FIRST: <- | eps
* FOLLOW: ; | , | in
OPT_EXPR_ASSIGNMENT -> EXPR_ASSIGNMENT $$ FIRST_PLUS = <-
| eps $$ FIRST_PLUS = ; | , | in
"""
global token
if token.name == "<-":
return expr_assignment(lexer)
elif token.name in FOLLOW['opt_expr_assignment']:
return True
else:
return recover_multiple_first_plus(lexer, 'opt_expr_assignment')
FIRST['expr_assignment'] = ['<-']
FOLLOW['expr_assignment'] = [';', ',', 'in']
def expr_assignment(lexer):
"""
* FIRST: <-
* FOLLOW: ; | , | in
EXPR_ASSIGNMENT -> <- EXPR $$ FIRST_PLUS = <-
"""
global token
if not match(lexer, FOLLOW['expr_assignment'], "<-"):
return is_in_follow('expr_assignment')
else:
token = next_token(lexer)
return expr(lexer)
FIRST['formal'] = ['object_id']
FOLLOW['formal'] = [',', ')']
def formal(lexer):
"""
* FIRST: object_id
* FOLLOW: , | )
FORMAL -> object_id : type_id $$ FIRST_PLUS = object_id
"""
global token
if not match(lexer, FOLLOW['formal'], "object_id"):
return is_in_follow('formal')
else:
token = next_token(lexer)
if not match(lexer, FOLLOW['formal'], ":"):
return is_in_follow('formal')
else:
token = next_token(lexer)
if not match(lexer, FOLLOW['formal'], "type_id"):
return is_in_follow('formal')
else:
token = next_token(lexer)
return True
FIRST['expr'] = ["object_id", "integer", "string", "true", "false", "(", "if", "while", "{", "let", "case", "new", "~",
"isvoid", "not"]
FOLLOW['expr'] = ['}', ';', 'in', ')', 'then', 'else', 'fi', 'loop', 'pool', 'of', ',']
def expr(lexer):
"""
* FIRST: object_id | integer | string | true | false | ( | if | while | { | let | case | new | ~ | isvoid | not
* FOLLOW: } | ; | in | ) | then | else | fi | loop | pool | of | ,
EXPR -> ID <- EXPR $$ FIRST_PLUS = object_id
| BOOLEAN_COMPLEMENT_EXPR $$ FIRST_PLUS = integer | string | true | false | ( | object_id | if |
while | { | let | case | new | ~ | isvoid | not
"""
global token
if token.name == "object_id":
# there is FIRST+ conflict for token object_id.
# peek_token preserves the token so that the next call to next_token will return the same token
peek = peek_token(lexer)
if peek.name == '<-':
if _id(lexer):
if not match(lexer, FOLLOW['expr'], "<-"):
return is_in_follow('expr')
else:
token = next_token(lexer)
return expr(lexer)
else:
return boolean_complement_expr(lexer)
elif token.name in ["integer", "string", "true", "false", "(", "if", "while", "{", "let", "case", "new", "~",
"isvoid", "not"]:
return boolean_complement_expr(lexer)
else:
return recover_multiple_first_plus(lexer, 'expr')
return False
FIRST['boolean_complement_expr'] = ["integer", "string", "true", "false", "(", "if", "while", "{", "let", "case",
"new", "~", "isvoid", "not"]
FOLLOW['boolean_complement_expr'] = ['}', ';', 'in', ')', 'then', 'else', 'fi', 'loop', 'pool', 'of', ',']
def boolean_complement_expr(lexer):
"""
* FIRST: integer | string | true | false | ( | object_id | if | while | { | let | case | new | ~ | isvoid | not
* FOLLOW: } | ; | in | ) | then | else | fi | loop | pool | of | ,
BOOLEAN_COMPLEMENT_EXPR -> not BOOLEAN_COMPLEMENT_EXPR $$ FIRST_PLUS = not
| COMPARISON_EXPR $$ FIRST_PLUS = integer | string | true | false | ( |
| object_id | if | while | { | let | case | new | ~ | isvoid
"""
global token
if token.name == "not":
token = next_token(lexer)
return boolean_complement_expr(lexer)
elif token.name in ['integer', 'string', 'true', 'false', '(', 'object_id', 'if', 'while', '{', 'let', 'case',
'new', '~', 'isvoid']:
return comparison_expr(lexer)
else:
return recover_multiple_first_plus(lexer, 'boolean_comparison_expr')
FIRST['comparison_expr'] = ['integer', 'string', 'true', 'false', '(', 'object_id', 'if', 'while', '{', 'let', 'case',
'new', '~', 'isvoid']
FOLLOW['comparison_expr'] = ['}', ';', 'in', ')', 'then', 'else', 'fi', 'loop', 'pool', 'of', ',']
def comparison_expr(lexer):
"""
* FIRST: integer | string | true | false | ( | object_id | if | while | { | let | case | new | ~ | isvoid
* FOLLOW: } | ; | in | ) | then | else | fi | loop | pool | of | ,
COMPARISON_EXPR -> ADD_EXPR COMPARISON_OP $$ FIRST_PLUS = integer | string | true | false | ( | object_id
| if | while | { | let | case | new | ~ | isvoid
"""
global token
if add_expr(lexer):
return comparison_op(lexer)
return False
FIRST['comparison_op'] = ['<=', '<', '=', '}', ';', 'in', ')', 'then', 'else', 'fi', 'loop', 'pool', 'of', ',']
FOLLOW['comparison_op'] = ['}', ';', 'in', ')', 'then', 'else', 'fi', 'loop', 'pool', 'of', ',']
def comparison_op(lexer):
"""
* FIRST: <= | < | = | eps
* FOLLOW: } | ; | in | ) | then | else | fi | loop | pool | of | ,
COMPARISON_OP -> <= ADD_EXPR COMPARISON_OP $$ FIRST_PLUS = <=
| < ADD_EXPR COMPARISON_OP $$ FIRST_PLUS = <
| = ADD_EXPR COMPARISON_OP $$ FIRST_PLUS = =
| eps $$ FIRST_PLUS = } | ; | in | ) | then | else | fi |
| loop | pool | of | ,
"""
global token
if token.name == "<=":
token = next_token(lexer)
if add_expr(lexer):
return comparison_op(lexer)
elif token.name == "<":
token = next_token(lexer)
if add_expr(lexer):
return comparison_op(lexer)
elif token.name == "=":
token = next_token(lexer)
if add_expr(lexer):
return comparison_op(lexer)
elif token.name in FOLLOW['comparison_op']:
return True
else:
return recover_multiple_first_plus(lexer, 'comparison_op')
return False
FIRST['add_expr'] = ['integer', 'string', 'true', 'false', '(', 'object_id', 'if', 'while', '{', 'let', 'case', 'new',
'~', 'isvoid']
FOLLOW['add_expr'] = ['<=', '<', '=', '}', ';', 'in', ')', 'then', 'else', 'fi', 'loop', 'pool', 'of', ',']
def add_expr(lexer):
"""
* FIRST: integer | string | true | false | ( | object_id | if | while | { | let | case | new | ~ | isvoid
* FOLLOW: <= | < | = | } | ; | in | ) | then | else | fi | loop | pool | of | ,
ADD_EXPR -> MULT_EXPR ADD_OP $$ FIRST_PLUS = integer | string | true | false | ( | object_id | if
| while | { | let | case | new | ~ | isvoid
"""
global token
if mult_expr(lexer):
return add_op(lexer)
return False
FIRST['add_op'] = ['+', '-', '<=', '<', '=', '}', ';', 'in', ')', 'then', 'else', 'fi', 'loop', 'pool', 'of', ',']
FOLLOW['add_op'] = ['<=', '<', '=', '}', ';', 'in', ')', 'then', 'else', 'fi', 'loop', 'pool', 'of', ',']
def add_op(lexer):
"""
* FIRST: + | - | eps
* FOLLOW: <= | < | = | } | ; | in | ) | then | else | fi | loop | pool | of | ,
ADD_OP -> + MULT_EXPR ADD_OP $$ FIRST_PLUS = +
| - MULT_EXPR ADD_OP $$ FIRST_PLUS = -
| eps $$ FIRST_PLUS = <= | < | = | } | ; | in | ) | then | else | fi
| loop | pool | of | ,
"""
global token
if token.name == "+":
token = next_token(lexer)
if mult_expr(lexer):
return add_op(lexer)
elif token.name == "-":
token = next_token(lexer)
if mult_expr(lexer):
return add_op(lexer)
elif token.name in FOLLOW['add_op']:
return True
else:
return recover_multiple_first_plus(lexer, 'add_op')
return False
FIRST['mult_expr'] = ['integer', 'string', 'true', 'false', '(', 'object_id', 'if', 'while', '{', 'let', 'case', 'new',
'~', 'isvoid']
FOLLOW['mult_expr'] = ['+', '-', '<=', '<', '=', '}', ';', 'in', ')', 'then', 'else', 'fi', 'loop', 'pool', 'of', ',']
def mult_expr(lexer):
"""
* FIRST: integer | string | true | false | ( | object_id | if | while | { | let | case | new | ~ | isvoid
* FOLLOW: + | - | <= | < | = | } | ; | in | ) | then | else | fi | loop | pool | of | ,
MULT_EXPR -> CHECKVOID_EXPR MULT_OP $$ FIRST_PLUS = integer | string | true | false | ( |
| object_id | if | while | { | let | case | new | ~ | isvoid
"""
global token
if checkvoid_expr(lexer):
return mult_op(lexer)
return False
FIRST['mult_op'] = ['*', '/', '+', '-', '<=', '<', '=', '}', ';', 'in', ')', 'then', 'else', 'fi', 'loop', 'pool',
'of', ',']
FOLLOW['mult_op'] = ['+', '-', '<=', '<', '=', '}', ';', 'in', ')', 'then', 'else', 'fi', 'loop', 'pool', 'of', ',']
def mult_op(lexer):
"""
* FIRST: * | / | eps
* FOLLOW: + | - | <= | < | = | } | ; | in | ) | then | else | fi | loop | pool | of | , | FOLLOW(ELEMENT)
MULT_OP -> * CHECKVOID_EXPR MULT_OP $$ FIRST_PLUS = *
| / CHECKVOID_EXPR MULT_OP $$ FIRST_PLUS = /
| eps $$ FIRST_PLUS = + | - | <= | < | = | } | ; | in | ) | then |
| else | fi | loop | pool | of | ,
"""
global token
if token.name == "*":
token = next_token(lexer)
if checkvoid_expr(lexer):
return mult_op(lexer)
elif token.name == "/":
token = next_token(lexer)
if checkvoid_expr(lexer):
return mult_op(lexer)
elif token.name in FOLLOW['mult_op']:
return True
else:
return recover_multiple_first_plus(lexer, 'mult_op')
return False
FIRST['checkvoid_expr'] = ['integer', 'string', 'true', 'false', '(', 'object_id', 'if', 'while', '{', 'let', 'case',
'new', '~', 'isvoid']
FOLLOW['checkvoid_expr'] = ['*', '/', '+', '-', '<=', '<', '=', '}', ';', 'in', ')', 'then', 'else', 'fi', 'loop',
'pool', 'of', ',']
def checkvoid_expr(lexer):
"""
* FIRST: integer | string | true | false | ( | object_id | if | while | { | let | case | new | ~ | isvoid
* FOLLOW: * | / | + | - | <= | < | = | } | ; | in | ) | then | else | fi | loop | pool | of | ,
CHECKVOID_EXPR -> isvoid CHECKVOID_EXPR $$ FIRST_PLUS = isvoid
| INTEGER_COMPLEMENT_EXPR $$ FIRST_PLUS = integer | string | true | false | ( |
| object_id | if | while | { | let | case | new | ~
"""
global token
if token.name == "isvoid":
token = next_token(lexer)
return checkvoid_expr(lexer)
elif token.name in ['integer', 'string', 'true', 'false', '(', 'object_id', 'if', 'while', '{', 'let', 'case',
'new', '~']:
return integer_complement_expr(lexer)
else:
return recover_multiple_first_plus(lexer, 'checkvoid_expr')
FIRST['integer_complement_expr'] = ['integer', 'string', 'true', 'false', '(', 'object_id', 'if', 'while', '{', 'let',
'case', 'new', '~']
FOLLOW['integer_complement_expr'] = ['*', '/', '+', '-', '<=', '<', '=', '}', ';', 'in', ')', 'then', 'else', 'fi',
'loop', 'pool', 'of', ',']
def integer_complement_expr(lexer):
"""
* FIRST: integer | string | true | false | ( | object_id | if | while | { | let | case | new | ~
* FOLLOW: * | / | + | - | <= | < | = | } | ; | in | ) | then | else | fi | loop | pool | of | ,
INTEGER_COMPLEMENT_EXPR -> ~ INTEGER_COMPLEMENT_EXPR $$ FIRST_PLUS = ~
| DISPATCH_EXPR $$ FIRST_PLUS = integer | string | true | false | ( |
| object_id | if | while | { | let | case | new
"""
global token
if token.name == "~":
token = next_token(lexer)
return integer_complement_expr(lexer)
elif token.name in ['integer', 'string', 'true', 'false', '(', 'object_id', 'if', 'while', '{', 'let', 'case',
'new']:
return dispatch_expr(lexer)
else:
return recover_multiple_first_plus(lexer, 'integer_complement_expr')
FIRST['dispatch_expr'] = ['integer', 'string', 'true', 'false', '(', 'object_id', 'if', 'while', '{', 'let',
'case', 'new']
FOLLOW['dispatch_expr'] = ['*', '/', '+', '-', '<=', '<', '=', '}', ';', 'in', ')', 'then', 'else', 'fi', 'loop',
'pool', 'of', ',']
def dispatch_expr(lexer):
"""
* FIRST: integer | string | true | false | ( | object_id | if | while | { | let | case | new
* FOLLOW: * | / | + | - | <= | < | = | } | ; | in | ) | then | else | fi | loop | pool | of | ,
DISPATCH_EXPR -> ELEMENT DISPATCH_OP $$ FIRST_PLUS = integer | string | true | false | ( | object_id |
| if | while | { | let | case | new
"""
global token
if element(lexer):
return dispatch_op(lexer)
return False
FIRST['dispatch_op'] = ['@', '.', '*', '/', '+', '-', '<=', '<', '=', '}', ';', 'in', ')', 'then', 'else', 'fi', 'loop',
'pool', 'of', ',']
FOLLOW['dispatch_op'] = ['*', '/', '+', '-', '<=', '<', '=', '}', ';', 'in', ')', 'then', 'else', 'fi', 'loop',
'pool', 'of', ',']
def dispatch_op(lexer):
"""
* FIRST: @ | . | eps
* FOLLOW: * | / | + | - | <= | < | = | } | ; | in | ) | then | else | fi | loop | pool | of | ,
DISPATCH_OP -> DISPATCH_TYPE . object_id ( OPT_EXPR_ARGS ) DISPATCH_OP $$ FIRST_PLUS = @ | .
| eps $$ FIRST_PLUS = * | / | + | - | <= | < | = | }
| ; | in | ) | then | else | fi | loop | pool | of | ,
"""
global token
if token.name == "@" or token.name == ".":
if dispatch_type(lexer):
if not match(lexer, FOLLOW['dispatch_op'], "."):
return is_in_follow('dispatch_op')
else:
token = next_token(lexer)
if not match(lexer, FOLLOW['dispatch_op'], "object_id"):
return is_in_follow('dispatch_op')
else:
token = next_token(lexer)
if not match(lexer, FOLLOW['dispatch_op'], "("):
return is_in_follow('dispatch_op')
else:
token = next_token(lexer)
if opt_expr_args(lexer):
if not match(lexer, FOLLOW['dispatch_op'], ")"):
return is_in_follow('dispatch_op')
else:
token = next_token(lexer)
return dispatch_op(lexer)
elif token.name in FOLLOW['dispatch_op']:
return True
else:
return recover_multiple_first_plus(lexer, 'dispatch_op')
return False
FIRST['dispatch_type'] = ['@', '.']
FOLLOW['dispatch_type'] = ['.']
def dispatch_type(lexer):
"""
* FIRST: @ | eps
* FOLLOW: .
DISPATCH_TYPE -> @ type_id $$ FIRST_PLUS = @
| eps $$ FIRST_PLUS = .
"""
global token
if token.name == "@":
token = next_token(lexer)
if not match(lexer, FOLLOW['dispatch_type'], "type_id"):
return is_in_follow('dispatch_type')
else:
token = next_token(lexer)
return True
elif token.name in FOLLOW['dispatch_type']:
return True
else:
return recover_multiple_first_plus(lexer, 'dispatch_type')
FIRST['element'] = ['integer', 'string', 'true', 'false', '(', 'object_id', 'if', 'while', '{', 'let',
'case', 'new']
FOLLOW['element'] = ['.', '@', '*', '/', '+', '-', '<=', '<', '=', '}', ';', 'in', ')', 'then', 'else', 'fi', 'loop',
'pool', 'of', ',']
def element(lexer):
"""
* FIRST: integer | string | true | false | ( | object_id | if | while | { | let | case | new
* FOLLOW: . | @ | * | / | + | - | <= | < | = | } | ; | in | ) | then | else | fi | loop | pool | of | ,
ELEMENT -> CONSTANT $$ FIRST_PLUS = integer | string | true | false
| ( EXPR ) $$ FIRST_PLUS = (
| ID ID_OP $$ FIRST_PLUS = object_id
| if EXPR then EXPR else EXPR fi $$ FIRST_PLUS = if
| while EXPR loop EXPR pool $$ FIRST_PLUS = while
| { EXPR_LIST } $$ FIRST_PLUS = {
| let LET_ARGS in EXPR $$ FIRST_PLUS = let
| case EXPR of CASE_ARGS esac $$ FIRST_PLUS = case
| new type_id $$ FIRST_PLUS = new
"""
global token
if token.name in ['integer', 'string', 'true', 'false']:
return constant(lexer)
elif token.name == "(":
token = next_token(lexer)
if expr(lexer):
if not match(lexer, FOLLOW['element'], ")"):
return is_in_follow('element')
else:
token = next_token(lexer)
return True
elif token.name == 'object_id':
if _id(lexer):
return id_op(lexer)
elif token.name == "if":
token = next_token(lexer)
if expr(lexer):
if not match(lexer, FOLLOW['element'], "then"):
return is_in_follow('element')
else:
token = next_token(lexer)
if expr(lexer):
if not match(lexer, FOLLOW['element'], "else"):
return is_in_follow('element')
else:
token = next_token(lexer)
if expr(lexer):
if not match(lexer, FOLLOW['element'], "fi"):
return is_in_follow('element')
else:
token = next_token(lexer)
return True
elif token.name == "while":
token = next_token(lexer)
if expr(lexer):
if not match(lexer, FOLLOW['element'], "loop"):
return is_in_follow('element')
else:
token = next_token(lexer)
if expr(lexer):
if not match(lexer, FOLLOW['element'], "pool"):
return is_in_follow('element')
else:
token = next_token(lexer)
return True
elif token.name == "{":
token = next_token(lexer)
if expr_list(lexer):
if not match(lexer, FOLLOW['element'], "}"):
return is_in_follow('element')
else:
token = next_token(lexer)
return True
elif token.name == "let":
token = next_token(lexer)
if let_args(lexer):
if not match(lexer, FOLLOW['element'], "in"):
return is_in_follow('element')
else:
token = next_token(lexer)
return expr(lexer)
elif token.name == "case":
token = next_token(lexer)
if expr(lexer):
if not match(lexer, FOLLOW['element'], "of"):
return is_in_follow('element')
else:
token = next_token(lexer)
if case_args(lexer):
if not match(lexer, FOLLOW['element'], "esac"):
return is_in_follow('element')
else:
token = next_token(lexer)
return True
elif token.name == "new":
token = next_token(lexer)
if not match(lexer, FOLLOW['element'], "type_id"):
return is_in_follow('element')
else:
token = next_token(lexer)
return True
else:
return recover_multiple_first_plus(lexer, 'element')
return False
FIRST['constant'] = ['integer', 'string', 'true', 'false']
FOLLOW['constant'] = ['.', '@', '*', '/', '+', '-', '<=', '<', '=', '}', ';', 'in', ')', 'then', 'else', 'fi', 'loop',
'pool', 'of', ',']
def constant(lexer):
"""
* FIRST: integer | string | true | false
* FOLLOW: . | @ | * | / | + | - | <= | < | = | } | ; | in | ) | then | else | fi | loop | pool | of | ,
CONSTANT -> integer $$ FIRST_PLUS = integer
| string $$ FIRST_PLUS = string
| true $$ FIRST_PLUS = true
| false $$ FIRST_PLUS = false
"""
global token
if token.name == "integer":
token = next_token(lexer)
return True
elif token.name == "string":
token = next_token(lexer)
return True
elif token.name == "true":
token = next_token(lexer)
return True
elif token.name == "false":
token = next_token(lexer)
return True
else:
return recover_multiple_first_plus(lexer, 'constant')
FIRST['_id'] = ['object_id']
FOLLOW['_id'] = ['(', '<-', '.', '@', '*', '/', '+', '-', '<=', '<', '=', '}', ';', 'in', ')', 'then', 'else', 'fi',
'loop', 'pool', 'of', ',']
def _id(lexer):
"""
* FIRST: object_id
* FOLLOW: ( | <- | . | @ | * | / | + | - | <= | < | = | } | ; | in | ) | then | else | fi | loop | pool | of | ,
ID -> object_id $$ FIRST_PLUS = object_id
"""
global token
if not match(lexer, FOLLOW['_id'], "object_id"):
return is_in_follow('_id')
else:
token = next_token(lexer)
return True
FIRST['id_op'] = ['(', '.', '@', '*', '/', '+', '-', '<=', '<', '=', '}', ';', 'in', ')', 'then', 'else', 'fi', 'loop',
'pool', 'of', ',']
FOLLOW['id_op'] = ['.', '@', '*', '/', '+', '-', '<=', '<', '=', '}', ';', 'in', ')', 'then', 'else', 'fi', 'loop',
'pool', 'of', ',']
def id_op(lexer):
"""
* FIRST: ( | eps
* FOLLOW: . | @ | * | / | + | - | <= | < | = | } | ; | in | ) | then | else | fi | loop | pool | of | ,
ID_OP -> ( OPT_EXPR_ARGS ) $$ FIRST_PLUS = (
| eps $$ FIRST_PLUS = . | @ | * | / | + | - | <= | < | = | } | ; |
| in | ) | then | else | fi | loop | pool | of | ,