forked from openaki/compiler
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcparse.py
More file actions
executable file
·1158 lines (1009 loc) · 27.2 KB
/
Copy pathcparse.py
File metadata and controls
executable file
·1158 lines (1009 loc) · 27.2 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
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 re
import ply.yacc as yacc
from copy import deepcopy
from c_lex import tokens
temp_count = 0; # To keep a track of temp variables ... using $$
label_count = 0; # To keep a track of Labels .... using ~~
dict_var = {} # A dict for mapping register to vairables
register_count = 0 # To keep a track on registers .... using @@
string_count = 0 #To keep a count on number of strings ... Will store the sting in a map
converted_list = []
proc_name = "" # The name of the procedure ... Not being converted to SUIF code at present
precedence = (
('right', 'ASSIGN', 'EQ_PLUS', 'EQ_MINUS', 'EQ_TIMES', 'EQ_DIV'),
('left', 'DOUBLE_PIPE'),
('left', 'DOUBLE_AMPERSAND'),
('left', 'NOT_EQ', 'EQ'),
('left', 'GREATER', 'LESS', 'GREATER_EQ', 'LESS_EQ'),
('left', 'PLUS', 'MINUS'),
('left', 'ASTERISK', 'DIV'),
('right', 'UEXCLAMATION', 'UASTERISK', 'UMINUS', 'AMPERSAND'),
('right', 'DOUBLE_PLUS', 'DOUBLE_MINUS'),
)
class ParseError(Exception):
"Exception raised whenever a parsing error occurs."
pass
def p_start(p):
'''start : translation_unit'''
global dict_var;
global register_count;
p[0] = p[1]
for induction in range(len(p[0])):
i = p[0][induction]
i_list = list(i)
for k in range(len(i_list)):
j = i_list[k]
if(j[:2] == "@@"):
var = j[2:]
if(dict_var.has_key(var)):
i_list[k] = dict_var[var];
else:
temp_str = "r" + str(register_count);
register_count = register_count + 1;
dict_var[var] = temp_str;
i_list[k] = temp_str;
if j[:2] == "$$" :
i_list[k] = j[2:]
if j[:2] == "~~" :
i_list[k] = "__" + j[2:]
i = tuple(i_list)
p[0][induction] = i;
global converted_list
converted_list = p[0]
print "Grammar Accepted"
pass
def p_translation_unit_01(p):
'''translation_unit : external_declaration'''
p[0] = p[1]
pass
def p_translation_unit_02(p):
'''translation_unit : translation_unit external_declaration'''
if type(p[1]) == list :
p[0] = p[1]
else :
p[0] = []
if type(p[2]) == list :
p[0].extend(p[2])
pass
def p_external_declaration(p):
'''external_declaration : function_definition
| declaration'''
p[0] = p[1]
def p_function_definition_01(p):
'''function_definition : type_specifier declarator compound_statement'''
if type(p[1]) == list :
p[0] = p[1]
else :
p[0] = []
if type(p[2]) == list :
p[0].extend(p[2])
if type(p[3]) == list :
p[0].extend(p[3])
pass
def p_function_definition_02(p):
'''function_definition : STATIC type_specifier declarator compound_statement'''
if type(p[2]) == list :
p[0] = p[2]
else :
p[0] = []
if type(p[3]) == list :
p[0].extend(p[3])
if type(p[4]) == list :
p[0].extend(p[4])
pass
def p_declaration_01(p):
'''declaration : type_specifier declarator SEMICOLON'''
# Doing nothing here at present ... just returning a []
p[0] = p[2]
def p_declaration_02(p):
'''declaration : EXTERN type_specifier declarator SEMICOLON'''
# Doing nothing here at present ... just returning a []
p[0] = p[3]
def p_declaration_list_opt_01(p):
'''declaration_list_opt : empty'''
p[0] = []
def p_declaration_list_opt_02(p):
'''declaration_list_opt : declaration_list'''
p[0] = p[1]
def p_declaration_list_02(p):
'''declaration_list : declaration'''
p[0] = p[1]
def p_declaration_list_03(p):
'''declaration_list : declaration declaration_list '''
p[0] = []
if type(p[1]) == list:
p[0].extend(p[1])
if type(p[2]) == list:
p[0].extend(p[2])
def p_type_specifier(p):
'''type_specifier : INT
| CHAR
| empty
| VOID
| FLOAT
| DOUBLE
| LONG'''
p[0] = []
def p_declarator_01(p):
'''declarator : direct_declarator'''
if type(p[1]) == str:
p[0] = [("cpy", "@@" + p[1], "[RAND]", "")]
elif type(p[1]) == list:
p[0] = p[1]
def p_declarator_01_01(p):
'''declarator : expression ASSIGN expression'''
p[0] = [];
if(type(p[1]) == list) :
p[0] = p[1];
temp1 = p[1][-1][1];
else :
p[0] = []
temp1 = p[1][1]
# for x = y ... got the x
if(type(p[3]) == list) :
p[0].extend(p[3])
temp2 = p[3][-1][1]
tup = ("cpy", temp1, temp2, "")
p[0].append(tup)
else :
temp2 = p[3][1]
p[0].append(("cpy", temp1, temp2, ""))
## Change made ....
def p_declarator_01_1(p):
'''declarator : direct_declarator COMMA declarator'''
p[0] = []
if(type(p[1]) == list) :
p[0].extend(p[1])
if(type(p[1]) == str):
p[0].append(("cpy", "@@" + p[1], "[RAND]", ""))
if(type(p[3]) == list) :
p[0].extend(p[3])
def p_declarator_01_2(p):
'''declarator : expression ASSIGN expression COMMA declarator'''
p[0] = [];
if(type(p[1]) == list) :
p[0] = p[1];
temp1 = p[1][-1][1];
else :
p[0] = []
temp1 = p[1][1]
# for x = y ... got the x
if(type(p[3]) == list) :
p[0].extend(p[3])
temp2 = p[3][-1][1]
tup = ("cpy", temp1, temp2, "")
p[0].append(tup)
else :
temp2 = p[3][1]
p[0].append(("cpy", temp1, temp2, ""))
if(type(p[5]) == list):
p[0].extend(p[5])
def p_declarator_02(p):
'''declarator : ASTERISK declarator'''
def p_direct_declarator_01(p):
'''direct_declarator : ID'''
p[0] = p[1]
def p_direct_declarator_02(p):
'''direct_declarator : direct_declarator LPAREN parameter_type_list RPAREN'''
p[0] = p[3]
global proc_name
proc_name = p[1]
def p_direct_declarator_03(p):
'''direct_declarator : direct_declarator LPAREN RPAREN'''
p[0] = []
global proc_name
proc_name = p[1]
def p_parameter_type_list_01(p):
'''parameter_type_list : parameter_list'''
p[0] = p[1]
#def p_parameter_type_list_02(p):
# '''parameter_type_list : parameter_list COMMA ELLIPSIS'''
# p[0] = []
def p_parameter_list_01(p):
'''parameter_list : type_specifier expression'''
if type(p[2]) == tuple:
p[0] = [("cpy", p[2][1], "[MEM]", "")]
else:
p[0] = p[2]
def p_parameter_list_02(p):
'''parameter_list : type_specifier expression COMMA parameter_list'''
p[0] = []
if(type(p[2]) == list) :
p[0].extend(p[2])
else:
p[0] = [("cpy", p[2][1], "[MEM]", "")]
if(type(p[4]) == list) :
p[0].extend(p[4])
def p_parameter_declaration(p):
'''parameter_declaration : type_specifier declarator'''
# NOTE: this is the same code as p_declaration_01!
p_declaration_01(p)
def p_compound_statement_01(p):
'''compound_statement : LBRACE declaration_list_opt statement_list RBRACE'''
if type(p[2]) == list :
p[0] = p[2]
else :
p[0] = []
if type(p[3]) == list:
p[0].extend(p[3])
pass
def p_compound_statement_02(p):
'''compound_statement : LBRACE declaration_list_opt RBRACE'''
p[0] = p[2]
def p_statement_list_02(p):
'''statement_list : statement'''
p[0] = p[1]
pass
def p_statement_list_03(p):
'''statement_list : statement statement_list '''
p[0] = p[1]
if type(p[1]) == list and type(p[2]) == list :
p[0].extend(p[2])
pass
# End of statement list ..
# Statements of a statement list ...
def p_statement(p):
'''statement : compound_statement
| jump_statement
| iteration_statement | selection_statement | expression_statement
| label '''
p[0] = p[1]
pass
def p_label(p):
'''label : ID COLON'''
global label_count
qwe = re.compile('L[0-9]+')
a = qwe.match(p[1])
if a:
ttt = a.group();
label = int(ttt[1:])
if label > label_count:
label_count = label + 1;
p[0] = [(p[1] + ":", "", "", "")]
pass
def p_expression_statement(p):
'''expression_statement : expression SEMICOLON'''
p[0] = p[1]
def p_expresssion_01(p):
'''expression : binary_expression
| unary_expression
| primary_expression
| postfix_expression '''
p[0] = p[1]
def p_binary_expression_01(p):
'''binary_expression : expression ASSIGN expression '''
p[0] = [];
if(type(p[1]) == list) :
p[0] = p[1];
temp1 = p[1][-1][1];
else :
p[0] = []
temp1 = p[1][1]
# for x = y ... got the x
if(type(p[3]) == list) :
p[0].extend(p[3])
temp2 = p[3][-1][1]
tup = ("cpy", temp1, temp2, "")
p[0].append(tup)
else :
temp2 = p[3][1]
p[0].append(("cpy", temp1, temp2, ""))
pass
def p_binary_expression_02(p):
'''binary_expression : expression DOUBLE_PIPE expression '''
global temp_count;
if(type(p[1]) == list) :
p[0] = p[1];
temp1 = p[1][-1][1];
else :
p[0] = []
temp1 = p[1][1]
if(type(p[3]) == list) :
p[0].extend(p[3]);
temp2 = p[3][-1][1];
else :
temp2 = p[3][1]
temp3 = "$$t" + str(temp_count)
temp_count = temp_count + 1;
p[0].append(('and', temp3, temp1, temp2))
pass
def p_binary_expression_03(p):
'''binary_expression : expression DOUBLE_AMPERSAND expression '''
global temp_count;
if(type(p[1]) == list) :
p[0] = p[1];
temp1 = p[1][-1][1];
else :
p[0] = []
temp1 = p[1][1]
if(type(p[3]) == list) :
p[0].extend(p[3]);
temp2 = p[3][-1][1];
else :
temp2 = p[3][1]
temp3 = "$$t" + str(temp_count)
temp_count = temp_count + 1;
p[0].append(('ior', temp3, temp1, temp2))
pass
def p_binary_expression_04(p):
'''binary_expression : expression EQ expression '''
global temp_count;
if(type(p[1]) == list) :
p[0] = p[1];
temp1 = p[1][-1][1];
else :
p[0] = []
temp1 = p[1][1]
if(type(p[3]) == list) :
p[0].extend(p[3]);
temp2 = p[3][-1][1];
else :
temp2 = p[3][1]
temp3 = "$$t" + str(temp_count)
temp_count = temp_count + 1;
p[0].append(('seq', temp3, temp1, temp2))
pass
def p_binary_expression_05(p):
'''binary_expression : expression NOT_EQ expression '''
global temp_count;
if(type(p[1]) == list) :
p[0] = p[1];
temp1 = p[1][-1][1];
else :
p[0] = []
temp1 = p[1][1]
if(type(p[3]) == list) :
p[0].extend(p[3]);
temp2 = p[3][-1][1];
else :
temp2 = p[3][1]
temp3 = "$$t" + str(temp_count)
temp_count = temp_count + 1;
p[0].append(('sne', temp3, temp1, temp2))
def p_binary_expression_06(p):
'''binary_expression : expression LESS expression '''
global temp_count;
if(type(p[1]) == list) :
p[0] = p[1];
temp1 = p[1][-1][1];
else :
p[0] = []
temp1 = p[1][1]
if(type(p[3]) == list) :
p[0].extend(p[3]);
temp2 = p[3][-1][1];
else :
temp2 = p[3][1]
temp3 = "$$t" + str(temp_count)
temp_count = temp_count + 1;
p[0].append(('sl', temp3, temp1, temp2))
pass
def p_binary_expression_07(p):
'''binary_expression : expression GREATER expression '''
global temp_count;
if(type(p[1]) == list) :
p[0] = p[1];
temp1 = p[1][-1][1];
else :
p[0] = []
temp1 = p[1][1]
if(type(p[3]) == list) :
p[0].extend(p[3]);
temp2 = p[3][-1][1];
else :
temp2 = p[3][1]
temp3 = "$$t" + str(temp_count)
temp_count = temp_count + 1;
p[0].append(('sl', temp3, temp2, temp1))
pass
def p_binary_expression_08(p):
'''binary_expression : expression LESS_EQ expression '''
global temp_count;
if(type(p[1]) == list) :
p[0] = p[1];
temp1 = p[1][-1][1];
else :
p[0] = []
temp1 = p[1][1]
if(type(p[3]) == list) :
p[0].extend(p[3]);
temp2 = p[3][-1][1];
else :
temp2 = p[3][1]
temp3 = "$$t" + str(temp_count)
temp_count = temp_count + 1;
p[0].append(('sle', temp3, temp1, temp2))
pass
def p_binary_expression_09(p):
'''binary_expression : expression GREATER_EQ expression '''
global temp_count;
if(type(p[1]) == list) :
p[0] = p[1];
temp1 = p[1][-1][1];
else :
p[0] = []
temp1 = p[1][1]
if(type(p[3]) == list) :
p[0].extend(p[3]);
temp2 = p[3][-1][1];
else :
temp2 = p[3][1]
temp3 = "$$t" + str(temp_count)
temp_count = temp_count + 1;
p[0].append(('sle', temp3, temp2, temp1))
pass
def p_binary_expression_10(p):
'''binary_expression : expression PLUS expression '''
global temp_count;
if(type(p[1]) == list) :
p[0] = p[1];
temp1 = p[1][-1][1];
else :
p[0] = []
temp1 = p[1][1]
if(type(p[3]) == list) :
p[0].extend(p[3]);
temp2 = p[3][-1][1];
else :
temp2 = p[3][1]
temp3 = "$$t" + str(temp_count)
temp_count = temp_count + 1;
p[0].append(('add', temp3, temp1, temp2))
pass
def p_binary_expression_11(p):
'''binary_expression : expression MINUS expression '''
global temp_count;
if(type(p[1]) == list) :
p[0] = p[1];
temp1 = p[1][-1][1];
else :
p[0] = []
temp1 = p[1][1]
if(type(p[3]) == list) :
p[0].extend(p[3]);
temp2 = p[3][-1][1];
else :
temp2 = p[3][1]
temp3 = "$$t" + str(temp_count)
temp_count = temp_count + 1;
p[0].append(('sub', temp3, temp1, temp2))
pass
def p_binary_expression_12(p):
'''binary_expression : expression ASTERISK expression '''
global temp_count;
if(type(p[1]) == list) :
p[0] = p[1];
temp1 = p[1][-1][1];
else :
p[0] = []
temp1 = p[1][1]
if(type(p[3]) == list) :
p[0].extend(p[3]);
temp2 = p[3][-1][1];
else :
temp2 = p[3][1]
temp3 = "$$t" + str(temp_count)
temp_count = temp_count + 1;
p[0].append(('mul', temp3, temp1, temp2))
pass
def p_binary_expression_13(p):
'''binary_expression : expression DIV expression '''
global temp_count;
if(type(p[1]) == list) :
p[0] = p[1];
temp1 = p[1][-1][1];
else :
p[0] = []
temp1 = p[1][1]
if(type(p[3]) == list) :
p[0].extend(p[3]);
temp2 = p[3][-1][1];
else :
temp2 = p[3][1]
temp3 = "$$t" + str(temp_count)
temp_count = temp_count + 1;
p[0].append(('div', temp3, temp1, temp2))
pass
def p_binary_expression_14(p):
'''binary_expression : expression EQ_PLUS expression '''
global temp_count;
p[0] = []
temp3 = "$$t" + str(temp_count)
temp_count = temp_count + 1;
temp4 = "$$t" + str(temp_count)
temp_count = temp_count + 1;
if(type(p[1]) == list) :
p[0] = p[1];
temp1 = p[1][-1][1];
else :
p[0] = []
temp1 = p[1][1]
if(type(p[3]) == list) :
p[0].extend(p[3]);
temp2 = p[3][-1][1];
else :
temp2 = p[3][1]
p[0].append(("add", temp3, temp1, temp2))
p[0].append(("cpy", temp1, temp3, ""))
pass
def p_binary_expression_15(p):
'''binary_expression : expression EQ_MINUS expression '''
global temp_count;
p[0] = []
temp3 = "$$t" + str(temp_count)
temp_count = temp_count + 1;
temp4 = "$$t" + str(temp_count)
temp_count = temp_count + 1;
if(type(p[1]) == list) :
p[0] = p[1];
temp1 = p[1][-1][1];
else :
p[0] = []
temp1 = p[1][1]
if(type(p[3]) == list) :
p[0].extend(p[3]);
temp2 = p[3][-1][1];
else :
temp2 = p[3][1]
p[0].append(("sub", temp3, temp1, temp2))
p[0].append(("cpy", temp1, temp3, ""))
pass
def p_binary_expression_16(p):
'''binary_expression : expression EQ_TIMES expression '''
global temp_count;
p[0] = []
temp3 = "$$t" + str(temp_count)
temp_count = temp_count + 1;
temp4 = "$$t" + str(temp_count)
temp_count = temp_count + 1;
if(type(p[1]) == list) :
p[0] = p[1];
temp1 = p[1][-1][1];
else :
p[0] = []
temp1 = p[1][1]
if(type(p[3]) == list) :
p[0].extend(p[3]);
temp2 = p[3][-1][1];
else :
temp2 = p[3][1]
p[0].append(("mul", temp3, temp1, temp2))
p[0].append(("cpy", temp1, temp3, ""))
pass
def p_binary_expression_17(p):
'''binary_expression : expression EQ_DIV expression '''
global temp_count;
p[0] = []
temp3 = "$$t" + str(temp_count)
temp_count = temp_count + 1;
temp4 = "$$t" + str(temp_count)
temp_count = temp_count + 1;
if(type(p[1]) == list) :
p[0] = p[1];
temp1 = p[1][-1][1];
else :
p[0] = []
temp1 = p[1][1]
if(type(p[3]) == list) :
p[0].extend(p[3]);
temp2 = p[3][-1][1];
else :
temp2 = p[3][1]
p[0].append(("div", temp3, temp1, temp2))
p[0].append(("cpy", temp1, temp3, ""))
pass
def p_unary_expression_01(p):
'''unary_expression : PLUS expression '''
p[0] = p[2]
pass
#Added for i++ and ++i
def p_unary_expression_02(p):
'''unary_expression : DOUBLE_PLUS primary_expression '''
global temp_count;
p[0] = []
temp1 = p[2][1]
temp2 = "$$t" + str(temp_count)
temp_count = temp_count + 1;
temp3 = "$$t" + str(temp_count)
temp_count = temp_count + 1;
p[0].append(("ldc", temp2, "1", ""))
p[0].append(("add", temp3, temp1, temp2))
p[0].append(("cpy", temp1, temp3, ""))
pass
def p_unary_expression_03(p):
'''unary_expression : primary_expression DOUBLE_PLUS'''
global temp_count;
p[0] = []
temp2 = "$$t" + str(temp_count)
temp_count = temp_count + 1;
temp3 = "$$t" + str(temp_count)
temp_count = temp_count + 1;
p[0].append(("ldc", temp2, "1", ""))
p[0].append(("add", temp3, p[1][1], temp2))
p[0].append(("cpy", p[1][1], temp3, ""))
pass
##
def p_unary_expression_021(p):
'''unary_expression : DOUBLE_MINUS primary_expression '''
global temp_count;
p[0] = []
temp1 = p[2][1]
temp2 = "$$t" + str(temp_count)
temp_count = temp_count + 1;
temp3 = "$$t" + str(temp_count)
temp_count = temp_count + 1;
p[0].append(("ldc", temp2, "1", ""))
p[0].append(("sub", temp3, temp1, temp2))
p[0].append(("cpy", temp1, temp3, ""))
pass
def p_unary_expression_031(p):
'''unary_expression : primary_expression DOUBLE_MINUS'''
global temp_count;
p[0] = []
temp2 = "$$t" + str(temp_count)
temp_count = temp_count + 1;
temp3 = "$$t" + str(temp_count)
temp_count = temp_count + 1;
p[0].append(("ldc", temp2, "1", ""))
p[0].append(("sub", temp3, p[1][1], temp2))
p[0].append(("cpy", p[1][1], temp3, ""))
pass
def p_unary_expression_04(p):
'''unary_expression : MINUS expression %prec UMINUS'''
global temp_count;
if(type(p[2]) == list) :
p[0] = p[2];
temp1 = p[2][-1][1];
else :
p[0] = []
temp1 = p[2][1]
temp3 = "$$t" + str(temp_count)
temp_count = temp_count + 1;
p[0].append(('neg', temp3, temp1, ""))
pass
def p_unary_expression_05(p):
'''unary_expression : ASTERISK expression %prec UASTERISK'''
global temp_count;
if(type(p[2]) == list) :
p[0] = p[2];
temp1 = p[2][-1][1];
else :
p[0] = []
temp1 = p[2][1]
temp3 = "$$t" + str(temp_count)
temp_count = temp_count + 1;
p[0].append(('load', temp3, temp1, ""))
pass
def p_unary_expression_06(p):
'''unary_expression : EXCLAMATION expression %prec UEXCLAMATION'''
global temp_count;
if(type(p[2]) == list) :
p[0] = p[2];
temp1 = p[2][-1][1];
else :
p[0] = []
temp1 = p[2][1]
temp3 = "$$t" + str(temp_count)
temp_count = temp_count + 1;
p[0].append(('not', temp3, temp1, ""))
def p_unary_expression_061(p): #Temp
'''unary_expression : AMPERSAND expression %prec AMPERSAND'''
p[0] = p[2]
pass
#def p_postfix_expression_01(p):
# '''postfix_expression : primary_expression'''
# t[0] = t[1]
def p_postfix_expression_02(p):
'''postfix_expression : primary_expression LPAREN argument_expression_list RPAREN'''
global temp_count;
temp1 = "$$t" + str(temp_count) # A dirty hack :)
temp2 = "$$*t" + str(temp_count) # A dirty hack :)
temp_count = temp_count + 1;
# Need to work on this TODO
p[0] = [('ldc', temp1, "&" + p[1][1][2:], "0")] # The 2: is to remove $$
temp_list = ["call", "", temp2]
if(type(p[3]) == list):
for x in p[3]:
if (x[0][:2] == "__"):
continue;
p[0].append(x)
for i in p[3]:
j = i[1]
temp_list.append(j)
temp_tuple = tuple(temp_list)
# p[0].append(("call", temp2, '(' + t_str +')', ""))
p[0].append(temp_tuple)
pass
def p_postfix_expression_03(p):
'''postfix_expression : primary_expression LPAREN RPAREN'''
global temp_count;
temp1 = "$$t" + str(temp_count)
temp_count = temp_count + 1;
# Need to work on this TODO
p[0] = [('ldc', temp1, "&" + p[1][1][2:], "0")]
p[0].append(("call", "", '*' + temp1, "()"))
pass
#def p_postfix_expression_04(p):
# '''postfix_expression : postfix_expression LBRACKET expression RBRACKET'''
# pass
def p_argument_expression_list_01(p):
'''argument_expression_list : expression'''
p[0] = p[1]
pass
def p_argument_expression_list_02(p):
'''argument_expression_list : expression COMMA argument_expression_list'''
p[0] = [];
if (type(p[1]) == list):
p[0].extend(p[1]);
else:
p[0].append(p[1])
if (type(p[3]) == list):
p[0].extend(p[3]);
else:
p[0].append(p[3])
pass
def p_primary_expression_01(p):
'''primary_expression : ID'''
p[0] = ("__ID", "@@" + p[1], "","")
pass
def p_primary_expression_02(p):
'''primary_expression : INUMBER'''
global temp_count;
temp1 = "$$t" + str(temp_count);
temp_count = temp_count + 1;
p[0] = [("ldc", temp1, p[1], "")]
pass
def p_primary_expression_03(p):
'''primary_expression : FNUMBER'''
global temp_count;
temp1 = "$$t" + str(temp_count);
temp_count = temp_count + 1;
p[0] = [("ldc", temp1, p[1], "")]
pass
def p_primary_expression_04(p):
'''primary_expression : CHARACTER'''
p[0] = ("__CONST", p[1], "","")
pass
def p_primary_expression_05(p):
'''primary_expression : string_literal'''
p[0] = p[1]
pass
def p_primary_expression_06(p):
'''primary_expression : LPAREN expression RPAREN'''
p[0] = p[2]
pass
def p_string_literal_01(p):
'''string_literal : STRING'''
global string_count;
global temp_count;
temp1 = "$$t" + str(temp_count);
temp_count = temp_count + 1;
temp_str = "&__tmp_string_" + str(string_count)
string_count = string_count + 1
p[0] = [("ldc", temp1, temp_str, "0")]
pass
#def p_string_literal_02(p):
# '''string_literal : string_literal STRING'''
# pass
def p_jump_statement_01(p):
'''jump_statement : RETURN SEMICOLON'''
p[0] = [("ret", "", "", "")]
pass
def p_jump_statement_02(p):
'''jump_statement : RETURN expression SEMICOLON'''
p[0] = []
if type(p[2]) == list :
p[0].extend(p[2])
ret = p[2][-1][1]
else :
ret = p[2][1]
p[0].append(("ret", ret, "", ""))
pass
def p_jump_statement_03(p):
'''jump_statement : BREAK SEMICOLON'''
p[0] = [("~~break", "", "", "")]
pass
def p_jump_statement_04(p):
'''jump_statement : CONTINUE SEMICOLON'''
p[0] = [("~~continue", "", "", "")]
pass
def p_jump_statement_05(p) :
'''jump_statement : GOTO ID SEMICOLON'''
p[0] = [("jmp", p[2], "", "")]
pass
def p_iteration_statement_01(p):
'''iteration_statement : WHILE LPAREN expression RPAREN statement'''
global label_count;
p[0] = []
temp_lab1 = "~~L" + str(label_count) # For begining of loop
label_count = label_count + 1
p[0].append((temp_lab1 + ":", "", "", ""))
temp_lab2 = "~~L" + str(label_count) # For end of loop
label_count = label_count + 1
if type(p[3]) == list:
p[0].extend(p[3])
cond = p[3][-1][1];
else :
cond = "Issues"
p[0].append(("bfls", cond, "", temp_lab2))
# Searching for break and continue
if(type(p[5]) == list) :
for i in range(len(p[5])) :
if(p[5][i][0] == "~~break") :
p[5][i] = ("jmp", temp_lab2, "", "")
if(p[5][i][0] == "~~continue") :
p[5][i] = ("jmp", temp_lab1, "", "")
p[0].extend(p[5])
p[0].append(("jmp", temp_lab1, "", ""));
p[0].append((temp_lab2 + ":", "", "", ""));
pass
def p_iteration_statement_02(p):
'''iteration_statement : DO statement WHILE LPAREN expression RPAREN SEMICOLON'''
global label_count;
is_continue = 0;
is_break = 0;
p[0] = []
temp_lab1 = "~~L" + str(label_count) # For begining of loop
label_count = label_count + 1
p[0].append((temp_lab1 + ":", "", "", ""))
temp_lab2 = "~~L" + str(label_count) # For end of loop
label_count = label_count + 1
temp_lab3 = ""
# Searching for break and continue
if(type(p[2]) == list) :
for i in range(len(p[2])) :
if(p[2][i][0] == "~~break") :
p[2][i] = ("jmp", temp_lab2, "", "")
is_break = 1;
if(p[2][i][0] == "~~continue") :
temp_lab3 = "~~L" + str(label_count) # check for continue
label_count = label_count + 1
p[2][i] = ("jmp", temp_lab3, "", "")
is_continue = 1;
p[0].extend(p[2])
if is_continue :
p[0].append((temp_lab3 + ":", "", "", ""));
if type(p[5]) == list:
p[0].extend(p[5])
cond = p[5][-1][1];
else :
cond = "Issues"
p[0].append(("btru", cond, "", temp_lab1))
if is_break:
p[0].append((temp_lab2 + ":", "", "", ""));
pass
def p_iteration_statement_03(p):
'''iteration_statement : FOR LPAREN expression_statement expression_statement expression RPAREN statement'''
global label_count;
p[0] = []