[antlr-interest] Operator overloading?

Vladimir Gusev vgusev at graphlogic.com
Mon Feb 26 07:53:28 PST 2007


Andy,

Thanks for your comment. This is very similar to how I do it - by manually
overriding the default AST behaviour. I hoped this could be done in the
grammar file somehow. I guess it could not.


Vladimir


This communication, including attachments, is for the exclusive use of
addressee and may contain proprietary, confidential and/or privileged
information. If you are not the intended recipient, any use, copying,
disclosure, dissemination or distribution is strictly prohibited. If you are
not the intended recipient, please notify the sender immediately by return
e-mail, delete this communication and destroy all copies.


-----Original Message-----
From: Andy Tripp [mailto:antlr at jazillian.com] 
Sent: Saturday, February 24, 2007 5:29 PM
To: Vladimir Gusev
Cc: 'ANTLR Interest'
Subject: Re: [antlr-interest] Operator overloading?

Vladimir Gusev wrote:
> Hello,
>
> Is there a way to implement in my language operator overloading using
ANTLR?
> I would appreciate any thoughts about this.
>
> Thanks.
>
> Vladimir
>   
Yes, I think this could be done. I replace C++ operator overloading with
Java function calls as part of my C++ to Java translator (jazillian.com),
which is a little different but related.
Say you're creating a binary "+" operator between two Money objects like
this. You'd parse the source files into ASTs, search the ASTs for binary "+"

operators, and then
figure out the types of the left and right ASTs. If they're both type Money,
then this is the thing you're looking for, and you could replace the "+"
node in the AST by something else, such as some METHOD_CALL type of node.

Print out your modified AST, and you've just used ANTLR as a preprocessor to
convert operator overloading to code without operator overloading. In that
way, your "money1 + money2" expressions would become "money1.plus(money2)".

Andy




More information about the antlr-interest mailing list