forked from sabrsorensen/Compiler
/
Parser.py
1267 lines (1168 loc) · 43.7 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
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 inspect
import logging
from symbol_table import SymbolTable
from semantic_analyzer import SemanticAnalyzer
from semantic_record import SemanticRecord
log = logging.getLogger()
ch = logging.StreamHandler()
log.setLevel(logging.DEBUG)
#log.setLevel(logging.ERROR)
class Parser(object):
def __init__(self, tokens):
self.index = len(tokens)
self.tokens = iter(tokens)
self.cur_token = self.tokens.next() #Default token holder
self.next_token = self.tokens.next() #LL2 lookahead token holder for when needed
self.cur_symbol_table = None
self.root_table = SymbolTable(None)
self.sem_analyzer = SemanticAnalyzer(self.root_table)
self.program_name = ''
self.cur_proc_name = ''
self.cur_func_name = ''
############### Utility Functions ###############
def error(self, expected=None):
logging.error("Couldn't match: \"%s\" near line: %s, col: %s in %s(). Received %s" % (self.t_lexeme(),
self.cur_token.line, self.cur_token.column,
inspect.stack()[1][3],self.t_type()))
logging.error('Expected tokens: %s' % expected)
logging.error("Three level parse tree (stack) trace, most recent call last.\n\t^ %s()\n\t^ %s()\n\t> %s()" % (inspect.stack()[3][3],
inspect.stack()[2][3],
inspect.stack()[1][3]))
exit()
def t_type(self):
"""
So that we don't have to call the line below
every time we need a current token type
"""
return self.cur_token.token_type
def t_lexeme(self):
"""
Same as above - just a wrapper to make code
more elegant
"""
return self.cur_token.token_value
@classmethod
def print_tree(cls, level):
"""
A method for printing where we are at in parse tree
In case future assignments will require more complexity
"""
logging.debug(level)
def match(self, lexeme):
self.cur_token = self.next_token
try:
self.next_token = self.tokens.next()
except StopIteration:
pass
logging.info("Matched '%s' in %s()" % (lexeme, inspect.stack()[1][3]))
return False
def print_symbol_table(self, type, table):
level = log.getEffectiveLevel()
log.setLevel(logging.DEBUG)
logging.debug(type + " Symbol Table " + table.name + table.__repr__() + '\n')
log.setLevel(level)
############### Rule handling functions ###############
def system_goal(self):
"""
Expanding Rule 1:
System Goal -> Program $
"""
if self.t_type() == 'MP_PROGRAM':
Parser.print_tree('1')
self.program()
if self.t_type() == 'MP_EOF':
self.match('EOF')
self.print_symbol_table("Program",self.root_table)
self.sem_analyzer.write_IR()
return "The input program parses!"
exit()
else:
self.error('MP_PROGRAM')
def program(self):
"""
Expanding Rule 2:
Program -> ProgramHeading ";" Block "."
"""
if self.t_type() == 'MP_PROGRAM':
Parser.print_tree('2')
self.program_heading()
self.match(';')
self.root_table.create_root()
self.root_table.name = self.program_name
self.cur_symbol_table = self.root_table
self.sem_analyzer.sym_table = self.cur_symbol_table
self.sem_analyzer.gen_begin()
self.block()
self.sem_analyzer.gen_end()
self.match('.')
else:
self.error('MP_PROGRAM')
def program_heading(self):
"""
Expanding Rule 3:
"program" Identifier
"""
if self.t_type() == 'MP_PROGRAM':
Parser.print_tree('3')
self.match('program')
self.program_identifier()
else:
self.error('MP_PROGRAM')
def block(self):
"""
Expanding Rule 4:
Block -> VariableDeclarationPart ProcedureAndFunctionDeclarationPart StatementPart
"""
accepted_list = ['MP_VAR', 'MP_PROCEDURE', 'MP_BEGIN', 'MP_FUNCTION']
if self.t_type() in accepted_list:
Parser.print_tree('4')
self.variable_declaration_part()
self.procedure_and_function_declaration_part()
self.statement_part()
else:
self.error(accepted_list)
def variable_declaration_part(self):
"""
Expanding Rules 5, 6:
VariableDeclarationPart -> "var" VariableDeclaration ";" VariableDeclarationTail
-> e
"""
eps_list = ['MP_BEGIN', 'MP_FUNCTION', 'MP_PROCEDURE']
if self.t_type() == 'MP_VAR':
Parser.print_tree('5')
self.match('var')
self.variable_declaration()
self.match(';')
self.variable_declaration_tail()
elif self.t_type() in eps_list:
Parser.print_tree('6')
self.epsilon()
else:
self.error(eps_list.append('MP_VAR'))
self.sem_analyzer.gen_add_sp()
def epsilon(self):
"""
Branch went to epsilon - pass
"""
pass
def variable_declaration_tail(self):
"""
Expanding Rules 7, 8:
VariableDeclarationTail -> VariableDeclaration ";" VariableDeclarationTail
-> e
"""
eps_list = ['MP_BEGIN', 'MP_FUNCTION', 'MP_PROCEDURE']
if self.t_type() == 'MP_IDENTIFIER':
Parser.print_tree('7')
self.variable_declaration()
self.match(';')
self.variable_declaration_tail()
elif self.t_type() in eps_list:
Parser.print_tree('8')
self.epsilon()
else:
self.error(eps_list.append('MP_IDENTIFIER'))
def variable_declaration(self):
"""
Expanding Rule 9:
VariableDeclaration -> IdentifierList ":" Type
"""
if self.t_type() == 'MP_IDENTIFIER':
Parser.print_tree('9')
var_list = self.identifier_list([])
self.match(':')
type = self.type()
#iterate through the list of vars
for var in var_list:
record = SemanticRecord()
record.type = type
record.lexeme = var
record.set_size(type)
record.kind = "var"
record.depth = self.cur_symbol_table.cur_depth
self.cur_symbol_table.insert(record)
else:
self.error('MP_IDENTIFIER')
def type(self):
"""
Expanding Rules 10, 11:
Type -> "Integer"
-> "Float"
"""
lexeme = ''
if self.t_type() == 'MP_FLOAT':
Parser.print_tree('11')
lexeme = self.t_lexeme()
self.match(lexeme)
elif self.t_type() == 'MP_INTEGER':
Parser.print_tree('10')
lexeme = self.t_lexeme()
self.match(lexeme)
else:
self.error(['MP_FLOAT', 'MP_INTEGER'])
return lexeme
def procedure_and_function_declaration_part(self):
"""
Expanding Rules 12, 13, 14:
ProcedureAndFunctionDeclarationPart -> ProcedureDeclaration ProcedureAndFunctionDeclarationPart
-> FunctionDeclaration ProcedureAndFunctionDeclarationPart
-> epsilon
"""
if self.t_type() == 'MP_PROCEDURE':
Parser.print_tree('12')
self.procedure_declaration()
self.procedure_and_function_declaration_part()
elif self.t_type() == 'MP_FUNCTION':
Parser.print_tree('13')
self.function_declaration()
self.procedure_and_function_declaration_part()
elif self.t_type() == 'MP_BEGIN':
Parser.print_tree('14')
self.epsilon()
else:
self.error(['MP_PROCEDURE', 'MP_FUNCTION', 'MP_BEGIN'])
def procedure_declaration(self):
"""
Expanding Rule 15:
ProcedureDeclaration -> ProcedureHeading ";" Block ";"
"""
if self.t_type() == 'MP_PROCEDURE':
old_proc_name = self.cur_proc_name
proc_sym_table = SymbolTable(self.cur_symbol_table)
proc_sym_table.create()
self.cur_symbol_table = proc_sym_table
self.sem_analyzer.sym_table = self.cur_symbol_table
Parser.print_tree('15')
self.procedure_heading()
proc_sym_table.name = self.cur_proc_name
self.match(';')
self.block()
self.match(';')
self.print_symbol_table("Procedure", proc_sym_table)
self.cur_symbol_table = self.cur_symbol_table.parent_table
self.cur_proc_name = old_proc_name
proc_sym_table.destroy()
else:
self.error('MP_PROCEDURE')
def function_declaration(self):
"""
Expanding Rule 16:
FunctionDeclaration -> FunctionHeading ";" Block ";"
"""
if self.t_type() == 'MP_FUNCTION':
old_func_name = self.cur_func_name
func_sym_table = SymbolTable(self.cur_symbol_table)
func_sym_table.create()
self.cur_symbol_table = func_sym_table
self.sem_analyzer.sym_table = self.cur_symbol_table
Parser.print_tree('16')
self.function_heading()
func_sym_table.name = self.cur_func_name
self.match(';')
self.block()
self.match(';')
self.print_symbol_table("Function",func_sym_table)
self.cur_func_name = old_func_name
self.cur_symbol_table = self.cur_symbol_table.parent_table
func_sym_table.destroy()
else:
self.error('MP_FUNCTION')
def procedure_heading(self):
"""
Expanding Rule 17:
ProcedureHeading -> "procedure" procedureIdentifier OptionalFormalParameterList
"""
if self.t_type() == 'MP_PROCEDURE':
Parser.print_tree('17')
self.match('procedure')
self.procedure_identifier()
self.optional_formal_parameter_list()
else:
self.error('MP_PROCEDURE')
def function_heading(self):
"""
Expanding Rule 18:
FunctionHeading -> "function" functionIdentifier OptionalFormalParameterList ":" Type
"""
if self.t_type() == 'MP_FUNCTION':
Parser.print_tree('18')
self.match('function')
self.function_identifier()
self.optional_formal_parameter_list()
self.match(':')
type = self.type()
return type
else:
self.error('MP_FUNCTION')
def optional_formal_parameter_list(self):
"""
Expanding Rules 19, 20:
OptionalFormalParameterList -> "(" FormalParameterSection FormalParameterSectionTail ")"
-> epsilon
"""
eps_list = ['MP_FLOAT', 'MP_INTEGER', 'MP_SCOLON']
if self.t_type() == 'MP_LPAREN':
Parser.print_tree('19')
self.match('(')
self.formal_parameter_section()
self.formal_parameter_section_tail()
self.match(')')
elif self.t_type() in eps_list:
Parser.print_tree('20')
self.epsilon()
else:
self.error(eps_list.append('MP_LPAREN'))
def formal_parameter_section_tail(self):
"""
Expanding Rules 21, 22:
FormalParameterSectionTail -> ";" FormalParameterSection FormalParameterSectionTail
-> epsilon
"""
if self.t_type() == 'MP_SCOLON':
Parser.print_tree('21')
self.match(';')
self.formal_parameter_section()
self.formal_parameter_section_tail()
elif self.t_type() == 'MP_RPAREN':
Parser.print_tree('22')
self.epsilon()
else:
self.error(['MP_SCOLON', 'MP_RPAREN'])
def formal_parameter_section(self):
"""
Expanding Rules 23, 24:
FormalParameterSection -> ValueParameterSection
-> VariableParameterSection
"""
if self.t_type() == 'MP_IDENTIFIER':
Parser.print_tree('23')
self.value_parameter_section()
elif self.t_type() == 'MP_VAR':
Parser.print_tree('24')
self.variable_parameter_section()
else:
self.error(['MP_IDENTIFIER', 'MP_VAR'])
def value_parameter_section(self):
"""
Expanding Rule 25:
ValueParameterSection -> IdentifierList ":" Type
"""
if self.t_type() == 'MP_IDENTIFIER':
Parser.print_tree('25')
val_param_list = self.identifier_list([])
self.match(':')
type = self.type()
for var in val_param_list:
record = SemanticRecord()
record.type = type
record.lexeme = var
record.set_size(type)
record.kind = "var"
record.depth = self.cur_symbol_table.cur_depth
self.cur_symbol_table.insert(record)
else:
self.error('MP_IDENTIFIER')
def variable_parameter_section(self):
"""
Expanding Rule 26:
VariableParameterSection -> "var" IdentifierList ":" Type
"""
if self.t_type() == 'MP_VAR':
Parser.print_tree('26')
self.match(self.t_type())
var_param_list = self.identifier_list([])
self.match(':')
type = self.type()
#iterate through the list of vars
for var in var_param_list:
record = SemanticRecord()
record.type = type
record.lexeme = var
record.set_size(type)
record.kind = "var"
record.depth = self.cur_symbol_table.cur_depth
self.cur_symbol_table.insert(record)
else:
self.error('MP_VAR')
def statement_part(self):
"""
Expanding Rule 27:
StatementPart -> CompoundStatement
"""
if self.t_type() == 'MP_BEGIN':
Parser.print_tree('27')
self.compound_statement()
else:
self.error('MP_BEGIN')
def compound_statement(self):
"""
Expanding Rule 28:
CompoundStatement -> "begin" StatementSequence "end"
"""
if self.t_type() == 'MP_BEGIN':
Parser.print_tree('28')
self.match('begin')
self.statement_sequence()
self.match('end')
else:
self.error('MP_BEGIN')
def statement_sequence(self):
"""
Expanding Rule 29:
StatementSequence -> Statement StatementTail
"""
accepted_list = ['MP_BEGIN', 'MP_END', 'MP_READ',
'MP_WRITE', 'MP_IF', 'MP_WHILE',
'MP_REPEAT', 'MP_FOR', 'MP_IDENTIFIER']
if self.t_type() in accepted_list:
Parser.print_tree('29')
self.statement()
self.statement_tail()
else:
self.error(accepted_list)
def statement_tail(self):
"""
Expanding Rules 30, 31 :
StatementTail -> ";" Statement StatementTail
-> epsilon
"""
eps_list = [ 'MP_END', 'MP_UNTIL']
if self.t_type() == 'MP_SCOLON':
Parser.print_tree('30')
self.match(';')
self.statement()
self.statement_tail()
elif self.t_type() in eps_list:
Parser.print_tree('31')
self.epsilon()
else:
self.error(eps_list.append('MP_SCOLON'))
def statement(self):
"""
Expanding Rule 32 - 41 :
Statement -> EmptyStatement
-> CompoundStatement
-> ReadStatement
-> WriteStatement
-> AssignmentStatement
-> IfStatement
-> WhileStatement
-> RepeatStatement
-> ForStatement
-> ProcedureStatement
"""
if self.t_type() == 'MP_END':
Parser.print_tree('32')
self.empty_statement()
elif self.t_type() == 'MP_BEGIN':
Parser.print_tree('33')
self.compound_statement()
elif self.t_type() == 'MP_READ':
Parser.print_tree('34')
self.read_statement()
elif self.t_type() == 'MP_WRITE':
Parser.print_tree('35')
self.write_statement()
elif self.t_type() == 'MP_IDENTIFIER':
Parser.print_tree('36')
procedure_list = ['MP_END', 'MP_SCOLON', 'MP_LPAREN']
procedure_list_2 = ['MP_COLON', 'MP_ASSIGN']
if self.next_token.token_type in procedure_list:
self.procedure_statement()
elif self.next_token.token_type in procedure_list_2:
self.assignment_statement()
else:
self.error(['MP_END', 'MP_SCOLON', 'MP_LPAREN', 'MP_COLON', 'MP_ASSIGN'])
elif self.t_type() == 'MP_IF':
Parser.print_tree('37')
self.if_statement()
elif self.t_type() == 'MP_WHILE':
Parser.print_tree('38')
self.while_statement()
elif self.t_type() == 'MP_REPEAT':
Parser.print_tree('39')
self.repeat_statement()
elif self.t_type() == 'MP_FOR':
Parser.print_tree('40')
self.for_statement()
elif self.t_type() == 'MP_UNTIL':
pass
elif self.t_type() == 'MP_ELSE':
pass
else:
self.error(['MP_END', 'MP_BEGIN', 'MP_WRITE', 'MP_IDENTIFIER',
'MP_IF', 'MP_WHILE', 'MP_REPEAT', 'MP_FOR'])
def empty_statement(self):
"""
Expanding Rule 42:
EmptyStatement -> epsilon
"""
accepted_list = ['MP_SCOLON', 'MP_ELSE', 'MP_END', 'MP_UNTIL']
if self.t_type() in accepted_list:
Parser.print_tree('42')
self.epsilon()
else:
self.error(accepted_list)
def read_statement(self):
"""
Expanding Rule 43:
ReadStatement -> "read" "(" ReadParameter ReadParameterTail ")"
"""
if self.t_type() == 'MP_READ':
Parser.print_tree('43')
self.match('read')
self.match('(')
self.read_parameter()
self.read_parameter_tail()
self.match(')')
else:
self.error('MP_READ')
def read_parameter_tail(self):
"""
Expanding Rules 44, 45 :
ReadParameterTail -> "," ReadParameter ReadParameterTail
-> epsilon
"""
if self.t_type() == 'MP_COMMA':
Parser.print_tree('44')
self.match(',')
self.read_parameter()
self.read_parameter_tail()
elif self.t_type() == 'MP_RPAREN':
Parser.print_tree('45')
self.epsilon()
else:
self.error(['MP_COMMA', 'MP_RPAREN'])
def read_parameter(self):
"""
Expanding Rule 46 :
ReadParameter -> VariableIdentifier
"""
read_param_rec = SemanticRecord()
if self.t_type() == 'MP_IDENTIFIER':
Parser.print_tree('46')
self.variable_identifier(read_param_rec)
self.sem_analyzer.gen_read(read_param_rec)
else:
self.error('MP_IDENTIFIER')
def write_statement(self): #AAA
"""
Expanding Rule 47:
WriteStatement -> "write" "(" WriteParameter WriteParameterTail ")"
"""
write_param_rec = SemanticRecord()
if self.t_type() == 'MP_WRITE':
Parser.print_tree('47')
self.match('write')
self.match('(')
self.write_parameter() # this is an expression
self.write_parameter_tail() # this too is an expression of some sort
self.match(')')
# todo: handle combining the two returns from param and param_tail, push on the stack
else:
self.error('MP_WRITE')
def write_parameter_tail(self):
"""
Expanding Rules 48, 49 :
WriteParameterTail -> "," WriteParameter WriteParameterTail
-> epsilon
"""
if self.t_type() == 'MP_COMMA':
Parser.print_tree('48')
self.match(',')
self.write_parameter()
self.write_parameter_tail()
elif self.t_type() == 'MP_RPAREN':
Parser.print_tree('49')
self.epsilon()
else:
self.error(['MP_COMMA', 'MP_PAREN'])
def write_parameter(self):
"""
Expanding Rule 50 :
WriteParameter -> OrdinalExpression
"""
write_param_rec = SemanticRecord()
accepted_list = ['MP_LPAREN','MP_PLUS','MP_MINUS','MP_IDENTIFIER', 'MP_INTEGER','MP_NOT']
if self.t_type() in accepted_list:
Parser.print_tree('50')
self.ordinal_expression(write_param_rec)
self.sem_analyzer.gen_write(write_param_rec)
else:
self.error(accepted_list)
def assignment_statement(self):
"""
Expanding Rules 51, 52:
AssignmentStatement -> VariableIdentifier ":=" Expression
-> FunctionIdentifier ":=" Expression
"""
# the conflict here should be considered resolved, because in the end
#both those guys lead to identifier
ident_rec = SemanticRecord()
express_rec = SemanticRecord()
if self.t_type() == 'MP_IDENTIFIER':
Parser.print_tree('51')
self.variable_identifier(ident_rec)
self.match(':=')
self.expression(express_rec)
self.sem_analyzer.gen_ass_statement(ident_rec, express_rec)
else:
self.error('MP_IDENTIFIER')
def if_statement(self):
"""
Expanding Rule 53:
IfStatement -> "if" BooleanExpression "then" Statement OptionalElsePart
"""
if_rec = SemanticRecord()
if self.t_type() == 'MP_IF':
Parser.print_tree('53')
self.match('if')
self.boolean_expression(if_rec)
self.sem_analyzer.begin_if(if_rec)
self.match('then')
self.statement()
self.optional_else_part(if_rec)
self.sem_analyzer.end_if(if_rec)
else:
self.error('MP_IF')
def optional_else_part(self, if_rec):
"""
Expanding Rule 54:
OptionalElsePart -> "else" Statement
"""
eps_list = ['MP_SCOLON', 'MP_END', 'MP_UNTIL']
self.sem_analyzer.opt_else(if_rec)
if self.t_type() == 'MP_ELSE':
Parser.print_tree('54')
self.match('else')
self.statement()
elif self.t_type() in eps_list:
Parser.print_tree('55')
self.epsilon()
else:
self.error(eps_list.extend('MP_ELSE'))
def repeat_statement(self):
"""
Expanding Rule 56:
RepeatStatement -> "repeat" StatementSequence "until" BooleanExpression
"""
rep_rec = SemanticRecord()
if self.t_type() == 'MP_REPEAT':
Parser.print_tree('56')
self.match('repeat')
self.sem_analyzer.begin_repeat(rep_rec)
self.statement_sequence()
self.match('until')
self.boolean_expression(rep_rec)
self.sem_analyzer.end_repeat(rep_rec)
else:
self.error('MP_REPEAT')
def while_statement(self):
"""
Expanding Rule 57:
WhileStatement -> "while" BooleanExpression "do" Statement
"""
while_rec = SemanticRecord()
expr_rec = SemanticRecord()
if self.t_type() == 'MP_WHILE':
Parser.print_tree('57')
self.match('while')
self.sem_analyzer.begin_while(while_rec)
self.boolean_expression(expr_rec)
self.sem_analyzer.gen_while(while_rec, expr_rec)
self.match('do')
self.statement()
self.sem_analyzer.end_while(while_rec)
else:
self.error('MP_WHILE')
def for_statement(self):
"""
Expanding Rule 58:
ForStatement -> "for" ControlVariable ":=" InitialValue StepValue FinalValue "do" Statement
"""
control_var_rec = SemanticRecord()
initial_rec = SemanticRecord()
final_rec = SemanticRecord()
for_rec = SemanticRecord()
if self.t_type() == 'MP_FOR':
Parser.print_tree('58')
self.match('for')
self.control_variable(control_var_rec)
self.match(':=')
self.initial_value(initial_rec)
self.step_value(for_rec)
self.final_value(final_rec)
self.match('do')
self.sem_analyzer.begin_for(for_rec)
self.sem_analyzer.gen_for(for_rec, control_var_rec, initial_rec)
self.statement()
self.sem_analyzer.end_for(for_rec, control_var_rec, final_rec)
else:
self.error('MP_FOR')
def control_variable(self, control_var_rec):
"""
Expanding Rule 59:
ControlVariable -> VariableIdentifier
"""
if self.t_type() == 'MP_IDENTIFIER':
Parser.print_tree('59')
self.variable_identifier(control_var_rec)
else:
self.error('MP_IDENTIFIER')
def initial_value(self, initial_rec):
"""
Expanding Rule 60:
InitialValue -> OrdinalExpression
"""
accepted_list = ['MP_LPAREN','MP_PLUS','MP_MINUS','MP_IDENTIFIER', 'MP_INTEGER','MP_NOT']
if self.t_type() in accepted_list:
Parser.print_tree('60')
self.ordinal_expression(initial_rec)
else:
self.error(accepted_list)
def step_value(self, step_rec):
"""
Expanding Rules 61, 62 :
StepValue -> "to"
-> "downto"
"""
if self.t_type() == 'MP_TO':
Parser.print_tree('61')
self.match(self.t_lexeme())
step_rec.lexeme = 'lte'
elif self.t_type() == 'MP_DOWNTO':
Parser.print_tree('62')
self.match(self.t_lexeme())
step_rec.lexeme = 'gte'
else:
self.error(['MP_TO', 'MP_DOWNTO'])
def final_value(self, final_rec):
"""
Expanding Rule 63:
FinalValue -> OrdinalExpression
"""
accepted_list = ['MP_LPAREN','MP_PLUS','MP_MINUS','MP_IDENTIFIER', 'MP_INTEGER','MP_NOT']
if self.t_type() in accepted_list:
Parser.print_tree('63')
self.ordinal_expression(final_rec)
else:
self.error(accepted_list)
def procedure_statement(self):
"""
Expanding Rule 64:
ProcedureStatement -> ProcedureIdentifier OptionalActualParameterList
"""
if self.t_type() == 'MP_IDENTIFIER':
Parser.print_tree('64')
self.procedure_identifier()
self.optional_actual_parameter_list()
else:
self.error('MP_IDENTIFIER')
def optional_actual_parameter_list(self):
"""
Expanding Rules 65, 66 :
OptionalActualParameterList -> "(" ActualParameter ActualParameterTail ")"
"""
eps_list = ['MP_TIMES', 'MP_RPAREN', 'MP_PLUS',
'MP_COMMA', 'MP_MINUS', 'MP_SCOLON',
'MP_LTHAN', 'MP_LEQUAL', 'MP_GTHAN',
'MP_GEQUAL', 'MP_EQUAL', 'MP_NEQUAL',
'MP_AND', 'MP_DIV', 'MP_DO',
'MP_DOWNTO', 'MP_ELSE', 'MP_END',
'MP_MOD', 'MP_OR', 'MP_THEN',
'MP_TO', 'MP_UNTIL']
actual_rec = SemanticRecord()
if self.t_type() == 'MP_LPAREN':
Parser.print_tree('65')
self.match('(')
self.actual_parameter(actual_rec)
self.actual_parameter_tail(actual_rec)
self.match(')')
elif self.t_type() in eps_list:
Parser.print_tree('66')
self.epsilon()
else:
self.error(eps_list.append('MP_LPAREN'))
def actual_parameter_tail(self, act_rec):
"""
Expanding Rules 67, 68:
ActualParameterTail -> "," ActualParameter ActualParameterTail
-> epsilon
"""
if self.t_type() == 'MP_COMMA':
Parser.print_tree('67')
self.match(',')
self.actual_parameter(act_rec)
self.actual_parameter_tail(act_rec)
elif self.t_type() == 'MP_RPAREN':
Parser.print_tree('68')
self.epsilon()
else:
self.error(['MP_COMMA', 'MP_RPAREN'])
def actual_parameter(self,act_rec):
"""
Expanding Rule 69:
ActualParameter -> OrdinalExpression
"""
accepted_list = ['MP_LPAREN','MP_PLUS','MP_MINUS','MP_IDENTIFIER', 'MP_INTEGER','MP_NOT']
if self.t_type() in accepted_list:
Parser.print_tree('69')
self.ordinal_expression(act_rec)
else:
self.error(accepted_list)
def expression(self, sem_rec):
"""
Expanding Rule 70 :
Expression -> SimpleExpression OptionalRelationalPart
"""
accepted_list = ['MP_LPAREN','MP_PLUS','MP_MINUS','MP_IDENTIFIER', 'MP_INTEGER','MP_NOT']
if self.t_type() in accepted_list:
Parser.print_tree('70')
self.simple_expression(sem_rec)
self.optional_relational_part(sem_rec)
else:
self.error(accepted_list)
def optional_relational_part(self, rel_rec):
"""
Expanding Rule 71, 72:
OptionalRelationalPart -> RelationalOperator SimpleExpression
-> epsilon
"""
accepted_list = ['MP_LTHAN', 'MP_LEQUAL', 'MP_GTHAN',
'MP_GEQUAL', 'MP_EQUAL', 'MP_NEQUAL']
eps_list = ['MP_RPAREN', 'MP_COMMA', 'MP_SCOLON',
'MP_DO', 'MP_DOWNTO', 'MP_ELSE',
'MP_END', 'MP_THEN', 'MP_TO', 'MP_UNTIL']
if self.t_type() in accepted_list:
Parser.print_tree('71')
self.relational_operator(rel_rec)
self.simple_expression(sem_rec = SemanticRecord())
elif self.t_type() in eps_list:
Parser.print_tree('72')
self.epsilon()
else:
self.error(accepted_list.extend(eps_list))
def relational_operator(self, expr_rec):
"""
Expanding Rules 73 - 78:
RelationalOperator -> "="
-> "<"
-> ">"
-> "<="
-> ">="
-> "<>"
"""
if self.t_type() == 'MP_EQUAL':
Parser.print_tree('73')
self.match(self.t_lexeme())
expr_rec.lexeme = 'eq'
elif self.t_type() == 'MP_LTHAN':
Parser.print_tree('74')
self.match(self.t_lexeme())
expr_rec.lexeme = 'lt'
elif self.t_type() == 'MP_GTHAN':
Parser.print_tree('75')
self.match(self.t_lexeme())
expr_rec.lexeme = 'gt'
elif self.t_type() == 'MP_LEQUAL':
Parser.print_tree('76')
self.match(self.t_lexeme())
expr_rec.lexeme = 'lte'
elif self.t_type() == 'MP_GEQUAL':
Parser.print_tree('77')
self.match(self.t_lexeme())
expr_rec.lexeme = 'gte'
elif self.t_type() == 'MP_NEQUAL':
Parser.print_tree('78')
self.match(self.t_lexeme())
expr_rec.lexeme = 'ne'
else:
self.error(['MP_EQUAL', 'MP_LTHAN', 'MP_GTHAN',
'MP_LEQUAL', 'MP_GEQUAL', 'MP_NEQUAL'])
def simple_expression(self, sem_rec):
"""
Expanding Rule 79 :
SimpleExpression -> OptionalSign Term TermTail
"""
accepted_list = ['MP_LPAREN', 'MP_PLUS', 'MP_MINUS',
'MP_IDENTIFIER', 'MP_INTEGER', 'MP_NOT']
if self.t_type() in accepted_list:
Parser.print_tree('79')
self.optional_sign(sem_rec)
self.term(sem_rec)
self.term_tail(sem_rec)
else:
self.error(accepted_list)
def term_tail(self, term_tail_rec): # term_tail_rec contains information about the left operand of the expression
"""
Expanding Rule 80,81 :
TermTail -> AddingOperator Term TermTail
TermTail -> ?
"""
result_sem_rec = SemanticRecord()
term_sem_rec = SemanticRecord()
add_op_sem_rec = SemanticRecord()
eps_list = ['MP_RPAREN', 'MP_COMMA', 'MP_SCOLON',
'MP_LTHAN', 'MP_LEQUAL', 'MP_NEQUAL',
'MP_EQUAL', 'MP_GTHAN', 'MP_GEQUAL',
'MP_DO', 'MP_DOWNTO', 'MP_ELSE',
'MP_TO', 'MP_UNTIL']
accepted_list = ['MP_PLUS', 'MP_MINUS', 'MP_OR']
if self.t_type() in accepted_list:
Parser.print_tree('80')
#self.optional_sign()
self.adding_operator(add_op_sem_rec)
self.term(term_sem_rec)
self.sem_analyzer.gen_arithmetic(term_tail_rec, add_op_sem_rec, term_sem_rec, result_sem_rec)
self.term_tail(result_sem_rec)
elif self.t_type() in eps_list:
Parser.print_tree('81')
self.epsilon()
else:
self.error(accepted_list.extend(eps_list))
def optional_sign(self, sem_rec):
"""