[antlr-interest] custom error recovery

Christian Kihm christian.kihm at googlemail.com
Mon Dec 28 11:32:23 PST 2009


Hi,

I try to implement a logfile parser. Content of the logfile is a
sequense of games like this:


   PokerStars Game #35139121861: Hold'em No Limit ($0.05/$0.10 USD) -
2009/11/09 16:48:09 ET
   ...

   PokerStars Game #35139121865: Hold'em No Limit ($0.05/$0.10 USD) -
2009/11/09 16:48:45 ET
   ...


If there is an syntax error within a game, I would like to skip the
whole game and start parsing the next. To do this I disabled the
default recovery by overriding recoverFromMismatchedToken with:

   protected Object recoverFromMismatchedToken(IntStream input, int
ttype, BitSet follow)
       throws RecognitionException
   {
       throw new MismatchedTokenException(ttype, input);
   }


I also changed the default rule catch clause with this one:


@rulecatch {
 catch (RecognitionException e) {

   reportError(e);
   throw e;
 }
}

For the parser rule "game" I have my own catch clause wich in case of
an error will consume Until the Token SITE, "PokerStars" for the above
example. Here are the relevant parts of the grammar:


handhistory
        : ( game )*
        ;

game
       : game_info
         table
         hand
         summary
       ;
                            catch [RecognitionException re] {
                               reportError(re);
                               consumeUntil(input, SITE);
                            }

 game_info
       :  site
          primarykey
          COLON (cash | tournament)
          MINUS datetime
       ;


 site
      : SITE
      ;

...


Unfortunately it don't works as expected. In case of an invalid game
the parser goes into the game catch clause, but don't continue
parsing. To debug I also have override the consumeUntil Method:

public void consumeUntil(IntStream input, int tokenType) {
   super.consumeUntil(input, tokenType);

   System.out.println("### NEXT Token ### " + getCurrentInputSymbol(input));
 }


The output of the parser is:

line 61:59 [handhistory, game, game_info, tournament, buyin, rake,
recoverFromMismatchedToken] mismatched input
[@699,1923:1923='$',<98>,61:59] expecting RAKE
### GAME CATCH ###
### NEXT Token ### [@1350,3688:3702='PokerStars',<4>,123:0]


I am wondering why the parser stops parsing here. As you could see in
the output the next Token is as expectet "PokerStars". I would be very
happy for each hint.

best regards, Christian


More information about the antlr-interest mailing list