[antlr-interest] How to force ANTLR 3.1.2 to raise exceptions on invalid tokens?

Jim Idle jimi at temporal-wave.com
Thu Jun 4 08:34:54 PDT 2009


Liviu U wrote:
> I notices something strange in 3.1.2
>
> If i have a rule:  var x = int; and another that matches: if cond then
> statements end
>
> for a script:
>
> if var x=3 ....
>
> ANTLR ignores the if and parses an assignment. This is not what i
> expect. I need to have an error raised.
>
> Is there a way to configure the behaviour i expect? I remember that in
> older versions the behavior of the parser was different...
>
> List: http://www.antlr.org/mailman/listinfo/antlr-interest
> Unsubscribe: http://www.antlr.org/mailman/options/antlr-interest/your-email-address
>   
You should use 3.1.3 really, but this sounds more like something has 
changed/gone awry with your grammar. If you are not getting an error on 
the if (as in extraneous "'if" at ....), then there must be a path from 
if to assignment/declarations. I don't think that anyone can help 
without you posting your grammar as there is not enough information 
here; both 3.1.2 and 3.1.3 will raise errors.

However, perhaps you mean that you are getting the extraneous token 
message but are not getting an exception. If you do, then this is 
expected behavior as ANTLR recovers from that error so gives you the 
call to display the error, but does not raise an exception. If you look 
at the comments in the source code for the recovery routines, they tell 
you how to get the old behavior, where you would get an exception and 
ANTLR will not try to work out if you have an extra or missing token. If 
you are getting this, then likely your statement lists are not including 
the if statement and therefor ANTLR views it as extraneous, skips it, 
and then, seeing that var is in the followset, resyncs to that, which is 
the declaration/assignment rule in your parser.

Jim


More information about the antlr-interest mailing list