|User defined precedence for user defined operators. email@example.com (glen herrmannsfeldt) (2006-08-14)|
|Re: User defined precedence for user defined operators. firstname.lastname@example.org (Tommy Thorn) (2006-08-15)|
|Re: User defined precedence for user defined operators. DrDiettrich1@aol.com (Hans-Peter Diettrich) (2006-08-15)|
|Re: User defined precedence for user defined operators. email@example.com (Marco van de Voort) (2006-08-15)|
|Re: User defined precedence for user defined operators. firstname.lastname@example.org (Michael Tiomkin) (2006-08-15)|
|User defined precedence for user defined operators. email@example.com (Derek M Jones) (2006-08-15)|
|Re: User defined precedence for user defined operators. firstname.lastname@example.org (2006-08-18)|
|Date:||18 Aug 2006 00:58:38 -0400|
|Organization:||Department of Computer Science, University of Copenhagen|
|References:||<email@example.com> <firstname.lastname@example.org> <email@example.com> <1hk1yfv.zyn74w12uw4xsNfirstname.lastname@example.org> <44E0B3FF.email@example.com> 06-08-081 06-08-089|
|Keywords:||Fortran, syntax, design|
|Posted-Date:||18 Aug 2006 00:58:38 EDT|
"Tommy Thorn" <firstname.lastname@example.org> writes:
> glen herrmannsfeldt wrote:
> > For new readers to this discussion, the question relates to being
> > able to specify the precedence for user defined operators (operator
> > overloading), and the possible ambiguities that can cause.
> It's a horrid idea, but it can be done. Fx. you could do it by
> completely ignoring operator precedence and reorder the tree at some
> point after parsing using the type information (this implies you'd
> have to keep parenthesis in the parse tree). Doing it on the fly is
> probably only possible with a hand-crafted parser.
If you only have a small number of possible precedences, you can use
different lexical symbols for each class and use a symbol table in the
lexer to decide the precedence class of a token. Precedence
declarations would update the symbol table.
> Notice, this is slightly similar to the problem with parsing C where
> (a) - (b) is ambigous and requires knowing if a is a type or a
C is a lot worse, as it is not the declaration of the operators but
that of the operands that determine parsing.
Allowing unlimited declaration of new operators with different
precedence rules quickly make programs unreadable.
My own suggestion to solve this is to make the precedence be
determined by the name of the operator: You specify an initial set of
operators with a sensible hierarchy of precedence and associativity,
but make sure that operators that start with the same character have
the same precedence rules (different starting characters can share,
though, like + and -). One minor snag is that this does not allow
unary minus to use the same character as infix minus, but some
languages already make this distinction (SML, for example, uses ~ for
User-defined operators must start with a character from the predefined
set and automatically inherits the precedence rules of that operator.
So, if a programmer defines an operator +>, it automatically has the
precedence of +, while >* would have the precedence of >.
Once a programmer knows the standard operator set, he can immediately
see the precedence of new operators without having to find the
declaration. It also makes parsing simpler: The lexer can assign the
precedence class based on the first character, without having to use a
I wouldn't suggest to make the standard hierarchy too large, though.
Many programmers have trouble keeping track of the precedences of the
operators in C, so something smaller would be ideal. I wouldn't go a
far as Pascal, though, where AND and OR has the same priorites as *
Return to the
Search the comp.compilers archives again.