Parsec: buildExpressionParser of a different type than parsed term

Folks, 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. How can this be resolved? 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. Thanks, Joel -- http://wagerlabs.com/

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.

Albert, On Apr 10, 2007, at 12:19 AM, Albert Y. C. Lai wrote:
As you probably suspect, one single use of buildExpressionParser cannot accomplish it. It is equivalent to the problem of homogeneous lists.
The issue is that I need buildExpressionParser to parse numerical expression but return logical ones, i.e. data CondExpr = CondExpr Op NumExpr NumExpr The way b-E-P is set up it wants NumExpr to be its return type and thus I cannot return a CondExpr. I'm still scratching my head but in all likehood I'll have to hack a custom version of b-E-P. Thanks, Joel -- http://wagerlabs.com/
participants (2)
-
Albert Y. C. Lai
-
Joel Reymont