0

I have a left recursive grammar. My AST looks something like:

...
and Expr = BinaryExpr of BinaryExpr
and BinaryExpr = Expr * BinaryOperator * Expr
and BinaryOperator = Plus
...

I was planning on using the operator precedence parser for this, for example:

let exprOpp = new OperatorPrecedenceParser<Expr,unit,unit>()
let pBinaryExpr = exprOpp.ExpressionParser
exprOpp.TermParser <- pExpr

let consBinExpr op x y = (x, op, y) |> BinaryExpr
exprOpp.AddOperator(InfixOperator("+", ws, 1, Associativity.Left, (consBinExpr Plus)))

where pExpr is a parser for Expr.

The left recursion causes a stack overflow. I was wondering whether there was a particular FParsec-way of dealing with this while still using the OperatorPrecedenceParser?

Thanks!

EDIT:

The issue may be coming from the fact that pExpr forwards all calls to another parser pExprRef.

let pExpr, pExprRef = createParserForwardedToRef()

and after the operator precedence parser, the reference is defined:

do pExprRef :=
    choice [pBinaryExpr, <other-parsers...>]; 
falkmar
  • 53
  • 3
  • Your example would never terminate; you'd need another option in `Expr` that represents a single value. As it is, your example types are infinitely recursive. – rmunn Mar 07 '19 at 00:29
  • Thanks. I also got rid of the `createParserForwardedToRef` as it seems `pExpr` forwards all calls to the `TermParser` – falkmar Mar 09 '19 at 12:28

0 Answers0