[antlr-interest] Help with Null Pointer Exception

swathy murthy pskswathy at gmail.com
Tue Mar 2 07:11:52 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:
               System.out.println(output.toString());
If I am removing this line, there is no error but I am not able to
print the output. I have attached the grammar files, template file and
the input file.
Please help to me to solve the error.

Thanks a lot.
Swathy
-------------- next part --------------
A non-text attachment was scrubbed...
Name: g.g
Type: application/octet-stream
Size: 1129 bytes
Desc: not available
Url : http://www.antlr.org/pipermail/antlr-interest/attachments/20100302/0c228c14/attachment.obj 
-------------- next part --------------
A non-text attachment was scrubbed...
Name: m.g
Type: application/octet-stream
Size: 1544 bytes
Desc: not available
Url : http://www.antlr.org/pipermail/antlr-interest/attachments/20100302/0c228c14/attachment-0001.obj 
-------------- next part --------------
A non-text attachment was scrubbed...
Name: t.g
Type: application/octet-stream
Size: 445 bytes
Desc: not available
Url : http://www.antlr.org/pipermail/antlr-interest/attachments/20100302/0c228c14/attachment-0002.obj 
-------------- next part --------------
A non-text attachment was scrubbed...
Name: input.mapcode
Type: application/octet-stream
Size: 16 bytes
Desc: not available
Url : http://www.antlr.org/pipermail/antlr-interest/attachments/20100302/0c228c14/attachment-0003.obj 
-------------- next part --------------
A non-text attachment was scrubbed...
Name: sal_prog_template.stg
Type: application/octet-stream
Size: 1088 bytes
Desc: not available
Url : http://www.antlr.org/pipermail/antlr-interest/attachments/20100302/0c228c14/attachment-0004.obj 


More information about the antlr-interest mailing list