George Sakkis wrote:
The S-expression parser below works, but I wonder if it can be simplified; it's 
not as short and
straightforward as I would expect given the simplicity of S-expressions. Can 
you see a simpler
non-recursive solution ?

How about this (minus your error checking)?

def parseS(expression):
    stack = []
    stacks = []
    for token in re.split(r'([()])|\s+', expression):
        print stack
        if token == '(':
            stacks.append(stack)
            stack = []
        elif token == ')':
            stacks[-1].append(stack)
            stack = stacks.pop()
        elif token:
            stack.append(token)
    return stack[0]

Michael

--
http://mail.python.org/mailman/listinfo/python-list

Reply via email to