[antlr-interest] Help with Null Pointer Exception

John B. Brodie jbb at acm.org
Tue Mar 2 08:25:03 PST 2010


Greetings!

On Tue, 2010-03-02 at 20:41 +0530, swathy murthy wrote:
> Hi
> 
> I am currently working on a language translation. So, I have created a
> template and the tree grammar. But, the template is not executing. It
> is giving a Null Pointer Exception. I have the following grammars:
...snipped....

If you look closely at the generated t.java file (and thankfully that is
a strength of ANTLR that one can inspect the generated code without
being driven insane, but i shouldn't digress...).

You will see that the generated program_return structure contains the
field `st` and that is what is returned by a call upon `getTemplate()`.

Further in the code for the program rule contains no initialization for
this `st` field! Thus the null pointer exception back in your Main when
you try to print it.

Unlike AST construction, ANTLR can not, by default, set any of the st
values returned by your rules. There is no reasonable implicit template
value.

So when using templates, you really want some form of
     -> {...result here...}
in *every* rule so that you explicitly govern the template generation
and propagation.

change your t.g program to be `program : stat -> {$stat.st} ;` and the
null pointer exception goes away. 

Hope this helps...
   -jbb





More information about the antlr-interest mailing list