[antlr-interest] templates and question marks (-> '?')

Jim Idle jimi at temporal-wave.com
Tue Jul 1 12:18:51 PDT 2008


On Tue, 2008-07-01 at 19:47 +0200, Jens Boeykens wrote:

> Hi,
> 
> Consider a grammar:
> 
> grammarDef
>     :   ^( grammarType ID DOC_COMMENT? optionsSpec? tokensSpec? attrScope* action* rule+ )
>     ;
> 
> If I want to add a template rewrite rule and use e.g. optionsSpec:
> 
> grammarDef
>     :   ^( grammarType ID DOC_COMMENT? optionsSpec? tokensSpec? attrScope* action* rule+ ) -> temp(options={$optionsSpec.text})
>     ;
> 
> This can give nullpointer exceptions when there is no optionsSpec given 
> (note the '?').
> What is the easiest way to change the rewrite and avoid an exception?


Use conditional rewrites such as:

gd
@init { boolean haveOptions = false;}
:
    ^(  x y (options {haveOptions = true})?)  

   -> {haveOptions}? temp....
   -> othertemp ...
;

I use a flag because I have been seeing a confusing scheme with labels,
which I need to ask Ter about i think:

 x=options?

-> {$x == null} xxxxxxxxxx

Missing attribute for $x

but if you use $x.tree then you will get a null pointer exception.

Jim
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.antlr.org/pipermail/antlr-interest/attachments/20080701/95353618/attachment.html 


More information about the antlr-interest mailing list