[antlr-interest] newbie stringtemplate question
Pete Siemsen
siemsen at UCAR.EDU
Thu Nov 8 20:12:07 PST 2007
I have a working ANTLR 3 parser that recognizes my input files. Now
I want to use StringTemplate to make some output. I stole code from
the example on page 205 of the book, but I'm having trouble with
getTemplate. My translator gives
$ java -cp /Users/siemsen/cimmof2java/target/cimmof2java-1.0-
SNAPSHOT.jar: \
/Users/siemsen/antlrplay/antlr-3.0.1/lib/antlr-2.7.7.jar: \
/Users/siemsen/antlrplay/antlr-3.0.1/lib/antlr-3.0.1.jar: \
/Users/siemsen/antlrplay/antlr-3.0.1/lib/antlr-
runtime-3.0.1.jar: \
/Users/siemsen/antlrplay/antlr-3.0.1/lib/
stringtemplate-3.1b1.jar
cimmof2java cimv216.mof /Users/siemsen/cimmof2java/cimmof2java-
java.stg /tmp
Exception in thread "main" java.lang.NullPointerException
at cimmof2java.main(cimmof2java.java:38)
$
Line 38 is the 3rd-from-last line of the program:
import org.antlr.runtime.*;
import org.antlr.stringtemplate.*;
import java.io.*;
public class cimmof2java {
public static void main(String[] args) throws Exception {
String inputFileName = args[0];
String stgFileName = args[1];
String outputDirectoryName = args[2];
StringTemplateGroup templates = null;
// Load template group file cimmof2java.stg into templates variable
try {
FileReader groupFileR = new FileReader(stgFileName);
templates = new StringTemplateGroup(groupFileR);
groupFileR.close();
} catch (IOException FileNotFoundException) {
System.out.println("couldn't open string template group file: " +
stgFileName);
System.exit(1);
}
// Open an input file stream from the given file name
CharStream input = new ANTLRFileStream(inputFileName);
// Create a lexer that feeds from the input file stream
cimmof2javaLexer lexer = new cimmof2javaLexer(input);
// Create a stream of tokens fed by the lexer
CommonTokenStream tokens = new CommonTokenStream(lexer);
// Create a parser that feeds off the token stream
cimmof2javaParser parser = new cimmof2javaParser(tokens);
// Give the parser the string templates
parser.setTemplateLib(templates);
// Begin parsing at rule mofSpecification
cimmof2javaParser.mofSpecification_return retVal =
parser.mofSpecification(outputDirectoryName);
StringTemplate output = (StringTemplate)retVal.getTemplate();
// Emit the translation
System.out.println(output.toString());
}
}
The program reads in my group file, named cimmof2java-java.stg, which
contains only this:
group cimmof2java;
showclass(clName) ::= "<clName>"
My parser contains these lines among others:
grammar cimmof2java;
options {output=template;}
className
: Identifier -> showclass(clName={$Identifier.text})
;
The className rule in the parser definitely gets executed. What's
the problem?
-- Pete
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.antlr.org/pipermail/antlr-interest/attachments/20071108/6e0806b7/attachment.html
More information about the antlr-interest
mailing list