[antlr-interest] How catch exception when "extraneous input" happens?

Jim Idle jimi at temporal-wave.com
Thu Jun 10 09:58:10 PDT 2010



> -----Original Message-----
> From: antlr-interest-bounces at antlr.org [mailto:antlr-interest-
> bounces at antlr.org] On Behalf Of BiGNoRm6969
> Sent: Thursday, June 10, 2010 7:09 AM
> To: antlr-interest at antlr.org
> Subject: [antlr-interest] How catch exception when "extraneous input"
> happens?
> 
> 
> During a string validation, I receive this message "line 1:20
> extraneous
> input 'f' expecting ASA" in the console. This is printed by the
> "getErrorMessage" function, initiated by the "match" function. I
> understanrd
> why this message is displayed.
> 
> My problem is that I want to catch the error generated by the parser. I
> include this code in my .g:
> 
> @rulecatch {
>        catch ( RecognitionException testing ) {
>            throw testing;
>        }
>   }
> 
> I also include a try/catch block in my Java code that use my parser, to
> catch the RecognitionException exception. However, it never catch it.
> So, my
> program is unable to know if the validation succeed or not.
> 
> What I am  missing?


When the parser recovery code discovers that it can 'correct' the input stream by either inserting a token it manufactures, or skipping an extra token, then it issues the error message as normal but it does not throw an exception because it can just carry on. So can override the recover methods and force a throw if you need that, but if you are just wanting to print your own message, then you just override displayRecognitionError() 

Jim





More information about the antlr-interest mailing list