[antlr-interest] NullPointerException in rewrite rule

Jim Idle jimi at temporal-wave.com
Wed Dec 19 08:46:27 PST 2007


Well, value isnt going to track quite what you want here I think :-)

Assuming that you need this in the parser (this is quite often 
legitimate) and not the lexer, then:

@init
{
	String text;
}
v1=NumberLiteral '.' v2=NumberLiteral

{
	$text = $v1.text '.' $v2.text;
}
	-> ^FLOAT[$text]

Or something similar.

Jim  

> -----Original Message-----
> From: Stefan Eder [mailto:stefan.eder at ebuconnect.de]
> Sent: Wednesday, December 19, 2007 3:42 AM
> To: antlr-interest at antlr.org
> Subject: [antlr-interest] NullPointerException in rewrite rule
> 
> 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.j
av
> a: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