f# - Applying a function returned from a subparser with fparsec -
noob alert!
ok, i'm trying build simple math expression parser in fparsec. right want handle strings "1+2-3*4/5" , return double result of evaluation. no spaces, newlines, or parens, , left right order of operations fine.
here's have far:
let number = many1 digit |>> fun ds -> int <| string.concat(ds) let op : parser<int -> int -> int, unit> = charreturn '+' (+) <|> charreturn '-' (-) <|> charreturn '*' (*) <|> charreturn '/' (/) let expression, expressionimpl = createparserforwardedtoref() expressionimpl := choice[ attempt(number .>> op >>. expression); number] let test p str = match run (p .>> eof) str | success(result, _, _) -> printfn "success: %a" result | failure(result, _, _) -> printfn "failure: %a" result [<entrypoint>] let main argv = test expression "1+1/2*3-4" console.read() |> ignore 0
in first choice of expression parser, i'm not sure how apply function returned op parser.
as usual, find answer right after posting question (after 3 hours of searching).
i changed line:
attempt(number .>> op >>. expression);
to this:
attempt(pipe3 number op expression (fun x y z -> y x z));
however, realized expressions parse backwards. drawing board.
Comments
Post a Comment