[Haskell-cafe] Parsec: buildExpressionParser of a different type than parsed term

Albert Y. C. Lai trebla at vex.net
Mon Apr 9 19:19:17 EDT 2007

Joel Reymont wrote:
> I'm trying to parse NumExpr > NumExpr (example) which should return a 
> logical expression while parsing numeric terms. I can't figure out how 
> to make buildExpressionParser do this for me since it takes the type of 
> the term parser given to it. If I supply a parser for numeric terms then 
> I cannot return a logical expression.
> Note that I do not want to put all expressions into the same Expr type 
> since it makes generating valid random ASTs with QuickCheck impossible.

As you probably suspect, one single use of buildExpressionParser cannot 
accomplish it. It is equivalent to the problem of homogeneous lists.

However, you can make two uses of buildExpressionParser, one for numeric 
expressions and the other for logical expressions. Mutual reference is 
no problem since, in all practical uses, even with just one single 
buildExpressionParser you already have the term parser call its own 
buildExpressionParser; now you just have one term parser call the 
other's buildExpressionParser. Sharing the same token parser is 
perfectly alright.

More information about the Haskell-Cafe mailing list