1

I keep getting a NullPoiterException in my TreeWalker but I can't seem to find out why.

I can't post the whole grammar, cause it's far too long.

This is the rule in the treeWalker where antlrWorks says the problem is:

collection_name returns [MyType value]
    : ID { $value = (MyType) database.get($collection_name.text); }
    ;

Note that database is a HashMap.

Thank you!

1 Answer 1

2

I can't post the whole grammar, cause it's far too long.

The following is more "readable" and does exactly the same as your original rule:

collection_name returns [MyType value]
 : ID { $value = (MyType) database.get($ID.text); }
 ;

Perhaps do some sanity checks:

collection_name returns [MyType value]
 : ID 
   {
     Object v = database.get($ID.text);
     if(v == null) {
       throw new RuntimeException($ID.text + " unknown in database!");
     }
     $value = (MyType) v;
   }
 ;

EDIT

As you already found out, accessing the .text attribute of a rule is not possible in a tree grammar (only in a parser grammar). In tree grammars, every rule is of type Tree and knows a .start and .end attributes instead. Tokens can be accessed the same in both parser- and tree-grammars. So $ID.text works okay.

3
  • Yes, you are right ,I obviously did not poste enough code. Anyway, that is not the problem. The problem is that somehow my AST is not parsed how it should be. Commented Aug 22, 2012 at 17:36
  • The problem was that in a treeGrammar you can't do something like $collection_name.text , as you would in a parser grammar. That was the whole problem. I spent hours thinking my AST is wrong and trying to fix it. Thank you for your help! Commented Aug 22, 2012 at 23:21
  • @user1550876, cool, I somehow read over the fact that it was a tree tree! Good to hear you resolved it.
    – Bart Kiers
    Commented Aug 23, 2012 at 6:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.