[antlr-interest] Implicit Typecasting with a tree grammar

Tilman Bender tbender at stud.hs-heilbronn.de
Tue Apr 28 11:36:21 PDT 2009


Hi,

I love replying to myself ;-)

I found some way of achieving the implicit uppercast, but not using a  
tree grammar.
I wrote my grammar to distinguish between int and float expressions  
right from the start.
Unfortunately I now run into trouble. I added boolean expressions to  
my grammar and cannot find a way to
make boolean expression like:

bool a := 3<4

work, but prevent my grammar from accepting things like

bool a:=4

which is obviously bad.
So I had a look at the Java grammars on antlr.org and found that, they  
do not impose type restrictions
on the parser side. E.g. with Terrence's Java 1.5 grammar:

private boolean b = 3;

is totally valid.

I know that trying to catch violations in typing via the parser- 
grammar was a bad idea.
But I cannot come up with a good solution to do this in an extra stage  
via a TreeWalker.

Any help would be greatly appreciated.


Tilman Bender
Student des Software Engineering
Hochschule Heilbronn
tbender at stud.hs-heilbronn.de



Am 09.04.2009 um 13:35 schrieb Tilman Bender:

> Hi there,
>
> I am still playing with my interpreter for arithmetic expressions.
> Now I would like to work with mixed expressions containing integer  
> and floating point numbers.
>
> When doing so I would like to have the integers automatically  
> "uppercasted" when
> they occur in an expression together with floats.
>
> So what I currently do is, that I have a Java class providing  
> methods for the respective
> arithmetic operations and invoke it from my interpreter:
>
>  ^(MINUS a=expr b=expr)  { $value = Arithmetic.subtract($a.value, 
> $b.value);}
>
> In the add-method I check for the type of the parameters passed in  
> and do the conversion
> to BigDecimal magic.
>
> However I would like to make this "implicit uppercast" independant  
> from the host language for
> my interpreter to minimize the amount of code I have to change, when  
> switching the host language.
>
> Has anyone already done such a thing via a TreeWalker?
>
> Tilman Bender
> Student des Software Engineering
> Hochschule Heilbronn
> tbender at stud.hs-heilbronn.de
>
>
>
>
> List: http://www.antlr.org/mailman/listinfo/antlr-interest
> Unsubscribe: http://www.antlr.org/mailman/options/antlr-interest/your-email-address

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.antlr.org/pipermail/antlr-interest/attachments/20090428/c88cfafa/attachment.html 
-------------- next part --------------
A non-text attachment was scrubbed...
Name: Arith.g
Type: application/octet-stream
Size: 6705 bytes
Desc: not available
Url : http://www.antlr.org/pipermail/antlr-interest/attachments/20090428/c88cfafa/attachment.obj 
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.antlr.org/pipermail/antlr-interest/attachments/20090428/c88cfafa/attachment-0001.html 
-------------- next part --------------
A non-text attachment was scrubbed...
Name: Arith.testsuite
Type: application/octet-stream
Size: 3322 bytes
Desc: not available
Url : http://www.antlr.org/pipermail/antlr-interest/attachments/20090428/c88cfafa/attachment-0001.obj 
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.antlr.org/pipermail/antlr-interest/attachments/20090428/c88cfafa/attachment-0002.html 


More information about the antlr-interest mailing list