[antlr-interest] THelp w/ Tree Grammer - Rule Action Error

John B. Brodie jbb at acm.org
Fri Mar 19 12:40:36 PDT 2010


Greetings!

On Fri, 2010-03-19 at 15:26 -0400, William Koscho wrote:
> Hi All,
> 
> I have a tree grammar, and am trying to just print out some information from
> the tree.  This works fine for matching tokens, but not when matching the
> rules.  I'm hoping someone can help explain why this is giving me a
> NullPointerException and how to correct it:
> 
> Tree Grammar:
> 
> // the $i.text causes the NullPointerException, when I $i.text, it works
> fine
> interfaceDeclaration:  ^(i=interfaceType ID) { System.out.println($i.text +
> ": " + $ID.text); };
> 
> interfaceType:
>    PROVIDES
>  | REQUIRES;
> 
> Corresponding Parser Grammar:
> 
> interfaceDeclaration:   interfaceType ID -> ^(interfaceType ID);
> interfaceType:
>    'provides' -> PROVIDES
>  | 'requires' -> REQUIRES;
> 

I believe your interfaceType parser rule does not initialize the text of
the token it creates for the tree node. You should be able to inspect
the generated code and verify this....

I believe you want your interfaceType parser rule to be:

interfaceType : 
    ( k='provides' -> PROVIDES[$k] ) 
  | ( k='requires' -> REQUIRES[$k] )
  ;

if i recall correctly, using the [] stuff sets both the text and the
location of the generated virtual token.

and oh by the way are you sure you really need the virtual tokens?

hope this helps...
   -jbb




More information about the antlr-interest mailing list