Created
October 18, 2019 21:21
-
-
Save tsukanov-as/562e36ba3249a36144f8fbbffec1ac20 to your computer and use it in GitHub Desktop.
Сравнение дерева разбора и AST в питоне
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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