Skip to content

Instantly share code, notes, and snippets.

@josephcc
Created November 23, 2013 23:15
Show Gist options
  • Select an option

  • Save josephcc/7621245 to your computer and use it in GitHub Desktop.

Select an option

Save josephcc/7621245 to your computer and use it in GitHub Desktop.
from re import *
from sys import stdin
uncnf = lambda g: g if type(g) == str else tuple(map(uncnf, g[match(r'\[X[1-9][0-9]*\]', g[0])!=None:]))
pyfy = lambda l: eval((sub('([^ (]) +', r'\1,', sub('([^() ]+)', r'"\1"', sub('([()])', r' \1 ', l).strip()))))
def _clean(g):
for gg in g:
if str in (type(gg), type(gg[0])):
yield gg
else:
for ggg in gg:
yield ggg
clean = lambda g: g if type(g) == str else tuple(map(clean, _clean(g)))
for l in stdin:
print str( clean(uncnf(pyfy(l))) ).replace("'", '').replace(',','')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment