[antlr-interest] NullPointerException in TreeRewriter

Anders Hessellund anders.hessellund at gmail.com
Fri Mar 27 04:02:02 PDT 2009


Yes, you're right of course. As a client, however, I think that I'll just
call the 2-arg constructor straightaway with a new RecognizerSharedState()
as 2nd arg. This should make my code robust in the face of ANTLR-upgrades,
right?

Cheers,

Anders

On Thu, Mar 26, 2009 at 9:40 PM, Terence Parr <parrt at cs.usfca.edu> wrote:

> Doh!  HMm...not sure it should create new state. here is super(input) ctor:
>
>        public TreeParser(TreeNodeStream input) {
>                super(); // highlight that we go to super to set state
> object
>                setTreeNodeStream(input);
>        }
>
> that super() calls
>
>        public BaseRecognizer() {
>                state = new RecognizerSharedState();
>        }
>
> So I think state is set.  We need the adaptor ....
>
> Ok, TreeFilter/Rewriter need:
>
>    public TreeFilter(TreeNodeStream input) {
>        this(input, new RecognizerSharedState());
>    }
>
> to call other ctor :)  Try that.
>
> Ter
>
> On Mar 26, 2009, at 6:57 AM, Anders Hessellund wrote:
>
>  Hi,
>>
>> I found the cause. When you call the 1-arg constructor in the TreeRewriter
>> class, it does not set the TreeAdaptor:
>>
>>    public TreeRewriter(TreeNodeStream input) {
>>        super(input);
>>    }
>>    public TreeRewriter(TreeNodeStream input, RecognizerSharedState state)
>> {
>>        super(input, state);
>>        originalAdaptor = input.getTreeAdaptor();
>>        originalTokenStream = input.getTokenStream();
>>    }
>>
>> Shouldn't the 1-arg constructor rather be?
>>
>>    public TreeRewriter(TreeNodeStream input) {
>>        super(input,new RecognizerSharedState());
>>    }
>>    public TreeRewriter(TreeNodeStream input, RecognizerSharedState state)
>> {
>>        super(input, state);
>>        originalAdaptor = input.getTreeAdaptor();
>>        originalTokenStream = input.getTokenStream();
>>    }
>>
>> Cheers,
>>
>> Anders
>>
>> On Thu, Mar 26, 2009 at 9:39 AM, Anders Hessellund <
>> anders.hessellund at gmail.com> wrote:
>> Hi,
>>
>> You're right I did not set a tree adaptor. I was actually trying the
>> example from the wiki but there is no mention of a tree adaptor there?
>>
>>  http://www.antlr.org/wiki/display/~admin/2008/11/30/Example+tree<http://www.antlr.org/wiki/display/%7Eadmin/2008/11/30/Example+tree>
>> +rewriting+with+patterns
>>
>> Any ideas?
>>
>> -- Anders
>>
>>
>> On Wed, Mar 25, 2009 at 9:20 PM, Terence Parr <parrt at cs.usfca.edu> wrote:
>> weird. oh, if you look at code it thinks the adaptor is null...maybe you
>> set it incorrectly or don't have one on the nodestream or reducer?
>> Ter
>>
>> On Mar 25, 2009, at 12:10 PM, Anders Hessellund wrote:
>>
>> Hi,
>>
>> I just updated to 3.1.3 to try TreeRewriter but I get a strange
>> NullPointerException?
>>
>> This is my tree grammar:
>>
>> tree grammar Reducer;
>> options {
>>   tokenVocab=Expression;
>>   ASTLabelType=CommonTree;
>>   output=AST;
>>   filter=true;
>> }
>> topdown
>>   :    ^(MULT a=. b=INT {$b.int==0}?) -> $b // x*0 -> 0
>>   ;
>> bottomup
>>   :    ^(DIV a=. b=INT {$b.int==0}?) -> $b // x*0 -> 0
>>   ;
>>
>> I use the following code to invoke the Reducer. (tree is the CommonTree
>> object from my parse)
>>
>>   final CommonTreeNodeStream nodes = new CommonTreeNodeStream(tree);
>>   final McAntlrExpressionReducer reducer = new
>> McAntlrExpressionReducer(nodes,env);
>>   tree = (CommonTree) reducer.downup(tree);
>>
>> When I run this on the following expression "3*0" I get the following
>> error:
>>
>> 20:09:08.226 [main] DEBUG expression.Expression - parsing '3*0' results in
>> this AST: (* 3 0)
>> 20:09:08.226 [main] DEBUG expression.Expression - before reduction: (* 3
>> 0)
>> java.lang.NullPointerException
>>   at
>> org.antlr.runtime.tree.CommonTreeNodeStream.<init>(CommonTreeNodeStream.java:67)
>>   at org.antlr.runtime.tree.TreeRewriter.applyOnce(TreeRewriter.java:56)
>>   at org.antlr.runtime.tree.TreeRewriter$1.pre(TreeRewriter.java:86)
>>   at org.antlr.runtime.tree.TreeVisitor.visit(TreeVisitor.java:28)
>>   at org.antlr.runtime.tree.TreeRewriter.downup(TreeRewriter.java:89)
>>   at
>> com.maconomy.expression.McAbstractExpression.reduce(McAbstractExpression.java:58)
>>   at com.maconomy.expression.test.MtReducerTest.add(MtReducerTest.java:27)
>>   at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
>>   at
>> sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
>>   at
>> sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
>>   at java.lang.reflect.Method.invoke(Method.java:597)
>>   at
>> org.junit.internal.runners.TestMethodRunner.executeMethodBody(TestMethodRunner.java:99)
>>   at
>> org.junit.internal.runners.TestMethodRunner.runUnprotected(TestMethodRunner.java:81)
>>   at
>> org.junit.internal.runners.BeforeAndAfterRunner.runProtected(BeforeAndAfterRunner.java:34)
>>   at
>> org.junit.internal.runners.TestMethodRunner.runMethod(TestMethodRunner.java:75)
>>   at
>> org.junit.internal.runners.TestMethodRunner.run(TestMethodRunner.java:45)
>>   at
>> org.junit.internal.runners.TestClassMethodsRunner.invokeTestMethod(TestClassMethodsRunner.java:66)
>>   at
>> org.junit.internal.runners.TestClassMethodsRunner.run(TestClassMethodsRunner.java:35)
>>   at
>> org.junit.internal.runners.TestClassRunner$1.runUnprotected(TestClassRunner.java:42)
>>   at
>> org.junit.internal.runners.BeforeAndAfterRunner.runProtected(BeforeAndAfterRunner.java:34)
>>   at
>> org.junit.internal.runners.TestClassRunner.run(TestClassRunner.java:52)
>>   at
>> org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:45)
>>   at
>> org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
>>   at
>> org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:460)
>>   at
>> org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:673)
>>   at
>> org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:386)
>>   at
>> org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:196)
>>
>> Can anyone help me with this?
>>
>> -- Anders
>>
>> List: http://www.antlr.org/mailman/listinfo/antlr-interest
>> Unsubscribe:
>> http://www.antlr.org/mailman/options/antlr-interest/your-email-address
>>
>>
>>
>>
>> --
>> Anders Hessellund
>> www.itu.dk/people/hessellund/
>>
>>
>>
>> --
>> Anders Hessellund
>> www.itu.dk/people/hessellund/
>>
>
>


-- 
Anders Hessellund
www.itu.dk/people/hessellund/
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.antlr.org/pipermail/antlr-interest/attachments/20090327/bc85d781/attachment.html 


More information about the antlr-interest mailing list