[antlr-interest] BaseTreeAdaptor.becomeRoot throws NullPointerException

Alessandro alessnet at gmail.com
Fri Jun 8 03:23:39 PDT 2007


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