[antlr-interest] BaseTreeAdaptor.becomeRoot throws NullPointerException

Alessandro alessnet at gmail.com
Tue Jun 5 12:33:44 PDT 2007


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