python - Recommended Compiler Parse Tree Data Structure -
i trying build custom python based parse tree data structure. know python contains modules ast, parser, tokenize, etc. designed make parsing python syntax relatively easy, since of these modules contain little documentation struggling them. guess have 2 questions:
1.) how can use modules ast parse tree? 2.) type of tree data structure recommend use save information can view/edit later?
any appreciated.
at python terminal, type help(ast):
>>> import ast >>> help(ast) on module ast: name ast file /system/library/frameworks/python.framework/versions/2.7/lib/python2.7/ast.py module docs http://docs.python.org/library/ast description ast ~~~ `ast` module helps python applications process trees of python abstract syntax grammar. abstract syntax might change each python release; module helps find out programmatically current grammar looks , allows modifications of it. abstract syntax tree can generated passing `ast.pycf_only_ast` flag `compile()` builtin function or using `parse()` function module. result tree of objects classes inherit `ast.ast`. … this tells call parse() method. let’s try that:
#!/usr/bin/env python2.7 import ast import sys open(sys.argv[0], 'r') my_source: my_ast = ast.parse(my_source.read()) print ast.dump(my_ast) the output bit messy, paste editor, auto-indent it, , detailed ast:
module(body=[import(names=[alias(name='ast', asname=none)]), import(names=[alias(name='sys', asname=none)]), with(context_expr=call(func=name(id='open', ctx=load()), args=[subscript(value=attribute(value=name(id='sys', ctx=load()), attr='argv', ctx=load()), slice=index(value=num(n=0)), ctx=load()), str(s='r')], keywords=[], starargs=none, kwargs=none), optional_vars=name(id='my_source', ctx=store()), body=[assign(targets=[name(id='my_ast', ctx=store())], value=call(func=attribute(value=name(id='ast', ctx=load()), attr='parse', ctx=load()), args=[call(func=attribute(value=name(id='my_source', ctx=load()), attr='read', ctx=load()), args=[], keywords=[], starargs=none, kwargs=none)], keywords=[], starargs=none, kwargs=none))]), print(dest=none, values=[call(func=attribute(value=name(id='ast', ctx=load()), attr='dump', ctx=load()), args=[name(id='my_ast', ctx=load())], keywords=[], starargs=none, kwargs=none)], nl=true)]) to poke @ things code, run above script python -i foo.py my_ast object can poke @ interactively dir():
>>> dir(my_ast) ['__class__', '__delattr__', '__dict__', '__doc__', '__format__', '__getattribute__', '__hash__', '__init__', '__module__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', '_attributes', '_fields', 'body'] >>> my_ast.body [<_ast.import object @ 0x1055b3590>, <_ast.import object @ 0x1055b3610>, <_ast.with object @ 0x1055b3690>] >>> with_stmt = my_ast.body[2] >>> dir(with_stmt) ['__class__', '__delattr__', '__dict__', '__doc__', '__format__', '__getattribute__', '__hash__', '__init__', '__module__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', '_attributes', '_fields', 'body', 'col_offset', 'context_expr', 'lineno', 'optional_vars'] >>> with_stmt.lineno 6 >>> with_stmt.body [<_ast.assign object @ 0x1055b3910>] >>> assign1 = with_stmt.body[0] >>> dir(assign1) ['__class__', '__delattr__', '__dict__', '__doc__', '__format__', '__getattribute__', '__hash__', '__init__', '__module__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', '_attributes', '_fields', 'col_offset', 'lineno', 'targets', 'value'] >>> assign1.value <_ast.call object @ 0x1055b3990>
Comments
Post a Comment