Domanda

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 :)

È stato utile?

Soluzione

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/

Altri suggerimenti

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
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top