Re: User defined precedence for user defined operators.

torbenm@app-5.diku.dk (=?iso-8859-1?q?Torben_=C6gidius_Mogensen?=)
18 Aug 2006 00:58:38 -0400

          From comp.compilers

Related articles
User defined precedence for user defined operators. gah@ugcs.caltech.edu (glen herrmannsfeldt) (2006-08-14)
Re: User defined precedence for user defined operators. tommy.thorn@gmail.com (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. marcov@stack.nl (Marco van de Voort) (2006-08-15)
Re: User defined precedence for user defined operators. tmk@netvision.net.il (Michael Tiomkin) (2006-08-15)
User defined precedence for user defined operators. derek@knosof.co.uk (Derek M Jones) (2006-08-15)
Re: User defined precedence for user defined operators. torbenm@app-5.diku.dk (2006-08-18)
| List of all articles for this month |

From: torbenm@app-5.diku.dk (=?iso-8859-1?q?Torben_=C6gidius_Mogensen?=)
Newsgroups: comp.lang.fortran,comp.compilers
Date: 18 Aug 2006 00:58:38 -0400
Organization: Department of Computer Science, University of Copenhagen
References: <1155280055.803334.85200@h48g2000cwc.googlegroups.com> <1155424619.168792.202040@h48g2000cwc.googlegroups.com> <ebpn6e$qbt$1@vilya.larc.nasa.gov> <1hk1yfv.zyn74w12uw4xsN%nospam@see.signature> <44E0B3FF.4040401@cits1.stanford.edu> 06-08-081 06-08-089
Keywords: Fortran, syntax, design

"Tommy Thorn" <tommy.thorn@gmail.com> 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
> variable.


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
unary minus).


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
symbol table.


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 *
and +.


                Torben


Post a followup to this message

Return to the comp.compilers page.
Search the comp.compilers archives again.