Skip to content

Instantly share code, notes, and snippets.

@airekans
Created July 20, 2012 05:35
Show Gist options
  • Save airekans/3148878 to your computer and use it in GitHub Desktop.
Save airekans/3148878 to your computer and use it in GitHub Desktop.
AST to symbol representation tranformation
#! /usr/bin/env python
import token
import symbol
def ast2symbol(ast):
if len(ast) < 2:
if type(ast) is type(""):
return ast
else:
raise "Error"
def num2sym(n):
if token.ISTERMINAL(n):
return token.tok_name[n]
else:
return symbol.sym_name[n]
num = ast[0]
rest = ast[1:]
return [num2sym(num)] + [ast2symbol(e) for e in rest]
if __name__ == '__main__':
import parser
a = parser.expr("1 + 2")
t = a.totuple()
print ast2symbol(t)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment