def f(x,a=""): if""==x:return a, h=x[0] t=x[1:] if h=="f": return f(t,h) elif h=="(": b,u=f(t) return f(u,"("+a+" "+b+")") return a,t while 1: print f(raw_input())[0]
Note that non-ascii characters in the above source code will be escaped (such as \x9f).