2

pyparser を使用して、S-Expression 言語用の非常に単純なパーサーを作成しようとしています。私は非常に小さな文法を書きました。これが私のコードです:

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  

このコードの例を見ると、すべて問題ないように見えますが、実行すると、この行の構文エラーが発生します

tests = """\
    ^

わかりません。どんな助けにも感謝します

4

1 に答える 1

4

前の行の括弧は閉じません。

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

もっと ) が必要

于 2009-11-24T16:41:06.317 に答える