2

我有一个 Parsec 解析器,我主要以应用风格编写。在一种情况下(我唯一一次使用sepBy)我的eol解析器有问题。首先,几个定义:

eol =   try (string "\n\r")
    <|> try (string "\r\n")
    <|> string "\n"
    <|> string "\r"
    <?> "eol"

betaLine = string "BETA " *> (StrandPair <$> p_int <*> p_int <*> p_int <*> (p_int *> p_direction) <*> p_exposure) <* eol

注意:betaLine完美运行(为简洁起见,我省略了p_int等的定义:

*HmmPlus> parse betaLine "beta" "BETA 6 11 5 24 -1 oiiio\n"
Right (StrandPair {firstStart = 6, secondStart = 11, pairLength = 5, parallel =   Antiparallel, exposure = [Exposed,Buried,Buried,Buried,Exposed]})

这个其他解析器会出现问题,hmmMatchEmissions

 hmmMatchEmissions     = spaces *> (V.fromList <$> sepBy p_logProb spaces) <* eol <?> "matchEmissions"

*HmmPlus> parse hmmMatchEmissions "me" "      2.61196  4.43481  2.86148  2.75135  3.26990  2.87580  3.69681\n"
Left "me" (line 2, column 1):
unexpected end of input

现在,如果我<* eol从解析器定义中删除,并\n从行中删除,它确实有效:

*HmmPlus> parse hmmMatchEmissions "me" "      2.61196  4.43481  2.86148  2.75135  3.26990  2.87580  3.69681"
Right (fromList [NonZero 2.61196,NonZero 4.43481,NonZero 2.86148,NonZero 2.75135,NonZero 3.2699,NonZero 2.8758,NonZero 3.69681])

那么,为什么在但不是eol的情况下工作?betaLinehmmMatchEmissions

我会注意到这是我唯一使用的地方sepBy;这可能是一个线索吗?

更新:我做了以下事情,现在失败了:/

reqSpaces = many1 (oneOf " \t")

optSpaces = many (oneOf " \t")

hmmMatchEmissions = optSpaces *> (V.fromList <$> sepBy1 p_logProb reqSpaces) <* eol <?> "matchEmissions"

这是失败的:

*HmmPlus> parse hmmMatchEmissions "me" "  0.123  0.124\n"
Left "me" (line 1, column 10):
unexpected "0"
expecting eol

我会注意到第010 列中的意外是0.124令牌的第一个字符。

4

1 回答 1

4

问题似乎是您的p_logProb解析器消耗了空格。所以,这就是解析过程中发生的事情:

  0.123  0.124\n
[]               optSpaces
  [-----]        p_logProb
         {       trying reqSpaces
         {       trying eol
                 failure: expecting eol

解析器应该只使用它解析的p_logProb东西,即实际数字。这将导致预期的解析:

  0.123  0.124\n
[]               optSpaces
  [---]          p_logProb
       []        reqSpaces
         [---]   p_logProb
              {  trying reqSpaces
              #  eol
于 2012-05-24T19:42:00.247 回答