views:

114

answers:

1

Using pyparser, I am trying to create a very simple parser for the S-Expression language. I have written a very small grammar. Here is my code:

from pyparsing import *  
alphaword = Word(alphas)  
integer = Word(nums)  
sexp = Forward()  
LPAREN = Suppress("(")  
RPAREN = Suppress(")")  
sexp << ( alphaword | integer | ( LPAREN + ZeroOrMore(sexp) + RPAREN)  
tests = """\  
    red  
    100  
    ( red 100 blue )  
    ( green ( ( 1 2 ) mauve ) plaid () ) """.splitlines()
for t in tests:  
    print t  
    print sexp.parseString(t)  
    print

While looking at examples of this code it seems that everything is fine, however when running i get a syntax error for this line

tests = """\
    ^

I don't understand it. I would be grateful for any help

+4  A: 

parentheses on a previous line are not closed.

sexp << ( alphaword | integer | ( LPAREN + ZeroOrMore(sexp) + RPAREN)

Needs more )'s

SilentGhost