3

I have a problem that I've been stuck on for a while and I would appreciate some help if possible.

I have a few rules in an ANTLR tree grammar:

block
  :  compoundstatement
  |  ^(VAR declarations) compoundstatement
  ;

declarations
  :  (^(t=type idlist))+
  ;

idlist
  :  IDENTIFIER+
  ;

type
  :  REAL
  |  i=INTEGER
  ;

I have written a Java class VarTable that I will insert all of my variables into as they are declared at the beginning of my source file. The table will also hold their variable types (ie real or integer). I'll also be able to use this variable table to check for undeclared variables or duplicate declarations etc.

So basically I want to be able to send the variable type down from the 'declarations' rule to the 'idlist' rule and then loop through every identifier in the idlist rule, adding them to my variable table one by one.

The major problem I'm getting is that I get a NullPointerException when I try and access the 'text' attribute if the $t variable in the 'declarations' rule (This is one one which refers to the type).

And yet if I try and access the 'text' attribute of the $i variable in the 'type' rule, there's no problem.

I have looked at the place in the Java file where the NullPointerException is being generated and it still makes no sense to me.

Is it a problem with the fact that there could be multiple types because the rule is

(^(typeidlist))+

??

I have the same issue when I get down to the idlist rule, becasue I'm unsure how I can write an action that will allow me to loop through all of the IDENTIFIER Tokens found.

Grateful for any help or comments.

Cheers

3
  • I presume this is a tree grammar, not a parser grammar. Correct?
    – Bart Kiers
    Commented Apr 14, 2011 at 15:23
  • That's right. It's a tree grammar. Sorry about the ambiguity.
    – Joe
    Commented Apr 14, 2011 at 15:32
  • No problem Joe, I thought as much, but wanted to make sure before answering.
    – Bart Kiers
    Commented Apr 14, 2011 at 18:47

1 Answer 1

4

You can't reference the attributes from production rules like you tried inside tree grammars, only in parser (or combined) grammars (they're different objects!). Note that INTEGER is not a production rule, just a "simple" token (terminal). That's why you can invoke its .text attribute.

So, if you want to get a hold the text of the type rule in your tree grammar and print it in your declarations rule, your could do something like this:

tree grammar T;

...

declarations
  :  (^(t=type idlist {System.out.println($t.returnValue);}))+ 
  ;

...

type returns [String returnValue]
  :  i=INTEGER {returnValue = "[" + $i.text + "]";}
  ;

...

But if you really want to do it without specifying a return object, you could do something like this:

declarations
  :  (^(t=type idlist {System.out.println($t.start.getText());}))+ 
  ;

Note that type returns an instance of a TreeRuleReturnScope which has an attribute called start which in its turn is a CommonTree instance. You could then call getText() on that CommonTree instance.

1
  • Awesome. Thanks for the reply. I've seen the TreeRuleReturnScope object being returned in the java code generated, but I'll have to check out the api for the CommonTree class. Thanks again. V. helpful.
    – Joe
    Commented Apr 15, 2011 at 16:12

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.