2
以下解析器爲任何輸入輸入無限循環。爲什麼這個Parsec解析器輸入一個無限循環?
data Ast
= Number Int
| Identifier String
| Operation Ast BinOp Ast
deriving (Show, Eq)
data BinOp = Plus | Minus
deriving (Show, Eq, Enum)
number = Number <$> read <$> many1 digit
identifier = Identifier <$> many1 letter
operator = choice $ mkParser <$> [(Plus, '+'), (Minus, '-')]
where mkParser (f, c) = f <$ char c
operation = Operation <$> ast <*> operator <*> ast
ast :: Parser Ast
ast = operation <|> number <|> identifier
問題出在操作解析器的某個地方。我認爲它與替代解析有關,但我不理解它。
你能解釋一下問題所在嗎?
謝謝!