# Read python code from stdin and write an sexp of its parse tree to stdout

import parser
import symbol
import token
import sys

def tuple2sexp(t):
  if type(t) == tuple:
    print "(",
    production = t[0]
    if token.ISTERMINAL(production):
      print token.tok_name[production],
    else:
      print symbol.sym_name[production],
    for e in t[1:]:
      tuple2sexp(e)
      print " ",
    print ")",
  elif type(t) == str:
    print '"' + str.replace(str.replace(t, '\\', '\\\\'), '"', '\\"') + '"',
  else:
    print t,

# The python parser module is less forgiving than the python interpreter about
#  different kinds of line endings and whitespace at the end of files
#  so we do a bit of normalization
code = ""
for l in sys.stdin:
  if l.strip() != "":
    code = code + l.rstrip() + "\n"

tuple2sexp(parser.ast2tuple(parser.suite(code), True))

