[antlr-interest] NullPointerException in rewrite rule

Stefan Eder stefan.eder at ebuconnect.de
Wed Dec 19 11:34:01 PST 2007


sometimes I need something like 47.11 as three tokens (NumberLiteral '.' 
NumberLiteral).
So, as far as I understood, I need to make a parser rule to match a REAL 
if such a match is in scope rather than a lexer rule.
On the other hand it would sometimes be pretty convenient to merge some 
tokens.
An within the "language" that I need to parse it is even worse, numbers 
may be identifiers...
:~(
I have an identifier rule that matches several tokens and it would make 
the AST much flatter and easier to work on, if I would find a way to do 
what I am trying to do

Mark Volkmann schrieb:
> On Dec 19, 2007 5:41 AM, Stefan Eder <stefan.eder at ebuconnect.de> wrote:
>   
>> 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))
>>     
>
> I don't know if this is related to the problem, but why don't you make
> a lexer rule that creates a single token for your floating point
> numbers? For example,
>
> REAL: INTEGER+ ('.' INTEGER+)?;
> INTEGER: '0'..'9';
>
> If you had those then your rule above could be written like this.
>
> floatSequence: REAL -> ^(FLOAT REAL);
>
>   
>>                 -> ^(FLOAT[$value])
>>     
>
> Why do you have the square brackets? I don't think you mean to pass
> $value as a parameter to the FLOAT rule.
>
>   
>>                 ;
>>         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 Eder
Senior Software Architekt / Senior Consultant
	

Phone:	+49 2137 934132
Office: +49 2181 2363-0
Fax:	+49 2181 2363-43
Mobile: +49 172 6050 486

Web: www.ebuconnect.de
Mail: stefan.eder at ebuconnect.de

Sitz der Gesellschaft: ebuconnect AG, Stadttor 1, D-40219 Düsseldorf 
Amtsgericht Düsseldorf: HRB 40219
Vorstand: Andreas Weiss - Vorsitzender
Vorsitzender des Aufsichtsrates: Dr. Marc Henze

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.antlr.org/pipermail/antlr-interest/attachments/20071219/d373927e/attachment.html 


More information about the antlr-interest mailing list