1

I would like to get this parser to work. I am currently not quite sure where the error is. It is largely taken from the Parsec section of "Write you a scheme"

One more question: if it eventually works, are there any means to ensure that the string that starts with a question mark is always the last in the list?

input_text :: String
input_text = "Eval (isFib::, 1000, ?BOOL)"

data LTuple
   = Command [LTuple]
   | Number Integer
   | String String
   | Query Bool
   deriving (Eq, Ord, Show)

data Command = Rd | Read | In | Take | Wr | Out | Eval
    deriving (Eq, Ord, Show)

main :: IO ()
main = do
  case parse lindaCmd "example" input_text of
    Left err -> print err
    Right res -> putStrLn $ "I parsed: '" ++ res ++ "'"

parseList :: Parser lindaTpl
parseList = liftM Command $ sepBy1 lindaCmd  ( symbol "," )

lindaCmd =   string "rd"
         <|> string "take"
         <|> string "out"
         <|> string "eval"
         <|> do char '('
                x <- try parseList
                char ')'return x

The current error message is:

test.hs:30:48:
    Couldn't match expected type `ParsecT s0 u0 m0 sep0'
                with actual type `String -> ParsecT s1 u1 m1 String'
    In the return type of a call of `symbol'
    Probable cause: `symbol' is applied to too few arguments
    In the second argument of `sepBy1', namely `(symbol ",")'
    In the second argument of `($)', namely
      `sepBy1 lindaCmd (symbol ",")'

test.hs:38:17:
    The function `char' is applied to three arguments,
    but its type `Char -> ParsecT s0 u0 m0 Char' has only one
    In a stmt of a 'do' block: char ')' return x
    In the second argument of `(<|>)', namely
      `do { char '(';
            x <- try parseList;
            char ')' return x }'
    In the second argument of `(<|>)', namely
      `string "eval"
       <|>
         do { char '(';
              x <- try parseList;
              char ')' return x }'
4

1 回答 1

3

第一个错误:

parseList = liftM Command $ sepBy1 lindaCmd  ( symbol "," )

应该是

parseList = liftM Command $ sepBy1 lindaCmd  ( string "," )

第二个错误,正如 dbaupp 指出的那样,

     <|> do char '('
            x <- try parseList
            char ')'return x

应该

     <|> do char '('
            x <- try parseList
            char ')'
            return x

这应该可以解决您现在遇到的两个错误,但我还没有检查是否还有其他错误。


编辑:

你有

parseList :: Parser lindaTpl

但我希望你想要

parseList :: Parser LTuple

我希望你也想要

lindaCmd :: Parser LTuple

并且还必须修改lindaCmd--- 除了最后一个分支之外的所有内容似乎都想要类型Parser String......

于 2012-11-22T15:05:23.497 回答