-
Notifications
You must be signed in to change notification settings - Fork 0
/
parser.py
717 lines (679 loc) · 13.8 KB
/
parser.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
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
# Parser for defining all grammar rules
import ply.yacc as yacc
import lexer
from lexer import tokens, lexer
from nerve_points import *
# Start
def p_start(p):
'''
start : punt_start_litaf LITAF START DOUBLE_DOT classes global_vars functions main puntSetMemory END
'''
p[0] = ""
for x in range(1, len(p)):
p[0] += str(p[x])
p[0]
# Classes
def p_classes(p):
'''
classes : class classes
| empty
'''
p[0] = ""
for x in range(1, len(p)):
p[0] += str(p[x])
p[0]
# Global Vars
def p_global_vars(p):
'''
global_vars : createGlobal global_vars_A
'''
p[0] = p[1] + p[2]
def p_global_vars_A(p):
'''
global_vars_A : declarations global_vars_A
| empty
'''
p[0] = ""
for x in range(1, len(p)):
p[0] += str(p[x])
p[0]
# Declarations
def p_declarations(p):
'''
declarations : declaration
| declaration_class
| declaration_list
'''
p[0] = p[1]
# Main
def p_main(p):
'''
main : punt_Go_main MAIN setMain IS INT main_A WITH INT_CONST END
'''
p[0] = ""
for x in range(1, len(p)):
p[0] += str(p[x])
p[0]
def p_main_A(p):
'''
main_A : statement main_A
| empty
'''
p[0] = ""
for x in range(1, len(p)):
p[0] += str(p[x])
p[0]
# Functions
def p_functions(p):
'''
functions : function functions
| empty
'''
p[0] = ""
for x in range(1, len(p)):
p[0] += str(p[x])
p[0]
# Declaration
def p_declaration(p):
'''
declaration : function_B declaration_A
'''
global actualScope
p[0] = p[1] + p[2]
def p_declaration_A1(p):
'''
declaration_A1 : COMMA declaration_A
| empty
'''
p[0] = ""
for x in range(1, len(p)):
p[0] += str(p[x])
p[0]
# Assign
def p_assign_A(p):
'''
assign_A : hyper_exp
| CLASS_ID DOT NEW OPEN_PARENTHESIS function_call_A CLOSE_PARENTHESIS
| list
'''
p[0] = p[1]
# Function
def p_function(p):
'''
function : FUN getFunId OPEN_PARENTHESIS params CLOSE_PARENTHESIS IS function_type function_C function_D END
'''
p[0] = ""
for x in range(1, len(p)):
p[0] += str(p[x])
p[0]
def p_function_C(p): # Statements inside function
'''
function_C : statement function_C
| empty
'''
p[0] = ""
for x in range(1, len(p)):
p[0] += str(p[x])
p[0]
# Function Call
def p_function_call(p):
'''
function_call : function_call_name punt_validate_void OPEN_PARENTHESIS function_call_A CLOSE_PARENTHESIS punt_function_call_end
'''
p[0] = ""
for x in range(1, len(p)):
p[0] += str(p[x])
p[0]
def p_function_call_A(p): # Parameters for function call
'''
function_call_A : function_call_hyper_exp function_call_A1
| empty
'''
p[0] = ""
for x in range(1, len(p)):
p[0] += str(p[x])
p[0]
def p_function_call_A1(p):
'''
function_call_A1 : COMMA function_call_hyper_exp pop_false_bottom_function function_call_A1
| empty
'''
p[0] = ""
for x in range(1, len(p)):
p[0] += str(p[x])
p[0]
# Statement
def p_statement(p):
'''
statement : declarations
| block
'''
p[0] = p[1]
# Block
def p_block(p):
'''
block : assign
| cycle
| condition
| lecture
| writing
| punt_flag_block function_call punt_flag_block
| COMMENT
'''
p[0] = p[1]
# Hyper Exp
def p_hyper_exp(p):
'''
hyper_exp : negation mega_exp punt_negation
'''
p[0] = ""
for x in range(1, len(p)):
p[0] += str(p[x])
p[0]
# Mega Exp
def p_mega_exp(p):
'''
mega_exp : super_exp puntAndOr mega_exp_A
'''
p[0] = ""
for x in range(1, len(p)):
p[0] += str(p[x])
p[0]
def p_mega_exp_A(p):
'''
mega_exp_A : mega_exp_A1 mega_exp
| empty
'''
p[0] = ""
for x in range(1, len(p)):
p[0] += str(p[x])
p[0]
# Super Exp
def p_super_exp(p):
'''
super_exp : exp puntLogical super_exp_A
'''
p[0] = ""
for x in range(1, len(p)):
p[0] += str(p[x])
p[0]
def p_super_exp_A(p):
'''
super_exp_A : super_exp_A1 super_exp
| empty
'''
p[0] = ""
for x in range(1, len(p)):
p[0] += str(p[x])
p[0]
# Exp
def p_exp(p):
'''
exp : term puntSum exp_A
'''
p[0] = ""
for x in range(1, len(p)):
p[0] += str(p[x])
p[0]
def p_exp_A(p):
'''
exp_A : exp_A1 exp
| empty
'''
p[0] = ""
for x in range(1, len(p)):
p[0] += str(p[x])
p[0]
# Term
def p_term(p):
'''
term : factor puntMul term_A
'''
p[0] = ""
for x in range(1, len(p)):
p[0] += str(p[x])
p[0]
def p_term_A(p):
'''
term_A : term_A1 term
| empty
'''
p[0] = ""
for x in range(1, len(p)):
p[0] += str(p[x])
p[0]
# Factor
# All are Nerve Points
# Cycle call
def p_cycle(p):
'''
cycle : cycle_A END
'''
p[0] = ""
for x in range(1, len(p)):
p[0] += str(p[x])
p[0]
def p_cycle_A(p):
'''
cycle_A : loop
| until
'''
p[0] = p[1]
# Loop Cycle
def p_loop(p):
'''
loop : LOOP FROM puntLoopID loop_to loop_value built_block BY patron
'''
p[0] = ""
for x in range(1, len(p)):
p[0] += str(p[x])
p[0]
def p_loop_to(p):
'''
loop_to : UPTO puntLoopUp
| DOWNTO puntLoopDown
'''
p[0] = p[1] + p[2]
# Until Cycle
def p_until(p):
'''
until : UNTIL puntUntilJump hyper_exp IS bool_values_cycle puntUntil DO built_block puntUntilEnd
'''
p[0] = ""
for x in range(1, len(p)):
p[0] += str(p[x])
p[0]
# Condition
def p_condition(p):
'''
condition : IF condition_exp puntIF built_block condition_A condition_B END puntIfEnd
'''
p[0] = ""
for x in range(1, len(p)):
p[0] += str(p[x])
p[0]
def p_condition_exp(p):
'''
condition_exp : OPEN_PARENTHESIS hyper_exp CLOSE_PARENTHESIS
'''
p[0] = ""
for x in range(1, len(p)):
p[0] += str(p[x])
p[0]
def p_condition_A(p):
'''
condition_A : puntElseIfGOTO ELSIF condition_exp puntElseIfGoToF built_block condition_A puntElseIfEnd
| empty
'''
p[0] = ""
for x in range(1, len(p)):
p[0] += str(p[x])
p[0]
def p_condition_B(p):
'''
condition_B : puntElse ELSE built_block
| empty
'''
p[0] = ""
for x in range(1, len(p)):
p[0] += str(p[x])
p[0]
# Lecture
def p_lecture(p):
'''
lecture : IN OPEN_PARENTHESIS lecture_A CLOSE_PARENTHESIS
'''
p[0] = ""
for x in range(1, len(p)):
p[0] += str(p[x])
p[0]
def p_lecture_A1(p):
'''
lecture_A1 : COMMA lecture_A
| empty
'''
p[0] = ""
for x in range(1, len(p)):
p[0] += str(p[x])
p[0]
# Writing
def p_writing(p):
'''
writing : OUT OPEN_PARENTHESIS writing_A CLOSE_PARENTHESIS puntReverseOuts
'''
p[0] = ""
for x in range(1, len(p)):
p[0] += str(p[x])
p[0]
def p_writing_A1(p):
'''
writing_A1 : COMMA writing_A
| empty
'''
p[0] = ""
for x in range(1, len(p)):
p[0] += str(p[x])
p[0]
#-------------------------------------------------------------------------------------------------------------------------------------------
# Class Related Grammar
#-------------------------------------------------------------------------------------------------------------------------------------------
# Class
def p_class(p):
'''
class : CLASS getClassId heritance IS class_attributes class_methods END
'''
p[0] = ""
for x in range(1, len(p)):
p[0] += str(p[x])
p[0]
def p_heritance(p):
'''
heritance : FROM CLASS_ID
| empty
'''
p[0] = ""
for x in range(1, len(p)):
p[0] += str(p[x])
p[0]
# Attributes
def p_class_attributes(p):
'''
class_attributes : ATTRIBUTES DOUBLE_DOT attributes END
'''
p[0] = ""
for x in range(1, len(p)):
p[0] += str(p[x])
p[0]
def p_attributes(p):
'''
attributes : attributes_A attributes
| empty
'''
p[0] = ""
for x in range(1, len(p)):
p[0] += str(p[x])
p[0]
def p_attributes_A(p):
'''
attributes_A : visibility type ID
'''
insert_var(p[3], p[2])
p[0] = ""
for x in range(1, len(p)):
p[0] += str(p[x])
p[0]
# Methods
def p_class_methods(p):
'''
class_methods : METHODS DOUBLE_DOT methods END
'''
p[0] = ""
for x in range(1, len(p)):
p[0] += str(p[x])
p[0]
def p_methods(p):
'''
methods : constructor methods_A
'''
p[0] = ""
for x in range(1, len(p)):
p[0] += str(p[x])
p[0]
def p_methods_A(p):
'''
methods_A : visibility function methods_A
| empty
'''
p[0] = ""
for x in range(1, len(p)):
p[0] += str(p[x])
p[0]
# Constructor
def p_constructor(p):
'''
constructor : visibility CLASS_ID OPEN_PARENTHESIS params CLOSE_PARENTHESIS IS CLASS_ID constructor_A END
'''
p[0] = ""
for x in range(1, len(p)):
p[0] += str(p[x])
p[0]
def p_params(p):
'''
params : function_A
| empty
'''
p[0] = p[1]
def p_constructor_A(p):
'''
constructor_A : statement constructor_A
| empty
'''
p[0] = ""
for x in range(1, len(p)):
p[0] += str(p[x])
p[0]
def p_visibility(p):
'''
visibility : PUBLIC
| PRIVATE
'''
p[0] = p[1]
# Class Declaration
def p_declaration_class(p):
'''
declaration_class : CLASS_ID ID declaration_class_A
'''
p[0] = ""
for x in range(1, len(p)):
p[0] += str(p[x])
p[0]
def p_declaration_class_A(p):
'''
declaration_class_A : COMMA ID declaration_class_A
| empty
'''
for x in range(1, len(p)):
p[0] += str(p[x])
p[0]
# Class Values
def p_class_values(p):
'''
class_values : call_attribute
| call_method
'''
p[0] = p[1]
# Call Attribute
def p_call_attribute(p):
'''
call_attribute : ID
'''
p[0] = ""
for x in range(1, len(p)):
p[0] += str(p[x])
p[0]
# Call Method
def p_call_method(p):
'''
call_method : FUNCTION_ID OPEN_PARENTHESIS function_call_A CLOSE_PARENTHESIS
'''
p[0] = ""
for x in range(1, len(p)):
p[0] += str(p[x])
p[0]
#-------------------------------------------------------------------------------------------------------------------------------------------
# Lists (Vectors) Related Grammars
#-------------------------------------------------------------------------------------------------------------------------------------------
# List
def p_list(p):
'''
list : OPEN_BRACKET function_call_A CLOSE_BRACKET
'''
p[0] = ""
for x in range(1, len(p)):
p[0] += str(p[x])
p[0]
# List Declaration
def p_declaration_list(p):
'''
declaration_list : LIS ID declaration_list_A1
'''
p[0] = ""
for x in range(1, len(p)):
p[0] += str(p[x])
p[0]
def p_declaration_list_A1(p):
'''
declaration_list_A1 : COMMA ID declaration_list_A1
| empty
'''
p[0] = ""
for x in range(1, len(p)):
p[0] += str(p[x])
p[0]
# List Value (Single)
def p_list_val(p):
'''
list_val : OPEN_BRACKET INT_CONST CLOSE_BRACKET
'''
p[0] = ""
for x in range(1, len(p)):
p[0] += str(p[x])
p[0]
# List Methods
def p_list_methods(p):
'''
list_methods : list_push
| list_size
| list_concat
| list_empty
| list_reverse
| list_pop
'''
p[0] = p[1]
# List Pop
def p_list_pop(p):
'''
list_pop : POP OPEN_PARENTHESIS CLOSE_PARENTHESIS
'''
p[0] = ""
for x in range(1, len(p)):
p[0] += str(p[x])
p[0]
# List Push
def p_list_push(p):
'''
list_push : PUSH OPEN_PARENTHESIS hyper_exp CLOSE_PARENTHESIS
'''
p[0] = ""
for x in range(1, len(p)):
p[0] += str(p[x])
p[0]
# List Size
def p_list_size(p):
'''
list_size : SIZE
'''
p[0] = ""
for x in range(1, len(p)):
p[0] += str(p[x])
p[0]
# List Concat
def p_list_concat(p):
'''
list_concat : JOIN OPEN_PARENTHESIS list_concat_A CLOSE_PARENTHESIS
'''
p[0] = ""
for x in range(1, len(p)):
p[0] += str(p[x])
p[0]
def p_list_concat_A(p):
'''
list_concat_A : ID
| list
'''
p[0] = p[1]
# List Empty
def p_list_empty(p):
'''
list_empty : EMPTY OPEN_PARENTHESIS CLOSE_PARENTHESIS
'''
p[0] = ""
for x in range(1, len(p)):
p[0] += str(p[x])
p[0]
# List Reverse
def p_list_reverse(p):
'''
list_reverse : FLIP OPEN_PARENTHESIS CLOSE_PARENTHESIS
'''
p[0] = ""
for x in range(1, len(p)):
p[0] += str(p[x])
p[0]
#-------------------------------------------------------------------------------------------------------------------------------------------
# General Real Value Grammars (These are used or called by many different grammar rules)
#-------------------------------------------------------------------------------------------------------------------------------------------
# Block for Condition and Cycles
def p_built_block(p):
'''
built_block : block built_block
| empty
'''
p[0] = ""
for x in range(1, len(p)):
p[0] += str(p[x])
p[0]
# Type Val
def p_type(p):
'''
type : INT
| FLO
| BOO
| CHA
| STR
'''
p[0] = p[1]
# Value
def p_value(p):
'''
value : ID structure_values
| constants
| function_call
structure_values : DOT structure_values_A
| list_val
| empty
structure_values_A : list_methods
| class_values
'''
p[0] = p[1]
# Constants
def p_constants(p):
'''
constants : int_const
| char_const
| float_const
| string_const
| bool_values
'''
p[0] = p[1]
# Empty
def p_empty(p):
'''
empty :
'''
p[0] = ""
# Simple Error
def p_error(p):
print("Syntax error at {}".format(p.lexer.lineno))
sys.exit(0)
# Build the parser
parser = yacc.yacc()
# Read file as an input and evaluate if the grammar is acceptable or not. Print a message if it finds an error
# in the grammar.
name = sys.argv[1]
if name.endswith('.lit'):
with open(name, 'r') as myfile:
line = myfile.read()
result = parser.parse(line)
else:
print('Cannot read non .lit files')