[antlr-interest] Error handling problem

Andrey Timoshenkov tas at ultersys.ru
Fri Jun 23 04:49:32 PDT 2006


Michiel,

thank you for your suggestion. I tried something like this, but 
unfortunately this causes nondeterminism errors to be generated. I guess 
the illegalStatement rule should be non-empty to avoid nondeterminism. 
Probably I need something like "any-token-not-in-the-lookahead-set" but 
I have no idea how to write such a rule.

Thank you,
Andrey Timosenkov

Michiel Vermandel wrote:
> 
> Hi,
> 
> I don't know if this is the best way but I solved this issue as follows:
> 
> spec:
> ( statement
>    exception
>    catch[RecognitionException er]
>    {
>       // report, consume tokens until the next statement
>    }
>   | illegalStatement
> )* EOF ;
> 
> illegalStatement:
> {
>    // report, consume tokens until the next statement
> }
> 
> so the illegalStatement rule takes anything that is not taken by the 
> statement rule
> 
> I hope this is of any help...
> 
> 
> 
> 
> 
> 
> *Andrey Timoshenkov <tas at ultersys.ru>*
> Sent by: antlr-interest-bounces at antlr.org
> 
> 23/06/2006 13:29
> 
> 	
> To
> 	ANTLR malilist <antlr-interest at antlr.org>
> cc
> 	
> Subject
> 	[antlr-interest] Error handling problem
> 
> 
> 	
> 
> 
> 
> 
> 
> 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