[antlr-interest] How to redirect all of antlr parser's error messages without changing recovery.

Mark Volkmann r.mark.volkmann at gmail.com
Tue Feb 5 05:27:13 PST 2008


On Feb 5, 2008 1:34 AM, Neville Clark <antlr at zavalon.com> wrote:
> Hi all,
> I am new to both this list and to antlr.
> I have to questions
> 1. How to override all of antlr's parser errors but without changing
> antlr's recovery. I am running as an Eclipse plug-in, and I want to use
> "problem marker" instead of "System.err".
> Overriding "reportError" catches most of the output. But had to also
> override "recoverFromMismatchedToken" and "mismatch" as per below
> @Override
> public void recoverFromMismatchedToken(IntStream input,
>
> RecognitionException mte,
>                                                                    int
> ttype,
>
> BitSet follow) throws RecognitionException
> {
>   //System.err.println("BR.recoverFromMismatchedToken");    <<<=== I
> removed this line
>   // if next token is what we are looking for then "delete" this token
>   if ( input.LA(2)==ttype )
>   {
>       reportError(mte);
>       /*
>       System.err.println("recoverFromMismatchedToken deleting
> "+input.LT(1)+
>                                    " since "+input.LT(2)+" is what we
> want");
>      */
>       beginResync();
>       input.consume(); // simply delete extra token
>      endResync();
>      input.consume(); // move past ttype token as if all were ok
>      return;
>   }
>   if ( !recoverFromMismatchedElement(input,mte,follow) )
>   {
>        throw mte;
>   }
> }
>
> As indicated in above had to override and reproduce the same
> functionality except for the print statement.
> Also required to override "mismatch" and reproduce it's code without change
> Is there a better way.

I think you just need to override the emitErrorMessage method in
BaseRecognizer which is the superclass of Lexer, Parser and
TreeParser. Make sure you do it for all three (if you are using all
three). You can have one @members section for each grammar type. You
just need to add a grammar type prefix to @members like this.

@lexer::members {
  ...
}

@parser::members {
  ...
}

@treeparser::members {
  ...
}

> 2nd Question
> In a combined grammar is it possible to "superClass" the Lexer as well
> as the Parser. I have added
> options
> {
>    superClass=MyParserSuperClass;
> }
> which works for the parser. But can not find same for the Lexer (within
> a combined grammar).
> I have not tried but am assuming I can superClass the Lexer if I
> separate the Parser and Lexer.

I think you do have to separate your grammar in order to specify a
custom superclass for both the generated lexer and the generated
parser since superClass is a grammar option.

-- 
R. Mark Volkmann
Object Computing, Inc.


More information about the antlr-interest mailing list