【发布时间】:2015-06-20 13:19:30
【问题描述】:
我尝试用关键字之间的一系列数据解析以下文本文件:
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
当我解析我的文本文件时,我收到以下错误:
"(stdin)" (line 16, column 1):
unexpected end of input
expecting "\n", "\r\n" or "END\n"
我是解析的新手,我不明白为什么会失败?
我的序列还在BEGIN和END之间
你知道我的解析器出了什么问题以及如何纠正它吗?
【问题讨论】:
-
请包含产生解析错误的实际代码(以及字符串输入或文本文件)。