-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparser.py
More file actions
731 lines (666 loc) · 29.6 KB
/
Copy pathparser.py
File metadata and controls
731 lines (666 loc) · 29.6 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
# -----------------------------------------------------------------------------
# narrtr: parser.py
# This file defines the Parser (Syntactic Analyzer) for the language narratr
#
# Copyright (C) 2015 Team narratr
# All Rights Reserved
# Team narratr: Yelin Hong, Shloka Kini, Nivvedan Senthamil Selvan, Jonah
# Smith, Cecilia Watt
#
# File Created: 21 March 2015
# Primary Authors: Nivvedan Senthamil Selvan <nivvedan.s@columbia.edu>,
# Jonah Smith, Shloka Kini
#
# Any questions, bug reports and complaints are to be directed at the primary
# author.
#
# -----------------------------------------------------------------------------
from sys import stderr, exit
import ply.yacc as yacc
from ply.lex import LexToken
from lexer import LexerForNarratr
from node import Node
from symtab import SymTabEntry, SymTab
# Error checking: Make sure when item added to list, the item is of the same
# type as the rest of the list.
class ParserForNarratr:
def __init__(self, **kwargs):
self.lexer = LexerForNarratr()
self.tokens = self.lexer.tokens
self.parser = yacc.yacc(module=self, **kwargs)
self.symtab = SymTab()
def p_program(self, p):
"program : newlines_optional blocks"
p[0] = Node(None, "program", [p[2]])
# The starttart state may be given multiple times.
# This is handled in the code
# generator.
def p_blocks(self, p):
'''blocks : scene_block newlines_optional
| item_block newlines_optional
| start_state newlines_optional
| blocks scene_block newlines_optional
| blocks item_block newlines_optional
| blocks start_state newlines_optional'''
# This statement differentiate parsing for a scene block
# as it is added to the list of blocks.
if p[1].type == "blocks" and p[2].type == "scene_block":
p[1].children[0][p[2].value] = p[2]
p[0] = p[1]
# This statement
# differentiates parsing for an item block
# as it is added to the list of blocks.
elif p[1].type == "blocks" and p[2].type == "item_block":
p[1].children[1][p[2].value] = p[2]
p[0] = p[1]
# For a startstate that is declared after blocks have been
# created,
# the start state is one of the children
# of the main program.
elif p[1].type == "blocks" and p[2].type == "start_state":
p[1].children.append(p[2])
p[0] = p[1]
# This parses the first scene block, as the first block
# in the program.
# It starts a Node containing all blocks.
elif p[1].type == "scene_block":
if(not isinstance(p[0], Node)):
p[0] = Node(None, "blocks", [{}, {}])
p[0].children[0][p[1].value] = p[1]
# This parses the first item block, as the first block
# in the program.
# It starts a Node containing all blocks.
elif p[1].type == "item_block":
if(not isinstance(p[0], Node)):
p[0] = Node(None, "blocks", [{}, {}])
p[0].children[1][p[1].value] = p[1]
# This parses the start state, as the first block
# in the program.
# It starts a Node containing all blocks.
elif p[1].type == "start_state":
if(not isinstance(p[0], Node)):
p[0] = Node(None, "blocks", [{}, {}])
p[0].children.append(p[2])
# Because newlines are ignored, these functions
# skip over the newlines in the AST.
def p_newlines_optional(self, p):
'''newlines_optional : newlines
| '''
# This appends a string of newlines, and
# skips over these characters in the AST.
def p_newlines(self, p):
'''newlines : newlines NEWLINE
| NEWLINE'''
# A scene block consists of a setup, action, and cleanup block
# all as children. Depending on the format, it creates the
# block node with the parts as children
# and inserts the resulting scene in to the symbol table.
def p_scene_block(self, p):
'''scene_block : SCENE SCENEID LCURLY newlines INDENT setup_block \
action_block cleanup_block DEDENT newlines_optional \
RCURLY
| SCENE SCENEID LCURLY newlines setup_block \
action_block cleanup_block RCURLY'''
if isinstance(p[6], Node) and p[6].type == 'setup_block':
children = [p[6], p[7], p[8]]
elif isinstance(p[5], Node) and p[5].type == 'setup_block':
children = [p[5], p[6], p[7]]
p[0] = Node(p[2], "scene_block", children, lineno=p.lineno(2))
try:
self.symtab.insert(p[2], p[0], "scene", "GLOBAL", False)
except:
self._semantic_error("Error at line " + str(p.lineno(1)) +
": A scene with the id '" + str(p[2]) +
"' already exists.")
self.pass_down(p[0], p[2])
# This item block consists of a suite and parameters
# both children of the item block.
# Depending on whether it is an empty block
# or not, it inserts the resulting item
# into the symbol table.
def p_item_block(self, p):
'''item_block : ITEM ID itemparams LCURLY newlines_optional RCURLY
| ITEM ID itemparams LCURLY suite RCURLY'''
if isinstance(p[5], Node) and p[5].type == "suite":
children = [p[3], p[5]]
else:
children = [p[3]]
p[0] = Node(p[2], "item_block", children, lineno=p.lineno(2))
try:
self.symtab.insert(p[2], p[0], "item", "GLOBAL", False)
except:
self._semantic_error("Error at line " + str(p.lineno(1)) +
": An item with the id '" + str(p[2]) +
"' already exists.")
self.pass_down(p[0], "item." + p[2])
def p_start_state(self, p):
'start_state : START COLON SCENEID'
p[0] = Node(p[3], "start_state", lineno=p.lineno(3))
def p_setup_block(self, p):
'''setup_block : SETUP COLON suite
| SETUP COLON newlines'''
if isinstance(p[3], Node) and p[3].type == "suite":
p[0] = Node(None, "setup_block", [p[3]], lineno=p.lineno(1))
else:
p[0] = Node(None, "setup_block", lineno=p.lineno(1))
def p_action_block(self, p):
'''action_block : ACTION COLON suite
| ACTION COLON newlines'''
if isinstance(p[3], Node) and p[3].type == "suite":
p[0] = Node(None, "action_block", [p[3]], lineno=p.lineno(1))
else:
p[0] = Node(None, "action_block", lineno=p.lineno(1))
def p_cleanup_block(self, p):
'''cleanup_block : CLEANUP COLON suite
| CLEANUP COLON newlines'''
if isinstance(p[3], Node) and p[3].type == "suite":
p[0] = Node(None, "cleanup_block", [p[3]], lineno=p.lineno(1))
else:
p[0] = Node(None, "cleanup_block", lineno=p.lineno(1))
# A suite can either be a single statement or a block of statements.
# Accordingly, either the block or statement is added as a child to the
# suite.
def p_suite(self, p):
'''suite : simple_statement
| newlines INDENT statements DEDENT newlines_optional'''
if isinstance(p[1], Node) and p[1].type == "simple_statement":
p[0] = Node("simple", "suite", [p[1]], lineno=p[1].lineno)
else:
p[0] = Node("statements", "suite", [p[3]], lineno=p.lineno(2))
# A list of statements is handled here.
# A single statement is added as the child node of a statement
# list.
def p_statements(self, p):
'''statements : statements statement
| statement'''
if p[1].type == "statements":
p[1].children.append(p[2])
p[0] = p[1]
else:
p[0] = Node(None, "statements", [p[1]], lineno=p[1].lineno)
def p_statement(self, p):
'''statement : simple_statement
| block_statement'''
if p[1].type == 'simple_statement':
value = 'simple'
else:
value = 'block'
p[0] = Node(value, 'statement', [p[1]], lineno=p[1].lineno)
# A simple statement can be of many set forms.
# Here we treat all these forms the same and encapsulate
# then into simple statements.
def p_simple_statement(self, p):
'''simple_statement : say_statement newlines
| exposition_statement newlines
| win_statement newlines
| lose_statement newlines
| flow_statement newlines
| expression_statement newlines'''
if isinstance(p[1], Node):
if p[1].type == "say_statement":
value = "say"
elif p[1].type == "exposition":
value = "exposition"
elif p[1].type == "win_statement":
value = "win"
elif p[1].type == "expression_statement":
value = "expression"
elif p[1].type == "flow_statement":
value = "flow"
elif p[1].type == "lose_statement":
value = "lose"
else:
self._semantic_error("Syntax Error forming simple_statement.")
p[0] = Node(value, 'simple_statement', [p[1]], lineno=p[1].lineno)
def p_say_statement(self, p):
'''say_statement : SAY testlist'''
p[0] = Node(None, "say_statement", [p[2]], lineno=p[2].lineno)
def p_exposition_statement(self, p):
'''exposition_statement : EXPOSITION testlist'''
if p[1] == "exposition":
p[0] = Node(None, "exposition", [p[2]], lineno=p.lineno(1))
def p_win_statement(self, p):
'''win_statement : WIN
| WIN testlist'''
if p[1] == "win":
if len(p) == 2:
children = []
if len(p) == 3:
children = [p[2]]
p[0] = Node("win", "win_statement", children)
def p_lose_statement(self, p):
'''lose_statement : LOSE
| LOSE testlist'''
if p[1] == "lose":
if len(p) == 2:
children = []
if len(p) == 3:
children = [p[2]]
p[0] = Node("lose", "lose_statement", children)
# Flow statements are statements that break the flow of the
# scene.
# These include while loop halting expressions,
# like break and continue,
# or statements that allow movement from scene to scene.
def p_flow_statement(self, p):
'''flow_statement : break_statement
| continue_statement
| moves_declaration
| moveto_statement'''
if isinstance(p[1], Node):
if p[1].type == 'break_statement':
value = "break"
elif p[1].type == 'continue_statement':
value = "continue"
elif p[1].type == 'moves_declaration':
value = "moves"
elif p[1].type == 'moveto_statement':
value = "moveto"
else:
self._semantic_error("Parse error in flow_statement.")
p[0] = Node(value, "flow_statement", [p[1]], lineno=p[1].lineno)
# Here we handle variable declarations,
# god variables, and regular variables.
def p_expression_statement(self, p):
'''expression_statement : ID IS testlist
| GOD ID IS testlist
| testlist'''
if isinstance(p[1], Node):
p[0] = Node("testlist", "expression_statement", [p[1]],
lineno=p[1].lineno)
elif p[1] == "god":
p[0] = Node("godis", "expression_statement", [Node(p[2], "god_id"),
p[4]], lineno=p.lineno(1))
else:
p[0] = Node("is", "expression_statement", [Node(p[1], "id"), p[3]],
lineno=p.lineno(1))
def p_break_statement(self, p):
'''break_statement : BREAK'''
p[0] = Node(p[1], 'break_statement', [], lineno=p.lineno(1))
def p_continue_statement(self, p):
'''continue_statement : CONTINUE'''
p[0] = Node(p[1], 'continue_statement', [], lineno=p.lineno(1))
def p_moves_declaration(self, p):
'''moves_declaration : MOVES directionlist'''
p[0] = Node("moves", 'moves_declaration', [p[2]], lineno=p.lineno(1))
# Here we create a list of the possible directions that
# are declared to lead you out of a scene.
# A list is created as a Node
# and subsequent directions are added as children
# to that root directionlist Node.
def p_directionlist(self, p):
'''directionlist : direction LPARAN SCENEID RPARAN
| directionlist COMMA direction LPARAN SCENEID \
RPARAN'''
if p[1].type == 'direction':
p[1].children.append(Node(p[3], 'sceneid', [], lineno=p.lineno(3)))
p[0] = Node(None, 'directionlist', [p[1]], lineno=p[1].lineno)
else:
p[3].children.append(Node(p[5], 'sceneid', [], lineno=p.lineno(5)))
p[1].children.append(p[3])
p[0] = p[1]
p[0].type = 'directionlist'
def p_direction(self, p):
'''direction : LEFT
| RIGHT
| UP
| DOWN'''
p[0] = Node(p[1], 'direction', [], lineno=p.lineno(1))
def p_moveto_statement(self, p):
'''moveto_statement : MOVETO SCENEID'''
p[0] = Node('moveto', 'moveto_statement', [Node(p[2], "sceneid")],
lineno=p.lineno(1))
# A testlist is a generic expression for some inequality
# conditional expression.
# Each test is added to the original testlist
# Node.
def p_testlist(self, p):
'''testlist : testlist COMMA test
| test'''
if p[1].type == 'test':
p[0] = Node(None, 'testlist', [p[1]], p[1].v_type,
lineno=p[1].lineno)
p[0].type = 'testlist'
else:
p[0] = p[1]
p[0].children.append(p[3])
p[0].type = 'testlist'
def p_test(self, p):
'''test : or_test'''
p[0] = Node(None, 'test', [p[1]], lineno=p[1].lineno)
def p_or_test(self, p):
'''or_test : or_test OR and_test
| and_test'''
if p[1].type == 'and_test':
p[0] = Node(None, 'or_test', [p[1]], lineno=p[1].lineno)
else:
children = [p[1], p[3]]
p[0] = Node('or', 'or_test', children, lineno=p[1].lineno)
def p_and_test(self, p):
'''and_test : and_test AND not_test
| not_test'''
if p[1].type == 'not_test':
p[0] = Node(None, 'and_test', [p[1]], lineno=p[1].lineno)
else:
children = [p[1], p[3]]
p[0] = Node('and', 'and_test', children, lineno=p[1].lineno)
def p_not_test(self, p):
'''not_test : NOT not_test
| comparison'''
if isinstance(p[1], Node) and p[1].type == 'comparison':
p[0] = Node(None, 'not_test', [p[1]], lineno=p[1].lineno)
else:
p[0] = Node('not', 'not_test', [p[2]], lineno=p[2].lineno)
# This concatenates expressions and comparison nodes
# to a comparison statement.
# E.g x > 3
def p_comparison(self, p):
'''comparison : comparison comparison_op expression
| expression'''
if p[1].type == 'comparison':
p[0] = Node('comparison', 'comparison', [p[1], p[2], p[3]],
p[1].v_type, lineno=p[1].lineno)
else:
p[0] = Node(None, 'comparison', [p[1]], p[1].v_type,
lineno=p[1].lineno)
p[0].type = 'comparison'
def p_expression(self, p):
'''expression : arithmetic_expression'''
p[0] = Node(p[1].value, "expression", [p[1]], lineno=p[1].lineno)
def p_comparison_op(self, p):
'''comparison_op : LESS
| GREATER
| LESSEQUALS
| GREATEREQUALS
| EQUALS
| NOTEQUALS
| NOT EQUALS'''
if p[1] == '=':
p[1] = '=='
p[0] = Node(p[1], 'comparison_op', [], lineno=p.lineno(1))
# In the first two productions for this rule, we need to ensure that
# the two sides are combinable. We overload to PLUS operator to string
# concatenation (which only allows two strings), and we allow floats
# and integers to be combined freely.
def p_arithmetic_expression(self, p):
'''arithmetic_expression : arithmetic_expression PLUS term
| arithmetic_expression MINUS term
| term'''
if p[1].type == 'term':
p[0] = Node("term", "arithmetic_expression", [p[1]], p[1].v_type,
lineno=p[1].lineno)
else:
# Extra condition for '+': allow string concatenation.
if p[1].v_type == "string":
if p[2] == "+":
if p[3].v_type in ["string", "id"]:
p[0] = Node(p[2], 'arithmetic_expression',
[p[1], p[3]], "string", p.lineno(2))
else:
self._semantic_error(p, err_type="combination_error")
# Reject any expression trying to subtract strings.
elif p[2] == "-":
self._semantic_error(p, err_type="combination_error")
else:
p[0] = self.combination_rules(p, 'arithmetic_expression')
# This specifies a term, used for arithmetic operations,
# as specified below.
def p_term(self, p):
'''term : term TIMES factor
| term DIVIDE factor
| term INTEGERDIVIDE factor
| factor '''
if p[1].type == "term":
# Type checking: reject anything with strings
if (p[1].v_type in ["string", "list"] or
p[3].v_type in ["string", "list"]):
self._semantic_error(p, err_type="combination_error")
p[0] = self.combination_rules(p, 'term')
# For integer division, we can just reset the v_type
if p[2] == "//":
p[0].v_type = "integer"
p[0].lineno = p.lineno(1)
else:
p[0] = Node("factor", 'term', [p[1]], p[1].v_type,
lineno=p[1].lineno)
def p_factor(self, p):
'''factor : PLUS factor
| MINUS factor
| power'''
if p[1].type == 'power':
p[0] = Node("power", "factor", [p[1]], p[1].v_type,
lineno=p[1].lineno)
else:
p[0] = Node(p[1], 'factor', [p[2]], p[2].v_type,
lineno=p.lineno(1))
def p_power(self, p):
'''power : power trailer
| atom'''
if p[1].type == 'power':
p[1].value = "trailer"
p[1].children.append(p[2])
p[0] = p[1]
else:
p[0] = Node("atom", 'power', [p[1]], p[1].v_type,
lineno=p[1].lineno)
def p_atom_node(self, p):
'''atom : LPARAN test RPARAN
| list
| number
| boolean'''
if isinstance(p[1], Node):
p[0] = Node(p[1].type, "atom", [p[1]], p[1].v_type,
lineno=p[1].lineno)
else:
p[0] = Node("test", "atom", [p[2]], p[2].v_type,
lineno=p.lineno(1))
def p_atom_string(self, p):
'''atom : STRING'''
p[0] = Node(p[1], 'atom', [], "string", lineno=p.lineno(1))
def p_atom_id(self, p):
'''atom : ID'''
p[0] = Node(p[1], 'atom', [], "id", lineno=p.lineno(1))
# This expression calls a function
# in one of two syntactic ways.
# Each is added as child to new trailer node.
def p_trailer(self, p):
'''trailer : calllist
| DOT ID'''
if isinstance(p[1], Node) and p[1].type == "calllist":
p[0] = Node("calllist", "trailer", [p[1]], lineno=p[1].lineno)
else:
p[0] = Node("dot", 'trailer', [p[2]], p.lineno(1))
def p_list(self, p):
'''list : LSQUARE RSQUARE
| LSQUARE testlist RSQUARE'''
if isinstance(p[2], Node) and p[2].type == 'testlist':
p[0] = Node(None, "list", [p[2]], "list", p.lineno(1))
else:
p[0] = Node(None, "list", [], "list", p.lineno(1))
def p_number_int(self, p):
'''number : INTEGER'''
p[0] = Node(p[1], 'number', [], "integer", lineno=p.lineno(1))
def p_number_float(self, p):
'''number : FLOAT'''
p[0] = Node(p[1], 'number', [], "float", lineno=p.lineno(1))
def p_boolean(self, p):
'''boolean : TRUE
| FALSE'''
p[0] = Node(p[1], 'boolean', [], "boolean", lineno=p.lineno(1))
def p_calllist(self, p):
'''calllist : LPARAN args RPARAN
| LPARAN RPARAN'''
if isinstance(p[2], Node):
p[0] = Node("args", "calllist", [p[2]], lineno=p.lineno(1))
else:
p[0] = Node(None, 'calllist', [], lineno=p.lineno(1))
def p_args(self, p):
'''args : args COMMA expression
| expression'''
if p[1].type == 'args':
p[0] = p[1]
p[0].value = "args"
p[0].children.append(p[3])
else:
p[0] = Node("expression", 'args', [p[1]], lineno=p[1].lineno)
# This parses parameters for an item block definition.
def p_itemparams(self, p):
'''itemparams : LPARAN RPARAN
| LPARAN fparams RPARAN'''
if isinstance(p[2], Node):
p[0] = Node('fparams', 'itemparams', [p[2]], lineno=p[2].lineno)
else:
p[0] = Node(None, 'itemparams', lineno=p.lineno(1))
# This parses parameters for a function.
def p_fparams(self, p):
'''fparams : fparams COMMA ID
| ID'''
if isinstance(p[1], Node):
p[1].value = "fparams"
p[1].children.append(Node(p[3], "id"))
p[0] = p[1]
else:
p[0] = Node("id", "fparams", [Node(p[1], "id")],
lineno=p.lineno(1))
# Blocks statements are conditional
# operations of the type if or while.
# Here they are parsed.
def p_block_statement(self, p):
'''block_statement : if_statement
| while_statement'''
if isinstance(p[1], Node):
if p[1].type == 'if_statement':
p[0] = Node('if', 'block_statement', [p[1]],
lineno=p[1].lineno)
elif p[1].type == 'while_statement':
p[0] = Node('while', 'block_statement', [p[1]],
lineno=p[1].lineno)
# This parses an if statement expression into a
# new node with children,
# skipping over key words and colons.
def p_if_statement(self, p):
'''if_statement : IF test COLON suite elif_statements ELSE COLON suite
| IF test COLON suite ELSE COLON suite
| IF test COLON suite elif_statements
| IF test COLON suite'''
if len(p) == 9:
p[0] = Node(None, 'if_statement', [p[2], p[4], p[5], p[8]],
lineno=p[2].lineno)
elif len(p) == 8:
p[0] = Node(None, 'if_statement', [p[2], p[4], None, p[7]],
lineno=p[2].lineno)
elif len(p) == 6:
p[0] = Node(None, 'if_statement', [p[2], p[4], p[5], None],
lineno=p[2].lineno)
else:
p[0] = Node(None, 'if_statement', [p[2], p[4], None, None],
lineno=p[2].lineno)
# This parses an elif statement expression into a new node with children,
# skipping over key words and colons.
def p_elif_statements(self, p):
'''elif_statements : elif_statements ELIF test COLON suite
| ELIF test COLON suite'''
if isinstance(p[1], Node) and p[1].type == 'elif_statements':
p[0] = p[1]
new_elif = Node(None, 'elif_statement', [p[3], p[5]])
p[0].children.append(new_elif)
else:
elif_statement = Node(None, 'elif_statement', [p[2], p[4]],
lineno=p.lineno(1))
p[0] = Node(None, 'elif_statements', [elif_statement],
lineno=elif_statement.lineno)
def p_while_statement(self, p):
'''while_statement : WHILE test COLON suite'''
p[0] = Node(p[2], 'while_statement', [p[2], p[4]], lineno=p[2].lineno)
p[0].type = 'while_statement'
# In order to create SymTab entries (in particular, in order to know
# the appropriate scope) for named entities discovered below a main
# branch (i.e. variables in a scene), we need to travel back down
# the branches once we get to the main node. This function does so,
# creating symtab entries as it goes. It is called recursively on
# every branch, looking for named entities. It takes as its argument
# a branch and the scope to be assigned to all found named entities.
def pass_down(self, branch, scope):
for i, child in enumerate(branch.children):
if not isinstance(child, Node):
continue
if child.type == "expression_statement":
if child[0].type == "id":
child[0].key = self.symtab.getKey(child[0].value, scope)
entry = self.symtab.getWithKey(child[0].key)
if not entry:
self.symtab.insert(child[0].value, None, None, scope,
False)
elif child[0].type == "god_id":
child[0].key = self.symtab.getKey(child[0].value, scope)
entry = self.symtab.getWithKey(child[0].key)
if not entry:
self.symtab.insert(child[0].value, None, None, scope,
True)
else:
if entry.god:
self._semantic_error("Re-declaring god " +
"variable in same scope",
lineno=child.lineno)
else:
self._semantic_error("Declaring previously " +
"declared variable as god",
lineno=child.lineno)
elif child.type == "atom" and child.v_type == "id":
entry = self.symtab.get(child.value, scope)
if entry:
child.key = self.symtab.getKey(child.value, scope)
self.pass_down(child, scope)
# This checks numbers for interoperability. If they are of
# differing types, the result is always the more general of
# the two data types (i.e. float). For now, we allow
# anything with id's, but later would intend to type check
# those as well.
def combination_rules(self, p, n_type):
if p[1].v_type == "id" or p[3].v_type == "id":
p[0] = Node(p[2], n_type, [p[1], p[3]], "id", p.lineno(2))
elif p[1].v_type == "integer":
if p[3].v_type == "integer":
p[0] = Node(p[2], n_type, [p[1], p[3]],
"integer", p.lineno(2))
elif p[3].v_type == "float":
p[0] = Node(p[2], n_type, [p[1], p[3]],
"float", p.lineno(2))
else:
self._semantic_error(p, "combination_error")
elif p[1].v_type == "float":
if p[3].v_type in ["integer", "float"]:
p[0] = Node(p[2], n_type, [p[1], p[3]],
"float", p.lineno(2))
else:
self._semantic_error(p, "combination_error")
elif p[1].v_type == "list":
if p[3].v_type == "list":
p[0] = Node(p[2], n_type, [p[1], p[3]],
"list", p.lineno(2))
else:
self._semantic_error(p, "combination_error")
elif p[1].v_type == "boolean":
self.p_error(p, "combination_error")
else:
p[0] = Node(p[2], n_type, [p[1], p[3]], "unknown", p.lineno(2))
return p[0]
# This is a wrapper function for error statements in the parser.
def p_error(self, p):
stderr.write("ERROR: Syntax Error at Line " + str(p.lineno) +
": " + "at token '" + str(p.value) + "'\n")
exit(1)
# This is a wrapper funciton for semantic errors.
def _semantic_error(self, p, err_type=None, lineno=0):
if err_type == "combination_error":
stderr.write("ERROR: Type error at line " + str(p.lineno(2)) +
": cannot combine '" + p[1].v_type +
"' with '" + p[3].v_type + "'\n")
elif isinstance(p, LexToken):
stderr.write("ERROR: Syntax Error at Line " + str(p.lineno) +
": " + "at token '" + str(p.value) + "'\n")
elif isinstance(p, str):
stderr.write("ERROR: Line " + str(lineno) + ": " + p + "\n")
exit(1)
def parse(self, string_to_parse, **kwargs):
return self.parser.parse(string_to_parse, lexer=self.lexer, **kwargs)