[antlr-interest] NullPointerException in rewrite rule

Stefan Eder stefan.eder at ebuconnect.de
Wed Dec 19 03:41:52 PST 2007


Hi,

I get a NullPointerException in a rewrite rule with Antlr 3.01.
My intention is to concat several tokens to one new token during AST construction.
Any ideas what I am doing wrong?

Grammar:
	grammar Test;
	options {output=AST;}
	tokens {FLOAT;}
	floatSequence
		: 
		value=(NumberLiteral ('.' NumberLiteral))
		-> ^(FLOAT[$value])
		;
	NumberLiteral	: ('0'..'9') + ;

Main method:
public static void main(String[] args)
throws RecognitionException {
	CharStream charStream = new ANTLRStringStream("0.815");
	TestLexer testLexer = new TestLexer(charStream);
	TokenStream tokenStream = new CommonTokenStream(testLexer);
	TestParser parser = new TestParser(tokenStream);
	parser.floatSequence();
}

Exception stack:
Exception in thread "main" java.lang.NullPointerException
	at org.antlr.runtime.CommonToken.<init>(CommonToken.java:72)
	at org.antlr.runtime.tree.CommonTreeAdaptor.createToken(CommonTreeAdaptor.java:60)
	at org.antlr.runtime.tree.BaseTreeAdaptor.create(BaseTreeAdaptor.java:102)
	at TestParser.floatSequence(TestParser.java:105)
	at Main.main(Main.java:16)


Reviewing the generated code:
    //...
    Token value=null;
    //...
    root_1 =
      (Object)adaptor.becomeRoot(adaptor.create(FLOAT, value), root_1);
The variable value has not been initialized between declaration and use.
Calling adaptor.create(FLOAT, null) causes the exception.

If I remove the rewrite rule, the exception disappears and the input is parsed ok.

Stefan





More information about the antlr-interest mailing list