1

他に質問する方法がよくわかりません。ここで一般的なガイダンスが必要だと思います。私はこのようなものを持っています:

expr = buildExpressionParser table term
    <?> "expression"

term = choice [
    (float >>= return . EDouble)
    , try (natural >>= return . EInteger)
    , try (stringLiteral >>= return . EString)
    , try (reserved "true" >> return (EBool True))
    , try (reserved "false" >> return (EBool False))
    , try assign
    , try ifelse
    , try lambda
    , try array
    , try eseq
    , parens expr
    ]
    <?> "simple expression"

ただし、そのパーサーをテストすると、ほとんどの場合問題が発生します...解析しようとしたときのように

 (a,b) -> "b"

それはパーサーによって受け入れられますlambdaが、exprパーサーはそれを嫌います。そして時にはそれは永遠のルールに完全にハングアップすることさえあります。

Write Yourself a Schemeを読みましたが、Schemeの同種のソースのみを解析します。

多分私は一般的に間違った方向に考えています。

編集:ここで内部パーサー:

assign = do
    i <- identifier
    reservedOp "="
    e <- expr
    return $ EAssign i e

ifelse = do
    reserved "if"
    e <- expr
    reserved "then"
    a <- expr
    reserved "else"
    b <- expr
    return $ EIfElse e a b

lambda = do
    ls <- parens $ commaSep identifier
    reservedOp "->"
    e <- expr
    return $ ELambda ls e

array = (squares $ commaSep expr) >>= return . EArray

eseq = do
    a <- expr
    semi <|> (newline >>= (\x -> return [x]))
    b <- expr
    return $ ESequence a b

table = [
        [binary "*" EMult AssocLeft, binary "/" EDiv AssocLeft, binary "%" EMod AssocLeft ],
        [binary "+" EPlus AssocLeft, binary "-" EMinus   AssocLeft ],
        [binary "~" EConcat AssocLeft],
        [prefixF "not" ENot],
        [binaryF "and" EAnd AssocLeft, binaryF "or" EAnd AssocLeft]
    ]

そして、「それを嫌う」とは、整数または浮動小数点を期待していることを意味します。

4

2 に答える 2

6
于 2011-05-03T23:22:09.153 に答える
4

There appears to be left recursion, which will cause the parser to hang if the choice in term ever gets to eseq:

expr -> term -> eseq -> expr

The term (a,b) will not parse as a lambda, or an array, so it will fall into the eseq loop.

I don't see why (a,b) -> "b" doesn't parse as an expr, since the choice in term should hit upon the lambda, which you say works, before reaching the eseq. What is the position reported in the parse error?

于 2011-05-04T16:39:48.647 に答える