Created
July 20, 2012 05:35
-
-
Save airekans/3148878 to your computer and use it in GitHub Desktop.
AST to symbol representation tranformation
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
#! /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