1 # Based on GardenSnake - a parser generator demonstration program
2 # GardenSnake was released into the Public Domain by Andrew Dalke.
4 # Portions of this work are derived from Python's Grammar definition
5 # and may be covered under the Python copyright and license
7 # Andrew Dalke / Dalke Scientific Software, LLC
8 # 30 August 2006 / Cape Town, South Africa
10 # Modifications for inclusion in PLY distribution
11 from pprint
import pprint
12 from ply
import lex
, yacc
15 from soc
.decoder
.power_decoder
import create_pdecode
16 from soc
.decoder
.pseudo
.lexer
import IndentLexer
17 from soc
.decoder
.orderedset
import OrderedSet
19 # I use the Python AST
20 #from compiler import ast
26 def Assign(left
, right
):
28 print("Assign", left
, right
)
29 if isinstance(left
, ast
.Name
):
30 # Single assignment on left
31 # XXX when doing IntClass, which will have an "eq" function,
32 # this is how to access it
33 # eq = ast.Attribute(left, "eq") # get eq fn
34 # return ast.Call(eq, [right], []) # now call left.eq(right)
35 return ast
.Assign([ast
.Name(left
.id, ast
.Store())], right
)
36 elif isinstance(left
, ast
.Tuple
):
37 # List of things - make sure they are Name nodes
39 for child
in left
.getChildren():
40 if not isinstance(child
, ast
.Name
):
41 raise SyntaxError("that assignment not supported")
42 names
.append(child
.name
)
43 ass_list
= [ast
.AssName(name
, 'OP_ASSIGN') for name
in names
]
44 return ast
.Assign([ast
.AssTuple(ass_list
)], right
)
45 elif isinstance(left
, ast
.Subscript
):
46 return ast
.Assign([left
], right
)
47 # XXX HMMM probably not needed...
49 if isinstance(ls
, ast
.Slice
):
50 lower
, upper
, step
= ls
.lower
, ls
.upper
, ls
.step
51 print("slice assign", lower
, upper
, step
)
53 ls
= (lower
, upper
, None)
55 ls
= (lower
, upper
, step
)
57 return ast
.Call(ast
.Name("selectassign"),
58 [left
.value
, ls
, right
], [])
61 raise SyntaxError("Can't do that yet")
64 # I implemented INDENT / DEDENT generation as a post-processing filter
66 # The original lex token stream contains WS and NEWLINE characters.
67 # WS will only occur before any other tokens on a line.
69 # I have three filters. One tags tokens by adding two attributes.
70 # "must_indent" is True if the token must be indented from the
71 # previous code. The other is "at_line_start" which is True for WS
72 # and the first non-WS/non-NEWLINE on a line. It flags the check so
73 # see if the new line has changed indication level.
76 # No using Python's approach because Ply supports precedence
78 # comparison: expr (comp_op expr)*
79 # arith_expr: term (('+'|'-') term)*
80 # term: factor (('*'|'/'|'%'|'//') factor)*
81 # factor: ('+'|'-'|'~') factor | power
82 # comp_op: '<'|'>'|'=='|'>='|'<='|'<>'|'!='|'in'|'not' 'in'|'is'|'is' 'not'
84 def make_le_compare(arg
):
86 return ast
.Compare(left
, [ast
.LtE()], [right
])
89 def make_ge_compare(arg
):
91 return ast
.Compare(left
, [ast
.GtE()], [right
])
94 def make_lt_compare(arg
):
96 return ast
.Compare(left
, [ast
.Lt()], [right
])
99 def make_gt_compare(arg
):
101 return ast
.Compare(left
, [ast
.Gt()], [right
])
104 def make_eq_compare(arg
):
106 return ast
.Compare(left
, [ast
.Eq()], [right
])
117 "<=": make_le_compare
,
118 ">=": make_ge_compare
,
119 "<": make_lt_compare
,
120 ">": make_gt_compare
,
121 "=": make_eq_compare
,
130 def check_concat(node
): # checks if the comparison is already a concat
131 print("check concat", node
)
132 if not isinstance(node
, ast
.Call
):
134 print("func", node
.func
.id)
135 if node
.func
.id != 'concat':
137 if node
.keywords
: # a repeated list-constant, don't optimise
142 # identify SelectableInt pattern
143 def identify_sint_mul_pattern(p
):
144 if not isinstance(p
[3], ast
.Constant
):
146 if not isinstance(p
[1], ast
.List
):
152 return isinstance(elt
, ast
.Constant
)
155 ########## Parser (tokens -> AST) ######
160 # https://www.mathcs.emory.edu/~valerie/courses/fall10/155/resources/op_precedence.html
161 # python operator precedence
162 # Highest precedence at top, lowest at bottom.
163 # Operators in the same box evaluate left to right.
165 # Operator Description
166 # () Parentheses (grouping)
167 # f(args...) Function call
168 # x[index:index] Slicing
169 # x[index] Subscription
170 # x.attribute Attribute reference
173 # +x, -x Positive, negative
174 # *, /, % mul, div, remainder
175 # +, - Addition, subtraction
176 # <<, >> Bitwise shifts
180 # in, not in, is, is not, <, <=, >, >=, <>, !=, == comp, membership, ident
184 # lambda Lambda expression
189 ("left", "EQ", "GT", "LT", "LE", "GE", "LTU", "GTU"),
192 ("left", "PLUS", "MINUS"),
193 ("left", "MULT", "DIV", "MOD"),
199 for rname
in ['RA', 'RB', 'RC', 'RT', 'RS']:
200 self
.gprs
[rname
] = None
201 self
.read_regs
= OrderedSet()
202 self
.uninit_regs
= OrderedSet()
203 self
.write_regs
= OrderedSet()
205 # The grammar comments come from Python's Grammar/Grammar file
207 # NB: compound_stmt in single_input is followed by extra NEWLINE!
208 # file_input: (NEWLINE | stmt)* ENDMARKER
210 def p_file_input_end(self
, p
):
211 """file_input_end : file_input ENDMARKER"""
215 def p_file_input(self
, p
):
216 """file_input : file_input NEWLINE
220 if isinstance(p
[len(p
)-1], str):
224 p
[0] = [] # p == 2 --> only a blank line
231 # funcdef: [decorators] 'def' NAME parameters ':' suite
232 # ignoring decorators
234 def p_funcdef(self
, p
):
235 "funcdef : DEF NAME parameters COLON suite"
236 p
[0] = ast
.FunctionDef(p
[2], p
[3], p
[5], ())
238 # parameters: '(' [varargslist] ')'
239 def p_parameters(self
, p
):
240 """parameters : LPAR RPAR
241 | LPAR varargslist RPAR"""
246 p
[0] = ast
.arguments(args
=args
, vararg
=None, kwarg
=None, defaults
=[])
248 # varargslist: (fpdef ['=' test] ',')* ('*' NAME [',' '**' NAME] |
252 def p_varargslist(self
, p
):
253 """varargslist : varargslist COMMA NAME
260 # stmt: simple_stmt | compound_stmt
261 def p_stmt_simple(self
, p
):
262 """stmt : simple_stmt"""
263 # simple_stmt is a list
266 def p_stmt_compound(self
, p
):
267 """stmt : compound_stmt"""
270 # simple_stmt: small_stmt (';' small_stmt)* [';'] NEWLINE
271 def p_simple_stmt(self
, p
):
272 """simple_stmt : small_stmts NEWLINE
273 | small_stmts SEMICOLON NEWLINE"""
276 def p_small_stmts(self
, p
):
277 """small_stmts : small_stmts SEMICOLON small_stmt
284 # small_stmt: expr_stmt | print_stmt | del_stmt | pass_stmt | flow_stmt |
285 # import_stmt | global_stmt | exec_stmt | assert_stmt
286 def p_small_stmt(self
, p
):
287 """small_stmt : flow_stmt
290 if isinstance(p
[1], ast
.Call
):
291 p
[0] = ast
.Expr(p
[1])
295 # expr_stmt: testlist (augassign (yield_expr|testlist) |
296 # ('=' (yield_expr|testlist))*)
297 # augassign: ('+=' | '-=' | '*=' | '/=' | '%=' | '&=' | '|=' | '^=' |
298 # '<<=' | '>>=' | '**=' | '//=')
299 def p_expr_stmt(self
, p
):
300 """expr_stmt : testlist ASSIGN testlist
302 print("expr_stmt", p
)
304 # a list of expressions
305 #p[0] = ast.Discard(p[1])
309 if isinstance(p
[1], ast
.Name
):
311 elif isinstance(p
[1], ast
.Subscript
):
313 if name
in self
.gprs
:
314 # add to list of uninitialised
315 self
.uninit_regs
.add(name
)
316 elif isinstance(p
[1], ast
.Call
) and p
[1].func
.id == 'GPR':
317 print(astor
.dump_tree(p
[1]))
318 # replace GPR(x) with GPR[x]
320 p
[1] = ast
.Subscript(p
[1].func
, idx
)
321 elif isinstance(p
[1], ast
.Call
) and p
[1].func
.id == 'MEM':
323 print(astor
.dump_tree(p
[1]))
324 p
[1].func
.id = "memassign" # change function name to set
325 p
[1].args
.append(p
[3])
327 print ("mem rewrite")
328 print(astor
.dump_tree(p
[0]))
332 print(astor
.dump_tree(p
[1]))
333 print("expr assign", name
, p
[1])
334 if name
and name
in self
.gprs
:
335 self
.write_regs
.add(name
) # add to list of regs to write
336 p
[0] = Assign(p
[1], p
[3])
338 def p_flow_stmt(self
, p
):
339 "flow_stmt : return_stmt"
342 # return_stmt: 'return' [testlist]
343 def p_return_stmt(self
, p
):
344 "return_stmt : RETURN testlist"
345 p
[0] = ast
.Return(p
[2])
347 def p_compound_stmt(self
, p
):
348 """compound_stmt : if_stmt
355 def p_break_stmt(self
, p
):
356 """break_stmt : BREAK
360 def p_for_stmt(self
, p
):
361 """for_stmt : FOR test EQ test TO test COLON suite
363 p
[0] = ast
.While(p
[2], p
[4], [])
364 # auto-add-one (sigh) due to python range
366 end
= ast
.BinOp(p
[6], ast
.Add(), ast
.Constant(1))
367 it
= ast
.Call(ast
.Name("range"), [start
, end
], [])
368 p
[0] = ast
.For(p
[2], it
, p
[8], [])
370 def p_while_stmt(self
, p
):
371 """while_stmt : DO WHILE test COLON suite ELSE COLON suite
372 | DO WHILE test COLON suite
375 p
[0] = ast
.While(p
[3], p
[5], [])
377 p
[0] = ast
.While(p
[3], p
[5], p
[8])
379 def p_if_stmt(self
, p
):
380 """if_stmt : IF test COLON suite ELSE COLON if_stmt
381 | IF test COLON suite ELSE COLON suite
382 | IF test COLON suite
384 if len(p
) == 8 and isinstance(p
[7], ast
.If
):
385 p
[0] = ast
.If(p
[2], p
[4], [p
[7]])
387 p
[0] = ast
.If(p
[2], p
[4], [])
389 p
[0] = ast
.If(p
[2], p
[4], p
[7])
391 def p_suite(self
, p
):
392 """suite : simple_stmt
393 | NEWLINE INDENT stmts DEDENT"""
399 def p_stmts(self
, p
):
400 """stmts : stmts stmt
407 def p_comparison(self
, p
):
408 """comparison : comparison PLUS comparison
409 | comparison MINUS comparison
410 | comparison MULT comparison
411 | comparison DIV comparison
412 | comparison MOD comparison
413 | comparison EQ comparison
414 | comparison LE comparison
415 | comparison GE comparison
416 | comparison LTU comparison
417 | comparison GTU comparison
418 | comparison LT comparison
419 | comparison GT comparison
420 | comparison BITOR comparison
421 | comparison BITAND comparison
425 | comparison APPEND comparison
430 p
[0] = ast
.Call(ast
.Name("ltu"), (p
[1], p
[3]), [])
432 p
[0] = ast
.Call(ast
.Name("gtu"), (p
[1], p
[3]), [])
434 l
= check_concat(p
[1]) + check_concat(p
[3])
435 p
[0] = ast
.Call(ast
.Name("concat"), l
, [])
436 elif p
[2] in ['<', '>', '=', '<=', '>=']:
437 p
[0] = binary_ops
[p
[2]]((p
[1], p
[3]))
438 elif identify_sint_mul_pattern(p
):
439 keywords
=[ast
.keyword(arg
='repeat', value
=p
[3])]
441 p
[0] = ast
.Call(ast
.Name("concat"), l
, keywords
)
443 p
[0] = ast
.BinOp(p
[1], binary_ops
[p
[2]], p
[3])
445 if isinstance(p
[2], str) and p
[2] == '-':
446 p
[0] = ast
.UnaryOp(unary_ops
[p
[2]], p
[1])
448 p
[0] = ast
.UnaryOp(unary_ops
[p
[1]], p
[2])
452 # power: atom trailer* ['**' factor]
453 # trailers enables function calls (and subscripts).
454 # I only allow one level of calls
455 # so this is 'trailer'
456 def p_power(self
, p
):
462 if p
[2][0] == "CALL":
463 #p[0] = ast.Expr(ast.Call(p[1], p[2][1], []))
464 p
[0] = ast
.Call(p
[1], p
[2][1], [])
465 # if p[1].id == 'print':
466 # p[0] = ast.Printnl(ast.Tuple(p[2][1]), None, None)
468 # p[0] = ast.CallFunc(p[1], p[2][1], None, None)
470 print("subscript atom", p
[2][1])
471 #raise AssertionError("not implemented %s" % p[2][0])
476 idx
= ast
.Slice(subs
[0], subs
[1], None)
477 p
[0] = ast
.Subscript(p
[1], idx
, ast
.Load())
479 def p_atom_name(self
, p
):
481 p
[0] = ast
.Name(id=p
[1], ctx
=ast
.Load())
483 def p_atom_number(self
, p
):
487 p
[0] = ast
.Constant(p
[1])
489 # '[' [listmaker] ']' |
491 def p_atom_listmaker(self
, p
):
492 """atom : LBRACK listmaker RBRACK"""
495 def p_listmaker(self
, p
):
496 """listmaker : test COMMA listmaker
500 p
[0] = ast
.List([p
[1]])
502 p
[0] = ast
.List([p
[1]] + p
[3].nodes
)
504 def p_atom_tuple(self
, p
):
505 """atom : LPAR testlist RPAR"""
508 print(astor
.dump_tree(p
[2]))
510 if isinstance(p
[2], ast
.Name
):
511 print("tuple name", p
[2].id)
512 if p
[2].id in self
.gprs
:
513 self
.read_regs
.add(p
[2].id) # add to list of regs to read
514 #p[0] = ast.Subscript(ast.Name("GPR"), ast.Str(p[2].id))
517 elif isinstance(p
[2], ast
.BinOp
):
518 if isinstance(p
[2].left
, ast
.Name
) and \
519 isinstance(p
[2].right
, ast
.Constant
) and \
520 p
[2].right
.value
== 0 and \
521 p
[2].left
.id in self
.gprs
:
523 self
.read_regs
.add(rid
) # add to list of regs to read
524 # create special call to GPR.getz
525 gprz
= ast
.Name("GPR")
526 gprz
= ast
.Attribute(gprz
, "getz") # get testzero function
527 # *sigh* see class GPR. we need index itself not reg value
528 ridx
= ast
.Name("_%s" % rid
)
529 p
[0] = ast
.Call(gprz
, [ridx
], [])
530 print("tree", astor
.dump_tree(p
[0]))
536 # trailer: '(' [arglist] ')' | '[' subscriptlist ']' | '.' NAME
537 def p_trailer(self
, p
):
538 """trailer : trailer_arglist
543 def p_trailer_arglist(self
, p
):
544 "trailer_arglist : LPAR arglist RPAR"
545 p
[0] = ("CALL", p
[2])
547 def p_trailer_subscript(self
, p
):
548 "trailer_subscript : LBRACK subscript RBRACK"
549 p
[0] = ("SUBS", p
[2])
551 # subscript: '.' '.' '.' | test | [test] ':' [test]
553 def p_subscript(self
, p
):
554 """subscript : test COLON test
559 if isinstance(p
[3], ast
.Constant
):
560 end
= ast
.Constant(p
[3].value
+1)
562 end
= ast
.BinOp(p
[3], ast
.Add(), ast
.Constant(1))
567 # testlist: test (',' test)* [',']
568 # Contains shift/reduce error
570 def p_testlist(self
, p
):
571 """testlist : testlist_multi COMMA
576 # May need to promote singleton to tuple
577 if isinstance(p
[1], list):
581 # Convert into a tuple?
582 if isinstance(p
[0], list):
583 p
[0] = ast
.Tuple(p
[0])
585 def p_testlist_multi(self
, p
):
586 """testlist_multi : testlist_multi COMMA test
592 if isinstance(p
[1], list):
598 # test: or_test ['if' or_test 'else' test] | lambdef
599 # as I don't support 'and', 'or', and 'not' this works down to 'comparison'
605 # arglist: (argument ',')* (argument [',']| '*' test [',' '**' test]
607 # XXX INCOMPLETE: this doesn't allow the trailing comma
609 def p_arglist(self
, p
):
610 """arglist : arglist COMMA argument
617 # argument: test [gen_for] | test '=' test # Really [keyword '='] test
618 def p_argument(self
, p
):
622 def p_error(self
, p
):
623 # print "Error!", repr(p)
627 class GardenSnakeParser(PowerParser
):
628 def __init__(self
, lexer
=None):
629 PowerParser
.__init
__(self
)
631 lexer
= IndentLexer(debug
=0)
633 self
.tokens
= lexer
.tokens
634 self
.parser
= yacc
.yacc(module
=self
, start
="file_input_end",
635 debug
=False, write_tables
=False)
637 self
.sd
= create_pdecode()
639 def parse(self
, code
):
640 # self.lexer.input(code)
641 result
= self
.parser
.parse(code
, lexer
=self
.lexer
, debug
=False)
642 return ast
.Module(result
)
645 ###### Code generation ######
647 #from compiler import misc, syntax, pycodegen
649 class GardenSnakeCompiler(object):
651 self
.parser
= GardenSnakeParser()
653 def compile(self
, code
, mode
="exec", filename
="<string>"):
654 tree
= self
.parser
.parse(code
)
658 #misc.set_filename(filename, tree)
659 return compile(tree
, mode
="exec", filename
="<string>")
661 gen
= pycodegen
.ModuleCodeGenerator(tree
)