User-defined operators and compound expressions using Happy

Duncan Coutts duncan.coutts at worc.ox.ac.uk
Mon Nov 22 13:09:45 EST 2004


On Mon, 2004-11-22 at 17:48 +0100, Frank-Andre Riess wrote:
> Hi there folks,
> 
> once again, I've got a question related to Happy (I've got version 1.13 at
> the moment).
> Maybe, it's even more a question on formal languages, but well...
> How can I write a grammar that can cope with user-defined operators (of
> different precedences/associativities

One standard solution is to parse user defined operators as if they were
all one precedence/associativity and then re-associate them later once
you know what the precedence and associativity of each operator is.

That way the parser grammar does not need to be adjusted on the fly.

So you wold parse
1+2*3
as
[LiteralInt 1, Op '+', LiteralInt 2, Op '*', LiteralInt 3]
and then later turn that into
BinOp '+' (LiteralInt 1) (BinOp '*' (LiteralInt 2) (LiteralInt 3))
using your mapping of operators to precedence/associativity.

Duncan



More information about the Glasgow-haskell-users mailing list