[antlr-interest] NullPointerException in rewrite rule

Stefan Eder stefan.eder at ebuconnect.de
Wed Dec 19 14:28:52 PST 2007


I figured it out:
floatSequence returns [String sequence]
    :
    v1=NumberLiteral '.' v2=NumberLiteral
    {
        $sequence = $v1.text + '.' + $v2.text;
    }
    -> ^(FLOAT[$sequence])
    ;
This way it works because $sequence is writable available in actions and 
readable in rewrite rules.

You brought me on the even more easier way that can solve even more 
difficult situations, because $text seems to be an attribute of the rule 
representing the text of the all matched tokens. You get an error if you 
try to assign a value to $text within an action.

So:
identifier returns [String name]
    :
    (NumberLiteral|LetterLiteral)+
    {
        $name=$text;
    }
    -> ^(ID[$text])
    ;
or
floatSequence returns [String sequence]
    :
    NumberLiteral '.' NumberLiteral
    {
        $sequence = $text;
    }
    -> ^(FLOAT[$sequence])
    ;

brings exactly what I want, one single token with the text of the 
complete subtree as text.
:~)

Yes, what I am going to parse has identifiers that may start with digits.

Stefan

Jim Idle schrieb:
> 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
>>
>>
>>
>>     
>
>
>
>   
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.antlr.org/pipermail/antlr-interest/attachments/20071219/24841bee/attachment.html 


More information about the antlr-interest mailing list