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

Ron Hunter-Duvar ron.hunter-duvar at oracle.com
Thu Jun 10 09:31:22 PDT 2010


Yes, the problem is that you have it in a try-catch, so when you throw 
it you just catch it again. But unfortunately displayRecognitionError is 
not defined with "throws RecognitionException" in BaseRecognizer, so if 
you remove the try-catch you'll get a compile error (which I'm guessing 
is why you added it). If you don't need an exception thrown, you could 
just set a flag that gets tested later (actually, Antlr already keeps a 
count of the number of syntax errors that you can check).

If you really need the exception thrown, then you need to overrride a 
different method. Inside the match method, if the match fails and it's 
not in the middle of backtracking, it calls recoverFromMismatchedToken. 
This method tries to recover by either inserting a single missing token 
or deleting tokens until a token in the follow set is found. If it 
succeeds in one of these, it reports the error but suppresses the 
exception and continues parsing. If you always want the exception thrown 
you need to override recoverFromMismatchedToken.

Ron


Normand Bédard wrote:
> I tried to override the displayRecognitionError to manually throw an 
> exception that will be catch in my java code using my parser:
>
> In my .g:
>
> @members {
>     public void displayRecognitionError(String[] tokenNames,
>                                         RecognitionException e) {
>         String hdr = getErrorHeader(e);
>         String msg = getErrorMessage(e, tokenNames);
>
>         // Now do something with hdr and msg...
>         emitErrorMessage(hdr+" "+msg);
>
>         try {
>         throw e;
>
>       } catch (RecognitionException e1) {
>         // TODO Auto-generated catch block
>         System.err.println("testing :P");
>       }
>     }
> }
>
>
> In the .java that use my parser I have:
>
> try
>         {
>             parser.evaluation_13_15();
>             System.out.println("flag 001 test");
>
>
>         }
>         catch (RecognitionException test)
>         {
>             System.out.println("flag 002 test");
>         }
>
>
>
> When I manually throw the exception in the overriden fonction, it is 
> catched by the try catch surrounding the throw call (of course :P). Is 
> there a way to throw an exception outside a try/catch (I am little 
> experienced with Java..) ? Or it is my approch that is problematic?
>
> Thanks!
>
> Normand
>
>
>
>
> On 10-06-10 10:55 AM, Ron Hunter-Duvar wrote:
>> BiGNoRm6969 wrote:
>>> 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?
>>>
>>>
>> Try overriding either the emitErrorMessage or the 
>> displayRecognitionError method in org.antlr.runtime.BaseRecognizer. 
>> You can do that with an @member section in your grammar, or in a 
>> separate superclass.
>>
>> Ron
>>
>

-- 
Ron Hunter-Duvar | Software Developer V | 403-272-6580
Oracle Service Engineering
Gulf Canada Square 401 - 9th Avenue S.W., Calgary, AB, Canada T2P 3C5

All opinions expressed here are mine, and do not necessarily represent
those of my employer.



More information about the antlr-interest mailing list