Parsing Parsec的输入意外结束
我尝试用关键字之间的一系列数据解析以下文本文件:Parsing Parsec的输入意外结束,parsing,haskell,parsec,Parsing,Haskell,Parsec,我尝试用关键字之间的一系列数据解析以下文本文件: many text many text many text BEGIN T LISTE2 1 154 2 321 3 519 4 520 5 529 6 426 END many text many text many text 通过使用以下haskell程序 import Text.Parsec import Text.Parsec.String import Text.Parsec.Char impo
many text many text many text
BEGIN
T LISTE2
1 154
2 321
3 519
4 520
5 529
6 426
END
many text many text many text
通过使用以下haskell程序
import Text.Parsec
import Text.Parsec.String
import Text.Parsec.Char
import Text.Parsec.Combinator
endOfLine :: Parser String
endOfLine = try (string "\n")
<|> try (string "\r\n")
line = many $ noneOf "\n"
parseListing = do
spaces
many $ noneOf "\n"
spaces
cont <- between (string "BEGIN\n") (string "END\n") $ endBy line endOfLine
spaces
many $ noneOf "\n"
spaces
eof
return cont
main :: IO ()
main = do
file <- readFile ("test_list.txt")
case parse parseListing "(stdin)" file of
Left err -> do putStrLn "!!! Error !!!"
print err
Right resu -> do putStrLn $ concat resu
我是一个语法分析新手,我不明白为什么它会失败?
我的顺序仍在开始
和结束
你知道我的解析器出了什么问题以及如何纠正它吗?你的
中间的永远不会停止,因为一行一行的endOfLine
会消耗任何一行,而且END\n
也会消耗越来越多的行,直到失败为止。
然后您的解析器尝试使用字符串“END\n”
,但也失败了,这就是为什么错误消息会提到“END\n”
必须重写行分析器才能在END\n
上失败。例如:
parseListing :: Parsec String () [String]
parseListing = do
spaces
many $ noneOf "\n"
spaces
cont <- between begin end $ endBy (notFollowedBy end >> line) endOfLine
spaces
many $ noneOf "\n"
spaces
eof
return cont
where
begin = string "BEGIN\n"
end = string "END\n"
parseListing::Parsec String()[String]
parseListing=do
空间
“\n”中的许多$NONE
空间
(续)内生燧石
空间
“\n”中的许多$NONE
空间
eof
返回控制
哪里
begin=字符串“begin\n”
end=字符串“end\n”
请包括产生解析错误的实际代码(以及字符串输入或文本文件)。
parseListing :: Parsec String () [String]
parseListing = do
spaces
many $ noneOf "\n"
spaces
cont <- between begin end $ endBy (notFollowedBy end >> line) endOfLine
spaces
many $ noneOf "\n"
spaces
eof
return cont
where
begin = string "BEGIN\n"
end = string "END\n"