递归解析器组合器(字符串)

时间:2015-11-08 22:19:12

标签: parsing haskell recursion parser-combinators

我正在阅读G. Hutton编写的Haskell编程,并在第8章(Parsers)中感到困惑。我无法理解字符串解析器如何递归工作。这是代码(我使用>> =明确地重写了代码,因为符号抽象了更多东西)。

type Parser a = String -> [(a, String)]

failure :: Parser a
failure = \ inp -> []

return :: a -> Parser a
return v = \ inp -> [(v, inp)]

item :: Parser Char
item = \ inp -> case inp of
                [] -> []
                (x : xs) -> [(x, xs)]

(>>=) :: Parser a -> (a -> Parser b) -> Parser b
p >>= f = \ inp -> case parse p inp of
                   [] -> []
                   [(v, out)] -> parse (f v) out

parse :: Parser a -> String -> [(a, String)]
parse p inp = p inp

char :: Char -> Parser Char
char x = sat (== x)

sat :: (Char -> Bool) -> Parser Char
sat p = item >>= \ ch -> if p ch then return ch else failure

到目前为止对我来说很清楚,但现在我没有看到递归字符串是如何工作的。

string :: String -> Parser String
string [] = return []
string (x:xs) = (char x) >>= \ _ ->
                string xs >>= \ _ ->
                return (x:xs)

有人可以在简单的输入上重写递归的每一步:例如string "he" "hell"应返回[("he","ll")]。这就是我的开始。

string ('h':['e']) "hell"

((char 'h') >>= \ _ -> string ['e'] >>= \ _ -> return('h':['e'])) "hell"

(\ inp -> case parse (char 'h') inp of
         [] -> []
         [(v, out)] -> parse ((\ _ -> string ['e'] >>= \ _ -> return('h':['e']) v) out) "hell"

parse ((\ _ -> string ['e'] >>= \ _ -> return('h':['e']) 'h') "ell"

(string ['e'] >>= \ _ -> return('h':['e'])) "ell"

(\ inp -> case parse (string ['e']) "ell" of
          [] -> []
          [('e', "ll")] -> parse ((\ _ return(['h','e']) 'e')) "ll"

((\ _ return "he") 'e') "ll"

return "he" "ll"

[("he", "ll")]

0 个答案:

没有答案