문제

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

도움이 되었습니까?

해결책

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/

다른 팁

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
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top