Fparsec recursive grammatics throw StackOverflowEx

2020-03-30 06:36发布

问题:

I've got this code

type Exprs = 
    | Val of float
    | Mult of Exprs  * Exprs
    | Plus of Exprs  * Exprs

let pexpr, exprRef = createParserForwardedToRef<Exprs, unit>()
let pval = pfloat |>> Val
let binaryOp s = (ws >>. pexpr.>> ws) .>>. (ws >>. str s >>. ws >>. pexpr)
let pplus = binaryOp "+" |>> Plus 
let pmuil = binaryOp "*" |>> Mult

do exprRef := choice [ attempt pmuil
                       pplus
                       pval ]

let expression = ws >>. pexpr .>> ws

When it evaluated it throws StackoverflowExcpetion. So the question is how can i write it without infinitie recursion?