[antlr-interest] Help with Null Pointer Exception

swathy murthy pskswathy at gmail.com
Mon Mar 1 22:01:22 PST 2010


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:
m.g is the main input grammar from which tree grammar g.g and finally,
I'm creating template t.g as the output.

My Java entry program:

import java.io.*;
import org.antlr.runtime.*;
import org.antlr.runtime.tree.*;
import org.antlr.stringtemplate.*;

public class Main {
    public static StringTemplateGroup templates;
    public static void main(String[] args) throws Exception {
	
		//Read the template file
		String stg = null;
		int a = 0;
		if ( args.length<=1 )
		{
			stg = "sal_prog_template.stg";
		}
		else
		{
			stg = args[a];
			a++;
		}
		templates = new StringTemplateGroup(new FileReader(stg));

		//Parse input and build AST				
		ANTLRInputStream input = new ANTLRInputStream(System.in);
    		mLexer lexer = new mLexer(input);
		CommonTokenStream tokens = new CommonTokenStream(lexer);
		mParser parser = new mParser(tokens);
		mParser.program_return r1 = parser.program();
		CommonTree tree1 = (CommonTree)r1.getTree();
		if (r1 != null)
                  System.out.println(tree1.toStringTree());

		//Build the tree grammar g
	     	CommonTreeNodeStream nodes = new CommonTreeNodeStream(tree1);
		nodes.setTokenStream(tokens);
         	g grammar = new g(nodes);
		g.program_return r2 = grammar.program();
		CommonTree tree2 = (CommonTree)r2.getTree();
		if (r2 != null)
         	  System.out.println(tree2.toStringTree());

		//Build the template
		CommonTreeNodeStream nodes2 = new CommonTreeNodeStream(tree2);
		nodes2.setTokenStream(tokens);
         	t grammar2 = new t(nodes2);
		grammar2.setTemplateLib(templates);
                t.program_return r3 = grammar2.program();
		StringTemplate output = (StringTemplate)r3.getTemplate();
		System.out.println(output.toString());		

    }
}

The error is coming at the line:
		StringTemplate output = (StringTemplate)r3.getTemplate();
		System.out.println(output.toString());		
Please help to me to solve the error.

Thanks a lot.
Swathy


More information about the antlr-interest mailing list