User-defined operators and compound expressions using Happy

Frank-Andre Riess riess at informatik.uni-muenchen.de
Wed Nov 24 08:57:26 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

Thank you very much. What I did by now is more or less along the lines of
your suggestion, but it doesn't work to my satisfication yet. Might be due
to overloaded tokens, though (e.g. '|' is both a set union operator and
the seperator in case statements).
Maybe we'll need to redesign the syntax.


More information about the Glasgow-haskell-users mailing list