[antlr-interest] Error handling in lexer

Arthur Goldberg goldberg at cbio.mskcc.org
Tue Nov 30 13:15:07 PST 2010


Hi All

I've another question related to the same project. (I won't be bothering 
you much longer, it's due by the end of the week. :-)  I've got to move 
really quickly now.)

Typically, errors in ANTLR parsers are sent to stderr, via this 
emitErrorMessage:

00189  <http://www.antlr.org/api/Java/classorg_1_1antlr_1_1runtime_1_1_base_recognizer.html#85afd3931a36f93c5e1fee84349b5673>          public  void  displayRecognitionError  <http://www.antlr.org/api/Java/classorg_1_1antlr_1_1runtime_1_1_base_recognizer.html#85afd3931a36f93c5e1fee84349b5673>(String[] tokenNames,
00190RecognitionException  <http://www.antlr.org/api/Java/classorg_1_1antlr_1_1runtime_1_1_recognition_exception.html>  e)
00191         {
00192                 String hdr =getErrorHeader  <http://www.antlr.org/api/Java/classorg_1_1antlr_1_1runtime_1_1_base_recognizer.html#08b70239348297407c4322a00a46c77c>(e);
00193                 String msg =getErrorMessage  <http://www.antlr.org/api/Java/classorg_1_1antlr_1_1runtime_1_1_base_recognizer.html#f5adeec09d6d06b09bf18e2c14620093>(e, tokenNames);
00194emitErrorMessage  <http://www.antlr.org/api/Java/classorg_1_1antlr_1_1runtime_1_1_base_recognizer.html#20c71583ddc8a129bf81ef64a1fb4857>(hdr+""+msg);
00195         }


But my lang will be embedded in a Web site, so rather than outputting 
errors to stderr I want to report them by throwing exceptions, which are 
easy to handle.
In my AST-producing grammar, I report syntactic errors by modifying 
reportError() to throw an IllegalArgumentException:
>        public void reportError(RecognitionException e) {
>            // if we've already reported an error and have not matched 
> a token
>            // yet successfully, don't report any errors.
>            if ( state.errorRecovery ) {
>                    //System.err.print("[SPURIOUS] ");
>                    return;
>            }
>            state.syntaxErrors++; // don't count spurious
>            state.errorRecovery = true;
>            throw new IllegalArgumentException( 
> getRecognitionError(this.getTokenNames(), e) );
>        }
>
getRecognitionError returns a string similar to the string that would 
normally be printed by displayRecognitionError.

In my tree grammar, a production can simply throw IllegalArgumentException.

But how -- short of modifying *Lexer.java -- can I direct the Lexer to 
throw an Exception which my driver can catch?

Thanks
Deadline looming!
A

-- 
Senior Research Scientist
Computational Biology
Memorial Sloan-Kettering Cancer Center



More information about the antlr-interest mailing list