-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathyacc.py
More file actions
868 lines (638 loc) · 26.1 KB
/
yacc.py
File metadata and controls
868 lines (638 loc) · 26.1 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
import ply.yacc as yacc
import lex
from env.var import Var, i
from env.quad_ruple import QuadRuple
from env.op import Operator
qr = QuadRuple()
var = lex.variables
tokens = lex.tokens
result = open('res.c', 'w')
zero = Var(place=0, type='int', value=0)
zero.set_unique()
precedence = (
('left', 'OR_KW', 'SHORT_CIRCUIT_OR_KW'),
('left', 'AND_KW', 'SHORT_CIRCUIT_AND_KW'),
('left', 'EQ'),
('left', 'LT', 'GT', 'LE', 'GE'),
('left', 'PLUS', 'MINUS'),
('left', 'MOD'),
('left', 'TIMES', 'DIVIDE'),
# ('left', 'INT_KW', 'FLOAT_KW', 'CHAR_KW'),
('right', 'NOT_KW', 'MINUS_MINUS', 'PLUS_PLUS'),
)
def p_barnameh(p):
""" barnameh : PROGRAM_KW SHENASE tarifha """
# print('Rule #1 \t: barnameh -> PROGRAM_KW SHENASE tarifha')
# var[p[2]].type = 'Program'
del var[p[2]]
def p_tarifha_1(p):
""" tarifha : tarifha tarif """
# print('Rule #2-1 \t: tarifha -> tarifha tarif')
pass
def p_tarifha_2(p):
""" tarifha : tarif """
# print('Rule #2-2 \t: tarifha -> tarif')
pass
def p_tarif_1(p):
""" tarif : tarifeSakhtar """
# print('Rule #3-1 \t: tarif -> tarifeSakhtar')
pass
def p_tarif_2(p):
""" tarif : tarifeMoteghayyer """
# print('Rule #3-2 \t: tarif -> tarifeMoteghayyer')
pass
def p_tarif_3(p):
""" tarif : tarifeTabe """
# print('Rule #3-3 \t: tarif -> tarifeTabe')
pass
def p_tarifeSakhtar(p):
""" tarifeSakhtar : STRUCT_KW SHENASE OPEN_BRACE tarifhayeMahalli CLOSE_BRACE """
# print('Rule #4 \t: tarifeSakhtar -> STRUCT_KW SHENASE OPEN_BRACE tarifhayeMahalli CLOSE_BRACE ')
# TODO handle kon
def p_tarifhayeMahalli_1(p):
""" tarifhayeMahalli : tarifhayeMahalli tarifMoteghayyereMahdud """
# print('Rule #5-1 \t: tarifhayeMahalli -> tarifhayeMahalli tarifMoteghayyereMahdud')
pass
def p_tarifhayeMahalli_2(p):
""" tarifhayeMahalli : epsilon """
# print('Rule #5-2 \t: tarifhayeMahalli -> epsilon')
pass
def p_tarifMoteghayyereMahdud(p):
""" tarifMoteghayyereMahdud : jenseMahdud tarifhayeMoteghayyerha NOGHTE_VIRGUL """
# print('Rule #6 \t: tarifMoteghayyereMahdud -> jensMahdud tarifhayeMoteghayyerha NOGHTE_VIRGUL')
if isinstance(p[2], list):
for v in p[2]:
if v.type is None or v.type == p[1].type:
v.type = p[1].type
var[v.place].type = v.type
var[v.place].value = v.value
# code += p[1].type + v.code + ';' # TODO?
# print(str(var[v.place]))
else:
raise Exception('type mismatch error near ' + v.place + ' = ' + str(v.value))
else:
if p[2].type is None or p[2].type == p[1].type:
p[2].type = p[1].type
var[p[2].place].type = p[2].type
var[p[2].place].value = p[2].value
# code += p[1].type + p[2].code + ';' # TODO?
# print(str(var[v.place]))
else:
raise Exception('type mismatch error near ' + p[2].place + ' = ' + str(p[2].value))
def p_jenseMahdud_1(p):
""" jenseMahdud : CONST_KW jens """
# print('Rule #7-1 \t: jenseMahdud -> CONST_KW jens')
p[0] = p[1]
p[0].is_const = True
def p_jenseMahdud_2(p):
""" jenseMahdud : jens """
# print('Rule #7-2 \t: jenseMahdud -> jens')
p[0] = p[1]
def p_jens_1(p):
""" jens : INT_KW """
# print('Rule #8-1 \t: jens -> INT_KW')
p[0] = Var(type='int')
def p_jens_2(p):
""" jens : FLOAT_KW """
# print('Rule #8-2 \t: jens -> FLOAT_KW')
p[0] = Var(type='float')
def p_jens_3(p):
""" jens : BOOL_KW """
# print('Rule #8-3 \t: jens -> BOOL_KW')
p[0] = Var(type='bool')
def p_jens_4(p):
""" jens : CHAR_KW """
# print('Rule #8-4 \t: jens -> CHAR_KW')
p[0] = Var(type='char')
def p_tarifeMoteghayyer(p):
""" tarifeMoteghayyer : jens tarifhayeMoteghayyerha NOGHTE_VIRGUL """
# print('Rule #9 \t: tarifeMoteghayyer -> jens tarifhayeMoteghayyerha NOGHTE_VIRGUL')
for v in p[2]:
if v.type is None or v.type == p[1].type:
v.type = p[1].type
else:
raise Exception('type mismatch error near ' + v.place + ' = ' + str(v.value))
def p_tarifhayeMoteghayyerha_1(p):
""" tarifhayeMoteghayyerha : tarifeMeghdareAvvalie """
# print('Rule #10-1 \t: tarifhayeMoteghayyerha -> tarifeMeghdareAvvalie')
p[0] = p[1]
def p_tarifhayeMoteghayyerha_2(p):
""" tarifhayeMoteghayyerha : tarifhayeMoteghayyerha COMMA tarifeMeghdareAvvalie """
# print('Rule #10-2 \t: tarifhayeMoteghayyerha -> tarifhayeMoteghayyerha COMMA tarifeMeghdarAvvalie')
p[1].append(p[3])
p[0] = p[1]
def p_tarifeMeghdareAvvalie_1(p):
""" tarifeMeghdareAvvalie : tarifeShenaseyeMoteghayyer """
# print('Rule #11-1 \t: tarifeMeghdarAvvalie -> tarifeShenaseyeMoteghayyer')
p[0] = [p[1]]
def p_tarifeMeghdareAvvalie_2(p):
""" tarifeMeghdareAvvalie : tarifeShenaseyeMoteghayyer EQUALS ebarateSade """
# print('Rule #11-2 \t: tarifeMeghdarAvvalie -> tarifeShenaseyeMoteghayyer EQUALS ebarateSade')
p[1].value, p[1].type = p[3].value, p[3].type
p[0] = p[1]
def p_tarifeShenaseyeMoteghayyer_1(p):
""" tarifeShenaseyeMoteghayyer : SHENASE """
# print('Rule #12-1 \t: tarifeShenaseyeMoteghayyer -> SHENASE')
p[0] = Var(place=p[1])
def p_tarifeShenaseyeMoteghayyer_2(p):
""" tarifeShenaseyeMoteghayyer : SHENASE OPEN_BRACKET INT CLOSE_BRACKET """
# print('Rule #12-2 \t: tarifeShenaseyeMoteghayyer -> SHENASE OPEN_BRACKET INT CLOSE_BRACKET ')
p[0] = Var(place=p[1])
p[0].len = int(p[3])
var[p[0].place].len = len(p[0])
# def p_tarifeShenaseyeMoteghayyer_3(p):
# """ tarifeShenaseyeMoteghayyer : SHENASE OPEN_BRACKET FLOAT CLOSE_BRACKET """
# # print('Rule #12-3 \t: tarifeShenaseyeMoteghayyer -> SHENASE OPEN_BRACKET FLOAT CLOSE_BRACKET ')
def p_tarifeTabe_1(p):
""" tarifeTabe : jens SHENASE OPEN_PAREN vorudi CLOSE_PAREN jomle """
# print('Rule #13-1 \t: tarifeTabe -> jens SHENASE OPEN_PAREN vorudi CLOSE_PAREN jomle')
del var[p[2]]
def p_tarifeTabe_2(p):
""" tarifeTabe : SHENASE OPEN_PAREN vorudi CLOSE_PAREN jomle """
# print('Rule #13-2 \t: tarifeTabe -> SHENASE OPEN_PAREN vorudi CLOSE_PAREN jomle')
del var[p[2]]
def p_vorudi_1(p):
""" vorudi : vorudiha """
# print('Rule #14-1 \t: vorudi -> vorudiha')
pass
def p_vorudi_2(p):
""" vorudi : epsilon """
# print('Rule #14-2 \t: vorudi -> epsilon ')
pass
def p_vorudiha_1(p):
""" vorudiha : vorudiha NOGHTE_VIRGUL jensVorudiha """
# print('Rule #15-1 \t: vorudiha -> vorudiha NOGHTE_VIRGUL jensVorudiha')
pass
def p_vorudiha_2(p):
""" vorudiha : jensVorudiha """
# print('Rule #15-2 \t: vorudiha -> jensVorudiha')
pass
def p_jensVorudiha(p):
""" jensVorudiha : jens shenaseyeVorudiha """
# print('Rule #16 \t: jensVorudiha -> jens shenaseyeVorudiha')
pass
def p_shenaseyeVorudiha_1(p):
""" shenaseyeVorudiha : shenaseyeVorudiha COMMA shenaseyeVorudi """
# print('Rule #17-1 \t: shenaseyeVorudiha -> shenaseyeVorudiha COMMA shenaseyeVorudi')
pass
def p_shenaseyeVorudiha_2(p):
""" shenaseyeVorudiha : shenaseyeVorudi """
# print('Rule #17-2 \t: shenaseyeVorudiha -> shenaseyeVorudi')
pass
def p_shenaseyeVorudi_1(p):
""" shenaseyeVorudi : SHENASE """
# print('Rule #18-1 \t: shenaseyeVorudi -> SHENASE')
pass
def p_shenaseyeVorudi_2(p):
""" shenaseyeVorudi : SHENASE OPEN_BRACKET CLOSE_BRACKET """
# print('Rule #18-2 \t: shenaseyeVorudi -> SHENASE OPEN_BRACKET CLOSE_BRACKET')
pass
def p_jomle_1(p):
""" jomle : jomleyeMorakkab """
# print('Rule #19-1 \t: jomle -> jomleyeMorakkab')
def p_jomle_2(p):
""" jomle : jomleyeEbarat """
# print('Rule #19-2 \t: jomle -> jomleyeEbarat')
def p_jomle_3(p):
""" jomle : jomleyeEntekhab """
# print('Rule #19-3 \t: jomle -> jomleyeEntekhab')
def p_jomle_4(p):
""" jomle : jomleyeTekrar """
# print('Rule #19-4 \t: jomle -> jomleyeTekrar')
def p_jomle_5(p):
""" jomle : jomleyeBazgasht """
# print('Rule #19-5 \t: jomle -> jomleyeBazgasht')
def p_jomle_6(p):
""" jomle : jomleyeShekast """
# print('Rule #19-6 \t: jomle -> jomeleyeShekast')
def p_jomleyeMorakkab(p):
""" jomleyeMorakkab : OPEN_BRACE tarifhayeMahalli jomleha CLOSE_BRACE """
# print('Rule #20 \t: jomleyeMorakkab -> OPEN_BRACE tarifhayeMahalli jomleha CLOSE_BRACE')
def p_jomleha_1(p):
""" jomleha : jomleha jomle """
# print('Rule #21-1 \t: jomleha -> jomleha jomle')
pass
def p_jomleha_2(p):
""" jomleha : epsilon """
# print('Rule #21-1 \t: jomleha -> epsilon ')
pass
def p_jomleyeEbarat_1(p):
""" jomleyeEbarat : ebarat NOGHTE_VIRGUL """
# print('Rule #22-1 \t: jomleyeEbarat -> ebarat ;')
def p_jomleyeEbarat_2(p):
""" jomleyeEbarat : NOGHTE_VIRGUL """
# print('Rule #22-2 \t: jomleyeEbarat -> ;')
def p_jomleyeEntekhab_1(p):
""" jomleyeEntekhab : IF_KW ebarateSade THEN_KW epsilon jomle """
# print('Rule #23-1 \t: jomleyeEntekhab -> IF_KW ebarateSade THEN_KW jomle')
p[0] = Var();
Var.backPatching(p[2].truelist ,qr ,str(p[4].quad));
Var.backPatching(p[2].falesList ,qr ,str(len(qr)));
def p_jomleyeEntekhab_2(p):
""" jomleyeEntekhab : IF_KW ebarateSade THEN_KW epsilon jomle ELSE_KW epsilon jomle """
# print('Rule #23-2 \t: jomleyeEntekhab -> IF_KW ebarateSade THEN_KW jomle ELSE_KW jomle')
p[0] = Var();
Var.backPatching(p[2].truelist ,qr ,str(p[4].quad));
Var.backPatching(p[2].falseList ,qr ,str(p[7].quad));
def p_jomleyeEntekhab_3(p):
""" jomleyeEntekhab : SWITCH_KW OPEN_PAREN ebarateSade CLOSE_PAREN onsoreHalat onsorePishfarz END_KW """
# print('Rule #23-3 \t: jomleyeEntekhab -> SWITCH_KW OPEN_PAREN ebarateSade CLOSE_PAREN onsoreHalat '
# 'onsorePishfarz END_KW')
def p_onsoreHalat_1(p):
""" onsoreHalat : CASE_KW INT COLON jomle NOGHTE_VIRGUL """
# print('Rule #24-1 \t: onsoreHalat -> CASE_KW INT COLON jomle NOGHTE_VIRGUL')
def p_onsoreHalat_2(p):
""" onsoreHalat : CASE_KW FLOAT COLON jomle NOGHTE_VIRGUL """
# print('Rule #24-2 \t: onsoreHalat -> CASE_KW FLOAT COLON jomle NOGHTE_VIRGUL')
def p_onsoreHalat_3(p):
""" onsoreHalat : onsoreHalat CASE_KW INT COLON jomle NOGHTE_VIRGUL """
# print('Rule #24-3 \t: onsoreHalat -> onsoreHalat CASE_KW INT COLON jomle NOGHTE_VIRGUL')
def p_onsoreHalat_4(p):
""" onsoreHalat : onsoreHalat CASE_KW FLOAT COLON jomle NOGHTE_VIRGUL """
# print('Rule #24-4 \t: onsoreHalat -> onsoreHalat CASE_KW FLOAT COLON jomle NOGHTE_VIRGUL')
def p_onsorePishfarz_1(p):
""" onsorePishfarz : DEFAULT_KW COLON jomle NOGHTE_VIRGUL """
# print('Rule #25-1 \t: onsorePishfarz -> DEFAULT_KW COLON jomle NOGHTE_VIRGUL')
def p_onsorePishfarz_2(p):
""" onsorePishfarz : epsilon """
# print('Rule #25-2 \t: onsorePishfarz -> epsilon ')
def p_jomleyeTekrar(p):
""" jomleyeTekrar : WHILE_KW OPEN_PAREN epsilon ebarateSade CLOSE_PAREN epsilon jomle """
# print('Rule #26 \t: jomleyeTekrar -> WHILE_KW OPEN_PAREN ebarateSade CLOSE_PAREN jomle')
p[0] = Var();
Var.backPatching(p[7].truelist ,qr ,p[3].quad);
Var.backPatching(p[4].truelist ,qr ,p[6].quad);
p[7].nextlist = p[4].falselist;
qr.add('goto ' + str(p[3].quad) ,'' ,'' ,'');
def p_jomleyeBazgasht_1(p):
"""jomleyeBazgasht : RETURN_KW NOGHTE_VIRGUL """
# print('Rule #28-1 \t: jomleyeBazgasht -> RETURN_KW NOGHTE_VIRGUL')
def p_jomleyeBazgasht_2(p):
""" jomleyeBazgasht : RETURN_KW ebarat NOGHTE_VIRGUL """
# print('Rule #28-2 \t: jomleyeBazgasht -> RETURN_KW ebarat NOGHTE_VIRGUL')
def p_jomleyeShekast(p):
""" jomleyeShekast : BREAK_KW NOGHTE_VIRGUL """
# print('Rule #29 \t: jomleyeShekast -> BREAK_KW NOGHTE_VIRGUL')
# TODO array in left dosnt handled
def p_ebarat_1(p):
""" ebarat : taghirpazir EQUALS ebarat """
# print('Rule #30-1 \t: ebarat -> taghirpazir EQUALS ebarat')
if p[1].type == 'int' and p[3].type == 'float':
raise Exception('type mismatch error near ' + p[1].place + ' = ' + p[3].place)
else:
if p[1].len is None:
p[0] = Var(place=p[1].place + '=' + p[3].place, type=p[1].type)
p[0].set_unique()
qr.add(p[1], '=', p[3])
p[0].code = p[1].unique + '=' + p[3].unique
else:
# print(p[0], p[1], p[2], p[3])
# pass
# p[0] = Var(place=p[1].place + '[]=' + p[3].place, type=p[1].type)
# p[0].set_unique()
qr.add(p[1], '[]=', p[1].len, p[3])
# p[0].code = p[1].unique + '=' + p[3].unique
# TODO correct += and others
def p_ebarat_2(p):
""" ebarat : taghirpazir PLUS_EQ ebarat """
# print('Rule #30-2 \t: ebarat -> taghirpazir PLUS_EQ ebarat')
if True:
p[0] = Var(place=p[1].place + '+' + p[3].place, type=p[1].type)
p[0].set_unique()
qr.add(p[0], '+', p[1], p[3])
p[0].code = p[1].unique + '+=' + p[3].unique
else:
raise Exception('type mismatch error near ' + p[1].place + ' += ' + p[3].place)
def p_ebarat_3(p):
""" ebarat : taghirpazir MINUS_EQ ebarat """
# print('Rule #30-3 \t: ebarat -> taghirpazir MINUS_EQ ebarat')
if True:
p[0] = Var(place=p[1].place + '-' + p[3].place, type=p[1].type)
p[0].set_unique()
qr.add(p[0], '-', p[1], p[3])
p[0].code = p[1].unique + '-=' + p[3].unique
else:
raise Exception('type mismatch error near ' + p[1].place + ' -= ' + p[3].place)
def p_ebarat_4(p):
""" ebarat : taghirpazir TIMES_EQ ebarat """
# print('Rule #30-4 \t: ebarat -> taghirpazir TIMES_EQ ebarat')
if True:
p[0] = Var(place=p[1].place + '*' + p[3].place, type=p[1].type)
p[0].set_unique()
qr.add(p[0], '*', p[1], p[3])
p[0].code = p[1].unique + '*=' + p[3].unique
else:
raise Exception('type mismatch error near ' + p[1].place + ' *= ' + p[3].place)
def p_ebarat_5(p):
""" ebarat : taghirpazir DIVIDE_EQ ebarat """
# print('Rule #30-5 \t: ebarat -> taghirpazir DIVIDE_EQ ebarat')
if p[3].value == 0:
p[0] = Var(place=p[1].place + '/' + p[3].place, type=p[1].type)
p[0].set_unique()
qr.add(p[0], '/', p[1], p[3])
p[0].code = p[1].unique + '/=' + p[3].unique
else:
raise Exception('division by zero ' + p[1].place + ' /= ' + p[3].place)
def p_ebarat_6(p):
""" ebarat : taghirpazir PLUS_PLUS """
# print('Rule #30-6 \t: ebarat -> taghirpazir PLUS_PLUS')
p[0] = Var(place=p[1].place + '++')
qr.add(p[0], '++')
p[0].code = p[1].unique + '++'
def p_ebarat_7(p):
""" ebarat : taghirpazir MINUS_MINUS """
# print('Rule #30-7 \t: ebarat -> taghirpazir MINUS_MINUS')
p[0] = Var(place=p[1].place + '--')
qr.add(p[0], '--')
p[0].code = p[1].unique + '--'
def p_ebarat_8(p):
""" ebarat : ebarateSade """
# print('Rule #30-8 \t: ebarat -> ebarateSade')
p[0] = p[1]
def p_ebarateSade_1(p):
""" ebarateSade : ebarateSade OR_KW ebarateSade """
# print('Rule #31-1 \t: ebarateSade -> ebarateSade OR_KW ebarateSade')
if p[1].type == 'bool' and p[3].type == 'bool':
p[0] = Var(place=p[1].place + '||' + p[3].place, type='bool')
p[0].set_unique()
qr.add(p[0], '||', p[1], p[3])
p[0].code = p[1].unique + '||' + p[3].unique
else:
tmp1 = Var(place=p[1].place + '!= 0', type='bool')
tmp1.set_unique()
tmp1.code = p[1].unique + '!= 0'
qr.add(tmp1, '!=', p[1], zero)
tmp2 = Var(place=p[3].place + '!= 0', type='bool')
tmp2.set_unique()
tmp2.code = p[3].unique + '!= 0'
qr.add(tmp2, '!=', p[3], zero)
p[0] = Var(place=tmp1.place + '||' + tmp2.place, type='bool')
p[0].set_unique()
qr.add(p[0], '||', tmp1, tmp2)
p[0].code = tmp1.unique + '||' + tmp2.unique
def p_ebarateSade_2(p):
""" ebarateSade : ebarateSade AND_KW ebarateSade """
# print('Rule #31-2 \t: ebarateSade -> ebarateSade AND_KW ebarateSade')
if p[1].type == 'bool' and p[3].type == 'bool':
p[0] = Var(place=p[1].place + '&&' + p[3].place, type='bool')
p[0].set_unique()
qr.add(p[0], '&', p[1], p[3])
p[0].code = p[1].unique + '&&' + p[3].unique
else:
tmp1 = Var(place=p[1].place + '!= 0', type='bool')
tmp1.set_unique()
tmp1.code = p[1].unique + '!= 0'
qr.add(tmp1, '!=', p[1], zero)
tmp2 = Var(place=p[3].place + '!= 0', type='bool')
tmp2.set_unique()
tmp2.code = p[3].unique + '!= 0'
qr.add(tmp2, '!=', p[3], zero)
p[0] = Var(place=tmp1.place + '&&' + tmp2.place, type='bool')
p[0].set_unique()
qr.add(p[0], '&&', tmp1, tmp2)
p[0].code = tmp1.unique + '&&' + tmp2.unique
def p_ebarateSade_3(p):
""" ebarateSade : ebarateSade SHORT_CIRCUIT_OR_KW ebarateSade """
# print('Rule #31-3 \t: ebarateSade -> ebarateSade SHORT_CIRCUIT_OR_KW ebarateSade')
if p[1].type == 'bool' and p[3].type == 'bool':
p[0] = Var(place=p[1].place + '|' + p[3].place, type='bool')
p[0].set_unique()
qr.add(p[0], '&', p[1], p[3])
p[0].code = p[1].unique + '|' + p[3].unique
else:
tmp1 = Var(place=p[1].place + '!= 0', type='bool')
tmp1.set_unique()
tmp1.code = p[1].unique + '!= 0'
qr.add(tmp1, '!=', p[1], zero)
tmp2 = Var(place=p[3].place + '!= 0', type='bool')
tmp2.set_unique()
tmp2.code = p[3].unique + '!= 0'
qr.add(tmp2, '!=', p[3], zero)
p[0] = Var(place=tmp1.place + '|' + tmp2.place, type='bool')
p[0].set_unique()
qr.add(p[0], '&', tmp1, tmp2)
p[0].code = tmp1.unique + '|' + tmp2.unique
def p_ebarateSade_4(p):
""" ebarateSade : ebarateSade SHORT_CIRCUIT_AND_KW ebarateSade """
# print('Rule #31-4 \t: ebarateSade -> ebarateSade SHORT_CIRCUIT_AND_KW ebarateSade')
if p[1].type == 'bool' and p[3].type == 'bool':
p[0] = Var(place=p[1].place + '&' + p[3].place, type='bool')
p[0].set_unique()
qr.add(p[0], '&', p[1], p[3])
p[0].code = p[1].unique + '&' + p[3].unique
else:
tmp1 = Var(place=p[1].place + '!= 0', type='bool')
tmp1.set_unique()
tmp1.code = p[1].unique + '!= 0'
qr.add(tmp1, '!=', p[1], zero)
tmp2 = Var(place=p[3].place + '!= 0', type='bool')
tmp2.set_unique()
tmp2.code = p[3].unique + '!= 0'
qr.add(tmp2, '!=', p[3], zero)
p[0] = Var(place=tmp1.place + '&' + tmp2.place, type='bool')
p[0].set_unique()
qr.add(p[0], '&', tmp1, tmp2)
p[0].code = tmp1.unique + '&' + tmp2.unique
def p_ebarateSade_5(p):
""" ebarateSade : NOT_KW ebarateSade """
# print('Rule #31-5 \t: ebarateSade -> NOT_KW ebarateSade')
if p[2].type == 'bool':
p[0] = Var(place='!' + p[2].place, type='bool')
p[0].set_unique()
qr.add(p[0], '!', p[1])
p[0].code = '!' + p[2].unique
else:
tmp = Var(place=p[2].place + '!= 0', type='bool')
tmp.set_unique()
tmp.code = p[2].unique + '!= 0'
qr.add(tmp, '==', p[2], zero)
p[0] = Var(place='!' + tmp.place, type='bool')
p[0].set_unique()
qr.add(p[0], '!', tmp)
p[0].code = '!' + tmp.unique
def p_ebarateSade_6(p):
""" ebarateSade : ebarateRabetei """
# print('Rule #31-6 \t: ebarateSade -> ebarateRabetei')
p[0] = p[1]
def p_ebarateRabetei_1(p):
""" ebarateRabetei : ebarateRiaziManteghi """
# print('Rule #32-1 \t: ebarateRabetei -> ebarateRiaziManteghi')
p[0] = p[1]
def p_ebarateRabetei_2(p):
""" ebarateRabetei : ebarateRiaziManteghi amalgareRabetei ebarateRiaziManteghi """
# print('Rule #32-2 \t: ebarateRabetei -> ebarateRiaziManteghi amalgareRabetei ebarateRiaziManteghi')
p[0] = Var(place=p[1].place + p[2].op + p[3].place, type='bool')
p[0].set_unique()
qr.add(p[0], p[2].op, p[1], p[3])
p[0].code = p[1].unique + p[2].op + p[3].unique
def p_amalgareRabetei_1(p):
""" amalgareRabetei : LT """
# print('Rule #33-1 \t: amalgareRabetei -> LT')
p[0] = Operator('<')
def p_amalgareRabetei_2(p):
""" amalgareRabetei : GT """
# print('Rule #33-2 \t: amalgareRabetei -> GT')
p[0] = Operator('>')
def p_amalgareRabetei_3(p):
""" amalgareRabetei : GE """
# print('Rule #33-3 \t: amalgareRabetei -> GE')
p[0] = Operator('>=')
def p_amalgareRabetei_4(p):
""" amalgareRabetei : LE """
# print('Rule #33-4 \t: amalgareRabetei -> LE')
p[0] = Operator('<=')
def p_amalgareRabetei_5(p):
""" amalgareRabetei : EQ """
# print('Rule #33-5 \t: amalgareRabetei -> EQ')
p[0] = Operator('==')
def p_ebarateRiaziManteghi_1(p):
""" ebarateRiaziManteghi : ebarateYegani """
# print('Rule #34-1 \t: ebarateRiaziManteghi -> ebarateYegani')
p[0] = p[1]
def p_ebarateRiaziManteghi_2(p):
"""ebarateRiaziManteghi : ebarateRiaziManteghi amalgareRiazi ebarateRiaziManteghi """
# print('Rule #34-2 \t: ebarateRiaziManteghi -> ebarateRiaziManteghi amalgareRiazi ebarateRiaziManteghi')
p[0] = Var(place=p[1].place + p[2].op + p[3].place)
if p[3].type == 'float' or p[1].type == 'float':
p[0].type = 'float'
else:
p[0].type = 'int'
p[0].set_unique()
qr.add(p[0], p[2].op, p[1], p[3])
p[0].code = p[1].unique + p[2].op + p[3].unique
def p_amalgareRiazi_1(p):
""" amalgareRiazi : PLUS """
# print('Rule #35-1 \t: amalgareRiazi -> PLUS')
p[0] = Operator('+')
def p_amalgareRiazi_2(p):
""" amalgareRiazi : MINUS """
# print('Rule #35-2 \t: amalgareRiazi -> MINUS')
p[0] = Operator('-')
def p_amalgareRiazi_3(p):
""" amalgareRiazi : TIMES """
# print('Rule #35-3 \t: amalgareRiazi -> TIMES')
p[0] = Operator('*')
def p_amalgareRiazi_4(p):
""" amalgareRiazi : DIVIDE """
# print('Rule #35-4 \t: amalgareRiazi -> DIVIDE')
p[0] = Operator('/')
def p_amalgareRiazi_5(p):
""" amalgareRiazi : MOD """
# print('Rule #35-5 \t: amalgareRiazi -> MOD')
p[0] = Operator('%')
def p_ebarateYegani_1(p):
""" ebarateYegani : amalgareYegani ebarateYegani """
# print('Rule #36-1 \t: ebarateYegani -> amalgareYegani ebarateYegani')
p[0] = p[2] # TODO add amalgareYegani
def p_ebarateYegani_2(p):
""" ebarateYegani : amel """
# print('Rule #36-2 \t: ebarateYegani -> amel')
p[0] = p[1]
def p_amalgareYegani_1(p):
""" amalgareYegani : MINUS """
# print('Rule #37-1 \t: amalgareYegani -> MINUS')
p[0] = Operator('') # TODO correct this
def p_amalgareYegani_2(p):
""" amalgareYegani : TIMES """
# print('Rule #37-2 \t: amalgareYegani -> TIMES')
p[0] = Operator('') # TODO correct this
def p_amalgareYegani_3(p):
""" amalgareYegani : QUESTION_MARK """
# print('Rule #37-3 \t: amalgareYegani -> QUESTION_MARK')
p[0] = Operator('') # TODO correct this
def p_amel_1(p):
""" amel : taghirpazir """
# print('Rule #38-1 \t: amel -> taghirpazir')
p[0] = p[1]
def p_amel_2(p):
""" amel : taghirnapazir """
# print('Rule #38-2 \t: amel -> taghirnapazir')
p[0] = p[1]
def p_taghirpazir_1(p):
""" taghirpazir : SHENASE """
# print('Rule #39-1 \t: taghirpazir -> SHENASE')
p[0] = var[p[1]]
def p_taghirpazir_2(p):
""" taghirpazir : taghirpazir OPEN_BRACKET ebarat CLOSE_BRACKET """
# print('Rule #39-2 \t: taghirpazir -> taghirpazir OPEN_BRACKET ebarat CLOSE_BRACKET')
# TODO test array
if p[3].value < len(var[p[1].place]):
p[0] = Var(place=p[1].place + '[' + p[3].place + ']')
p[0].set_unique()
# p[0].len = len(var[p[1].place])
p[0].len = p[3].place
qr.add(p[0], '=[]', p[1], p[3])
else:
raise Exception('array out of range ' + p[1].place + '[' + str(p[3].value) + ']')
def p_taghirpazir_3(p):
"""taghirpazir : taghirpazir DOT SHENASE """
# print('Rule #39-3 \t: taghirpazir -> taghirpazir DOT SHENASE')
# TODO structure handling
def p_taghirnapazir_1(p):
""" taghirnapazir : OPEN_PAREN ebarat CLOSE_PAREN """
# print('Rule #40-1 \t: taghirnapazir -> OPEN_PAREN ebarat CLOSE_PAREN')
p[0] = p[2]
def p_taghirnapazir_2(p):
""" taghirnapazir : sedaZadan """
# print('Rule #40-2 \t: taghirnapazir -> sedaZadan')
pass
def p_taghirnapazir_3(p):
"""taghirnapazir : meghdareSabet """
# print('Rule #40-3 \t: taghirnapazir -> meghdarSabet')
p[0] = p[1]
def p_sedaZadan(p):
""" sedaZadan : SHENASE OPEN_PAREN bordareVorudi CLOSE_PAREN """
# print('Rule #40 \t: sedaZadan -> SHENASE OPEN_PAREN bordareVorudi CLOSE_PAREN ')
pass
def p_bordareVorudi_1(p):
""" bordareVorudi : bordareVorudiha """
# print('Rule #41-1 \t: bordareVorudi -> bordareVorudiha')
pass
def p_bordareVorudi_2(p):
""" bordareVorudi : epsilon """
# print('Rule #41-2 \t: bordareVorudi -> epsilon ')
pass
def p_bordareVorudiha_1(p):
""" bordareVorudiha : bordareVorudiha COMMA ebarat """
# print('Rule #42-1 \t:s bordareVorudiha -> bordareVorudiha COMMA ebarat')
pass
def p_bordareVorudiha_2(p):
"""bordareVorudiha : ebarat """
# print('Rule #42-2 \t: bordareVorudiha -> ebarat')
pass
def p_meghdareSabet_1(p):
""" meghdareSabet : INT """
# print('Rule #43-1 \t: meghdareSabet -> INT ')
p[0] = Var(place=str(p[1]), type='int', value=p[1])
p[0].unique = p[0].place
def p_meghdareSabet_2(p):
""" meghdareSabet : FLOAT """
# print('Rule #43-2 \t: meghdareSabet -> FLOAT ')
p[0] = Var(place=str(p[1]), type='float', value=p[1])
p[0].unique = p[0].place
def p_meghdareSabet_3(p):
""" meghdareSabet : HARFE_SABET """
# print('Rule #43-3 \t: meghdareSabet -> HARFE_SABET ')
p[0] = Var(place=str(p[1]), type='char', value=p[1])
# TODO make value integer
p[0].unique = p[0].place
def p_meghdareSabet_4(p):
""" meghdareSabet : TRUE_KW """
# print('Rule #43-4 \t: meghdareSabet -> TRUE_KW ')
p[0] = Var(place=str(p[1]), type='bool', value=1)
p[0].unique = p[0].place
def p_meghdareSabet_5(p):
""" meghdareSabet : FALSE_KW """
# print('Rule #43-5 \t: meghdareSabet -> FALSE_KW ')
p[0] = Var(place=str(p[1]), type='bool', value=0)
p[0].unique = p[0].place
def p_epsilon(p):
""" epsilon : """
p[0] = Var(quad = len(qr))
def p_error(p):
if p:
print('Syntax Error')
print(p)
else:
print('Syntax Error EOF')
parser = yacc.yacc()