How does one recover from an error in ANTLR ?!_NEWLINE_!The ...
1 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   Max_Andersen
Posted On:   Wednesday, January 3, 2001 07:58 AM

How does one recover from an error in ANTLR ?
The documentation mentions how to catch exceptions, but not how to recover from the error and continue from a given rule or e.g. just to "fast-forward" to the next ";"-token to skip errornous lines in e.g. a Java parser ?

I know the default mechanism does this of some sort, but I would like to find the "official" API for handling it my own error/recovery methods.

Re: How does one recover from an error in ANTLR?

Posted By:   Matthew_Daniel  
Posted On:   Thursday, March 29, 2001 01:13 PM

Look at the javadoc for Parser.consumeUntil (both varieties). For example, you can add to the examples/java/includeFile/P.g in 2.7.1:

decl: ...
;
exception
catch [RecognitionException re] {
System.err.println(re.toString());
consumeUntil(SEMI);
}


and it will behave very much like the standard ANTLR error trap (consuming until end of rule). If you know of a better set of tokens to consume until, then you may specify them instead. Of course, the more you separate the rules of your grammar, the more granularity you have over catching exceptions.
About | Sitemap | Contact