[antlr-interest] BaseTreeAdaptor.becomeRoot throwsNullPointerException

Jim Idle jimi at temporal-wave.com
Fri Jun 8 08:04:27 PDT 2007


Hmm - debatable whether you should get the crash, which I presume is null exception on atom10 after an NVE? 

However, I suspect that what is happening is that you are generating a default tree or have not covered the case, so you can either 'fix' this or work around it, depending on whether this is really a bug or not by looking for it. We would need to see your grammar to see exactly why it causes this, but for the moment, let's presume that atom10 can somehow be null and you still want to generate a tree, then:

r1: e1=r2 (PLUS e2=r2)
	-> {$e2 != null}? ^(PLUS e1 e2)
	-> e1
;

Or something similar, more apropos to your purpose should get you around that. 

However it looks a bit suspicious that it is trying to generate a tree on an error, unless this is just cascading up the expression tree and by the time you try to use it... etc.

Jim



> -----Original Message-----
> From: antlr-interest-bounces at antlr.org [mailto:antlr-interest-
> bounces at antlr.org] On Behalf Of Alessandro
> Sent: Friday, June 08, 2007 3:24 AM
> To: antlr-interest at antlr.org
> Subject: Re: [antlr-interest] BaseTreeAdaptor.becomeRoot
> throwsNullPointerException
> 
> Hello,
> 
> Does nobody really know if this is a "normal" behavior ?
> 
> This is how I call the parser :
> 
> String text = "var = \r\n";
> 
> ANTLRStringStream input = new
> ANTLRStringStream(text.toCharArray(),text.length());
> KixExempleDebugLexer lexer = new KixExempleDebugLexer(input);
> CommonTokenStream tokens = new CommonTokenStream(lexer);
> KixExempleDebugParser parser = new KixExempleDebugParser(tokens);
> parser.prog();
> 
> This is where the code crash, in "public final multExpr_return
> multExpr()" :
> 
> root_0 = (Object)adaptor.nil();
> 
> pushFollow(FOLLOW_atom_in_multExpr249);
> atom10=atom(); // atom procedure doesn't create any AST because there
> is not match
> _fsp--;
> root_0 = (Object)adaptor.becomeRoot(atom10.getTree(), root_0); //
> throws "java.lang.NullPointerException" because atom10.getTree() =
> null
> 
> I didn't change any generated source code. Do I have to put my own
> TreeAdaptor to prevent this error from occurring ?
> 
> Thanks
> 
> 
> On 6/5/07, Alessandro <alessnet at gmail.com> wrote:
> > Hello,
> >
> > I'm learning to use ANTLR thank to examples.
> > There is no error when I try to parse a syntactically correct text
> > with my genereted parser.
> > But when I give a specific erroneus text to my parser, there is an
> > exception when creating the AST.
> >
> > adaptor.becomeRoot(atom10.getTree(), root_0);
> > throws "NullPointerException" because of failing rule
> > (NoViableAltException). This failing rule returns no AST, thus
> > atom10.getTree() == null.
> >
> > Is this normal ?
> > Thanks :-)
> >
> > My input text : "var ="
> > Here is my grammar (with -debug option) :
> >
> >
> > options {output=AST; }
> >
> > tokens
> > {
> >         PROG_DEC='PROG_DEC';
> >         ASSIGN='ASSIGN';
> >         EXRP = 'EXPR';
> >         MULEXPR= 'MULEXPR';
> > }
> >
> >
> > prog:   stat+
> >         -> ^(PROG_DEC stat*);
> >
> > stat:   expr NEWLINE {  /*expression*/ } -> ^(EXRP expr)
> >     |   ID EQUAL expr NEWLINE { /* assignation matchée*/ } ->
> ^(ASSIGN
> > ID ^(EXRP expr))
> >
> >     |   NEWLINE!
> >     ;
> >
> > expr
> >     :   (multExpr { /* multExpr */ } -> ^(MULEXPR multExpr))
> >         ( op=OP
> >                 s1=multExpr {/* +multExpr*/ }
> >         -> ^($op $expr ^(MULEXPR $s1))
> >         )*
> >     ;
> >
> > multExpr
> >     :   atom^ {/*  atom ... */ } ('*'^ atom {/* * atom */ })*
> >     ;
> >
> > atom
> >     :   INT^ {/* INT */}
> >     |   ID^  {/* ID */}
> >     |   PL expr PR {/* (expr) */} -> ^(EXRP expr)
> >     ;
> >
> > ID  :   ('a'..'z'|'A'..'Z')+ ;
> > INT :   '0'..'9'+ ;
> > NEWLINE!:'\r'? '\n' ;
> > WS!  :   (' '|'\t')+ {skip();} ;
> > OP      :       '+' | '-';
> > PL      :       '(' ;
> > PR      :       ')' ;
> > EQUAL   :       '=' ;
> >


More information about the antlr-interest mailing list