[antlr-interest] custom error recovery
Jim Idle
jimi at temporal-wave.com
Mon Dec 28 12:04:34 PST 2009
Firstly you probably want:
handhistory: game* EOF ;
Then, did you read the wiki article:
http://www.antlr.org/wiki/display/ANTLR3/Custom+Syntax+Error+Recovery
You probably need to have catch and/or resync on the other rules in your game parsing productions.
Jim
> -----Original Message-----
> From: antlr-interest-bounces at antlr.org [mailto:antlr-interest-
> bounces at antlr.org] On Behalf Of Christian Kihm
> Sent: Monday, December 28, 2009 11:32 AM
> To: antlr-interest at antlr.org
> Subject: [antlr-interest] custom error recovery
>
> 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
>
> List: http://www.antlr.org/mailman/listinfo/antlr-interest
> Unsubscribe: http://www.antlr.org/mailman/options/antlr-interest/your-
> email-address
More information about the antlr-interest
mailing list