[antlr-interest] Allow empty file/input

Rui Vilão rpvilao at gmail.com
Wed Dec 1 11:22:41 PST 2010


Sorry for the late reply, but I was only able to get back to this today.

I manage to get it to work, but like this:

eat: bananas* EOF

      ->^(TRANSLATION_UNIT bananas*)
;

Then your tree rule is:

eat: ^(TRANSLATION_UNIT bananas*)
 | TRANSLATION_UNIT
     // Empty input
;

Once again thank you,

Rui Vilao

On Tue, Nov 23, 2010 at 6:33 PM, Jim Idle <jimi at temporal-wave.com> wrote:

> The code below IS the example J The rule eat is the top level rule for the
> parser and the tree walker.
>
>
>
> Jim
>
>
>
> From: Rui Vilão [mailto:rpvilao at gmail.com]
> Sent: Tuesday, November 23, 2010 6:28 AM
> To: Jim Idle
> Cc: antlr-interest at antlr.org
> Subject: Re: [antlr-interest] Allow empty file/input
>
>
>
> Hi,
>
> First of all thanks for the quick reply.
>
> I'm not quite following you. Designing the grammar like you said, I'm
> having
> troubles on the tree grammar side and how I can raise the root object that
> represents my whole language.
>
> Do you haven an example regarding this matter?
>
> Thanks again,
>
> --
> Rui
>
> On Mon, Nov 22, 2010 at 7:23 PM, Jim Idle <jimi at temporal-wave.com> wrote:
>
> Looks like your tree grammar does not allow an empty input. I find it
> useful
> to always have a single base node:
>
> eat: b+=bananas+ EOF
>
>       ->^(TRANSLATION_UNIT $b*)
> ;
>
> Then your tree rule is:
>
> eat: ^(TRANSLATION_UNIT bananas+)
>  | TRANSLATION_UNIT
>      // Empty input
> ;
>
>
> Jim
>
>
> > -----Original Message-----
> > From: antlr-interest-bounces at antlr.org [mailto:antlr-interest-
> > bounces at antlr.org] On Behalf Of Rui Vilão
> > Sent: Monday, November 22, 2010 11:03 AM
> > To: antlr-interest at antlr.org
> > Subject: [antlr-interest] Allow empty file/input
> >
> > Hi there,
> >
> > I implemented a simple grammar and I want to add support to allow empty
> > input. Empty input can be for example comments too. So I defined it
> > this way
> >
> > eat: (bananas)+
> >     |
> >     ;
> >
> > bananas: <lots of stuff here>
> >
> > LINE_COMMENT   : '//' ~('\r' | '\n')*   {skip(); };
> >
> > ETC...
> >
> > The thing is if I put anything that does not match the grammar, i.e.
> > empty string, comment, whatever I get the following error when I try to
> > get the object that will represent my DSL:
> >
> > java.lang.NullPointerException: null
> >         at
> > org.antlr.runtime.tree.BaseTreeAdaptor.isNil(BaseTreeAdaptor.java:70)
> > ~[antlr-runtime-3.2.jar:na]
> >         at
> > org.antlr.runtime.tree.CommonTreeNodeStream.nextElement(CommonTreeNodeS
> > tream.java:93)
> > ~[antlr-runtime-3.2.jar:na]
> >         at
> > org.antlr.runtime.misc.LookaheadStream.fill(LookaheadStream.java:94)
> > ~[antlr-runtime-3.2.jar:na]
> >         at
> > org.antlr.runtime.misc.LookaheadStream.sync(LookaheadStream.java:88)
> > ~[antlr-runtime-3.2.jar:na]
> >         at
> > org.antlr.runtime.misc.LookaheadStream.LT(LookaheadStream.java:119)
> > ~[antlr-runtime-3.2.jar:na]
> >         at
> > org.antlr.runtime.tree.CommonTreeNodeStream.LA(CommonTreeNodeStream.jav
> > a:116)
> > ~[antlr-runtime-3.2.jar:na]
> >         at
> > com.feedzai.pulse.service.kpi.parser.KpiTree.script(KpiTree.java:85)
> > ~[classes/:na]
> >
> > Does anyone know how can I achieve this?
> >
> > Thank you in advance,
> >
> > Rui Vilao
> >
>
> > List: http://www.antlr.org/mailman/listinfo/antlr-interest
> > Unsubscribe: http://www.antlr.org/mailman/options/antlr-interest/your-
> > email-address
>
>
> List: http://www.antlr.org/mailman/listinfo/antlr-interest
> Unsubscribe:
> http://www.antlr.org/mailman/options/antlr-interest/your-email-address
>
>
>
>
> List: http://www.antlr.org/mailman/listinfo/antlr-interest
> Unsubscribe:
> http://www.antlr.org/mailman/options/antlr-interest/your-email-address
>


More information about the antlr-interest mailing list