[antlr-interest] Interesting error in attribute access

Jim Idle jimi at temporal-wave.com
Sun May 6 14:41:21 PDT 2012


Use:

r
@init {
bool haveX = false;
bool haveY = false;
: (x=X { haveX = true; } | y=Y {haveY = true; }

...

But why not just

-> ^(TOK_BLAH $x? $y?)

As only one will be present.

Jim

> -----Original Message-----
> From: antlr-interest-bounces at antlr.org [mailto:antlr-interest-
> bounces at antlr.org] On Behalf Of bharath vissapragada
> Sent: Sunday, May 06, 2012 8:48 AM
> To: antlr-interest
> Subject: [antlr-interest] Interesting error in attribute access
>
> Hi all,
>
> My grammar is as follows
>
> A = blah blah (X|Y)?   // I can have one or none X , Y but not both
>
> Now my grammar is as follows :
>
> A :  blah blah (x=X|y=Y)?
>  -> {$x ==  null } ^(tok_blah X)
> -> {$y == null} ^(tok_blah Y)
> ->   ^ (tok_blah)
> ;
>
> When I compile my grammar .. It raises "missing attribute access on
> rule scope" ..I googled a bit and changed my grammar to the following
>
> A :  blah blah (x=X|y=Y)?
>  -> {$x.text ==  null } ^(tok_blah X)
> -> {$y.text == null} ^(tok_blah Y)
> ->   ^ (tok_blah)
> ;
>
> and now the grammar compiles correctly. When I give the input as  "blah
> blah X" .. it parses it correctly ..However if I give "blah blah Y" ..
> it fails ...I am using java and I looked into the source of the code
> generated by grammar.
>
> The code is trying to access attribute "text" of null variable $x
> without checking if $x is null.  Any workaround for this error?
>
> I need to check it the variable $x is null instead of checking if its
> 'text" attribute is null. Any help is highly appreciated.
>
> Thanks and Regards,
>
>
> --
> Regards,
> Bharath .V
> w:http://researchweb.iiit.ac.in/~bharath.v
>
> 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