[antlr-interest] Parser Generation: How to catch an exception within an rule?

luciano mantuaneli mantu_lists at yahoo.com.br
Mon Mar 12 06:43:35 PDT 2007


Sorry to bother again, but it just don't work... I still having the same problem...


importDefinition
    options {defaultErrorHandler = true;}
    { boolean isStatic = false; String imp = "";}
    :    
        i:"import"^ {#i.setType(IMPORT);} 
        ( "static"! {#i.setType(STATIC_IMPORT);} )? 
        imp=identifierStar SEMI! 
    ;
    exception
        catch[StarImportException exp]{
            syso("Error: Import star found");
        }

Genarates, agian, the folowing parser code:


catch (StarImportException exp) {
    if (inputState.guessing==0) {
        syso("Error: Import star found");
    } else {
        throw exp;
    }
}

Is it relevant the fact I'm using ANTLR 2.7.7 ?


Johannes Luber <jaluber at gmx.de> escreveu: luciano mantuaneli wrote:
> Thanks for answering, Luber. But i guess I don't figure out where is the
> insertion point of the code you mentioned... I changed my rule to this:

The insertion point is after the semicolon. You added the catch handler
inside the rule, not enclosing it. Here is the corrected code:

importDefinition
    options {defaultErrorHandler = true;}
    { boolean isStatic = false; String imp = "";}
    :
        i:"import"^ {#i.setType(IMPORT);}
        ( "static"! {#i.setType(STATIC_IMPORT);} )?
        imp=identifierStar SEMI!
    ;
    exception
    catch[StarImportException exp]{
        syso("Error: Import star found");
    }

Best regards,
Johannes Luber



Luciano Mantuaneli
Analist-Programmer
 __________________________________________________
Fale com seus amigos  de graça com o novo Yahoo! Messenger 
http://br.messenger.yahoo.com/ 
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.antlr.org/pipermail/antlr-interest/attachments/20070312/c74a002c/attachment.html 


More information about the antlr-interest mailing list