Skip to content

Instantly share code, notes, and snippets.

@tsukanov-as
Created October 18, 2019 21:21
Show Gist options
  • Save tsukanov-as/562e36ba3249a36144f8fbbffec1ac20 to your computer and use it in GitHub Desktop.
Save tsukanov-as/562e36ba3249a36144f8fbbffec1ac20 to your computer and use it in GitHub Desktop.
Сравнение дерева разбора и AST в питоне
import parser
import symbol
import token
from pprint import pprint
print('parse tree:')
x = parser.expr('1 + 2 * 3')
y = x.tolist()
def humanize(x):
for i, y in enumerate(x):
if type(y) is int:
x[i] = symbol.sym_name.get(y) or token.tok_name.get(y) or y
try:
humanize(y)
except:
pass
humanize(y)
pprint(y)
print('AST:')
import ast
tree = ast.parse('1 + 2 * 3')
pprint(ast.dump(tree))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment