[antlr-interest] StringTemplate null pointer exception with '?' and other constructs, antlr3.0

Hardy, Stephen Stephen_Hardy at rabbit.com
Fri Jul 13 20:33:25 PDT 2007


In June there was a small thread on the problem of working around null
pointer exceptions when a tree grammar used the '?' operator e.g. 

 

init_declarator

    : ^(INIT_DECL declarator oi=initializer?)

    ->template(d={$declarator.st}, i={(oi==null?"":$oi.st)})

<< 

Here is a wonderful declarator: <d> <i; null="## no initializer ##">

>> 

    ;

 

(this shows the ugly workaround which requires tests for null in the
template attribute).  Is this going to be fixed?  Setting to an empty
string in the 'null' case means that the null="..." construct in the
template never gets triggered.

 

Surely it is not asking too much that Antlr should generate the
appropriate check for null when the '?' (or presumably '*') operator is
used.  I have yet to work out how to make use of the null="..."
construct in the template expansion.

 

Here's another seemingly obvious Antlr fragment which never works:

 

type_specifier

    : k=('void'

    | 'char'

    | 'int'  // etc.etc.

    | struct_or_union_specifier

    | enum_specifier

    | type_id)

        -> template(k={$k.text}) "<k>"

    ;

 

In this case the generated code sets k to null at the start, goes
through the normal lookahead/action switches, then issues the following:

 

 

            // TEMPLATE REWRITE

            // 108:9: -> template(k=$k.text) \"<k>\"

            {

                retval.st = new StringTemplate(templateLib, "<k>",

              new STAttrMap().put("k", k.getText()));

            }

 

Unfortunately, nowhere is k set to anything, so it is null and causes
the exception.

 

What am I not understanding?  "The definitive Antlr reference" is not
very definitive when it comes to using string templates :-(  Read the
previous comment as adding "More rigorous description of string template
usage" to the Antlr documentation wish list.

 

Regards,

SJH

 

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.antlr.org/pipermail/antlr-interest/attachments/20070713/b4550a52/attachment-0001.html 


More information about the antlr-interest mailing list