[antlr-interest] Error handling problem

Andrey Timoshenkov tas at ultersys.ru
Fri Jun 23 04:29:35 PDT 2006


Hi all.

I have a problem with parsing error handling in ANTLR 2.7.5.

Consider the simple grammar:

spec: (statement)* EOF;
statement: statement1 |	statement2 ;

My goal is to handle syntax errors in each statement so that I can 
report an error, drop the incorrect statement and try to continue from 
the next one. I do the following:

spec:
( statement
    exception
    catch[RecognitionException er]
    {
       // report, consume tokens until the next statement
    }
)* EOF ;

This works fine for any errors inside the statement itself, but if the 
very first token of the statement is not recognized, the exception is 
not handled. Instead the loop generated for the rule exits and parser 
tries to match EOF.

How I can handle this situation to be able to recover the parsing process?

And one more question: can I refer the rule lookahead set inside action 
block to be able to write:
	consumeUntil( <ruleLookahedSet> );
in the above catch block to adjust token stream to the next valid statement?

Thank you very much,
Andrey Timoshenkov


More information about the antlr-interest mailing list