[antlr-interest] Parser stops, returns partial tree sometimes correctly so.

Gavin Lambert antlr at mirality.co.nz
Tue Oct 16 23:41:58 PDT 2007


At 11:10 17/10/2007, Ted Villalba wrote:
>I have created a parser that stops parsing my input string and 
>returns an AST, when instead, I expected it to throw a 
>recognition exception.

If your toplevel rule doesn't end by trying to match EOF, then 
ANTLR will assume it doesn't need to consume the entire input 
stream and will just return whatever it can match.  If you want it 
to consume the whole stream or produce an error, then put in an 
EOF.

>Testing in ANTLRWorks I get a NullPointerException, i think 
>because of some code that sets the token type.
>
>boolTerm : b=BOOL_OP|WOK_OP { $b.setType(WCHAR); }  ;

The problem here is that "|" has the lowest precedence.  This 
means that if you have a BOOL_OP the code will not be run at all, 
and if you have a WOK_OP then "b" is not assigned and you'll get a 
NullReferenceException.  Change it to the following and it should 
do what you want:

boolTerm : b=(BOOL_OP|WOK_OP) { $b.setType(WCHAR); } ;



More information about the antlr-interest mailing list