Reputation: 1716
This is the most puzzling combinator in all of FParsec...
http://www.quanttec.com/fparsec/reference/primitives.html#members.chainl1
...but there is no example on how to use it in the documentation or, AFAIK, on any web pages on the internet. I have a left-recursive parse that seems to require it, but for the life of me I can't figure out how to call it or what to pass to it.
Please help :)
Upvotes: 5
Views: 1033
Reputation: 2241
I put together a simple expression parser in FParsec at the end of this unrelated post. Here's an excerpt using chainl1
to make a parser for a chained operator expression from parsers for the operand and operator.
(* fop : (double -> double -> double) -> (env -> double) -> (env -> double) -> env -> double *)
let fop op fa fb env = fa env |> op <| fb env
(* Parse single operators - return function taking two operands and giving the result *)
let (addop : Parser<_,unit>) =
sym "+" >>% fop (+)
<|> ( sym "-" >>% fop (-) )
(* term, expr - chain of operators of a given precedence *)
let term = chainl1 atom mulop
let expr = chainl1 term addop
Upvotes: 2
Reputation: 118895
I have some pretty diagrams involving chainl1
(from my own C# code) here:
http://lorgonblog.wordpress.com/2007/12/04/monadic-parser-combinators-part-three/
Upvotes: 3