Hi Tony,

On Sat, May 17, 2008 at 12:14 AM, Tony Djordjevski <[EMAIL PROTECTED]> wrote:
>  Thanks for the reply.  I'm actually working with Curtis on this; we
> happened to fix it by casting the denominator to a float, so no worries on
> getting it fixed for 3.1.
>
>  We also tried to use the double forward slash operator to enable true
> division but we were getting errors because we believe ANTLR was
> interpreting the operator as a comment in our grammar.  We tried all sorts
> of escaping with no luck.  How would one do something like the following:
>
>  expr returns [value]
>         ... (stuff omitted for brevity)
>         |       ^(DIV a = expr  b = expr)       {$value = a//b}
>         ... (rest of stuff)

I guess you mean something like that:
---
grammar T;
options { language=Python; }
a: 'a' { foo = 1 // 2 };
---
ANTLR Parser Generator  Version 3.1b1 (??)  1989-2007
error(10):  internal error: Exception /tmp/T.g:4:1: expecting '}',
found '<EOF>'@org.antlr.tool.ANTLRLexer.nextToken(ANTLRLexer.java:333):
unexpected stream error from parsing /tmp/T.g

error(150):  grammar file /tmp/T.g has no rules
error(100): /tmp/T.g:0:0: syntax error: assign.types: <AST>:0:0:
unexpected end of subtree
error(100): /tmp/T.g:0:0: syntax error: define: <AST>:0:0: unexpected
end of subtree

Ter: that looks like a bug to me...

A usable workaround seems to be to move the closing brace to the next line, i.e.
a: 'a' { foo = 1 // 2
    };


-Ben

>  Benjamin Niemann wrote:
>
> > HI Curtis,
> >
> > good point, haven't thought about his yet.
> > Should be fairly easy to fix, but it may not make it into the upcoming
> > V3.1 release.
> > If you are not afraid of building antlr yourself:
> >
> > In src/org/antlr/codegen/templates/Python/Python.stg there's a line
> >
> > <actions.(actionScope).header>
> >
> > move it up right before the <@imports> line. Then you should be able to
> say
> >
> > @header {
> >    from __future__ import division
> > }
> >
> > and it should appear where you need it.
> >
> > As a long term solution I probably have to introduce a separate @
> > action, because e.g. when having other things than imports in @header,
> > you want these to be after the 'from antlr import *'.
> >
> > -Ben
> >
> > On Fri, May 16, 2008 at 4:19 PM, Curtis Deck <[EMAIL PROTECTED]> wrote:
> >
> > >
> > >
> > >
> > > I am writing an expression evaluator using antlr 3 and python. I want to
> do
> > > real division but I cannot use from __future__ import division because
> it
> > > needs to be at the top of the script.
> > >
> > >
> > >
> > > Can any one help me please.
> > >
> > >
> > >
> > > Thanks in advance!
> > >
> >
> >
>
>
_______________________________________________
antlr-dev mailing list
[email protected]
http://www.antlr.org:8080/mailman/listinfo/antlr-dev

Reply via email to