Python: Invalid Syntax with test data using Pyparser
-
22-09-2019 - |
Question
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
Solution
parentheses on a previous line are not closed.
sexp << ( alphaword | integer | ( LPAREN + ZeroOrMore(sexp) + RPAREN)
Needs more )'s
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow