我想讓這個解析器工作。我目前不太確定錯誤在哪裏。它主要來自Parsec部分的「給你寫一個方案」我的parsec解析器有什麼問題?
還有一個問題:如果它最終有效,是否有任何方法可以確保以問號開頭的字符串始終是列表中的最後一個?
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
當前的錯誤信息是:
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 }'
你有'char')'return x' 'lindaCmd'的末尾。 – huon