[antlr-interest] Allow empty file/input

Jim Idle jimi at temporal-wave.com
Tue Nov 23 10:33:22 PST 2010


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

 



More information about the antlr-interest mailing list