![]() The try combinator changes this behavior by "undoing" the consumption of input in case of failure. In other words, by default, after the left-hand side of the alternative consumes some input and then fails, there is no "backtracking" to put that consumed input back. The doesn't try its right-hand side alternative parser ( primitive) because p q only tries q if p fails without consuming input, and that's not what happened here. As a result, binOp fails after consuming input. The binOp parser consumes a primitive, namely "x", and then fails to parse add (because there's no " " character). The problem in this second case is that the operator tries binOp first. It parses "x y" okay: λ> parseTest parser "x y"īinOp Add (Primitive "x") (Primitive "y")īut it fails to parse "x" alone: λ> parseTest parser "x" Consider a simplified version of your parser that allows no spaces, only single-letter primitives, and addition of two primitives as the only operation. Note the emphasized phrase - that's emphasis in the original documentation, not emphasis I've added. If p fails without consuming any input, parser q is tried. If it succeeds, the value of p is returned. This may make little sense until you match it up with the documentation for the () operator which states: The parser try p behaves like parser p, except that it pretends that it hasn't consumed any input when an error occurs. The documentation for try doesn't actually use the word "backtrack". Main = print $ Parsec.parse (parser binOp and primitive fails, it should go to the binOp option.ĭesired parse is BinOp Add (Primitive " #-!") (Primitive "-$!
0 Comments
Leave a Reply. |
Details
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |