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

luciano mantuaneli mantu_lists at yahoo.com.br
Mon Mar 12 10:40:42 PDT 2007


Well, I don't knew (and still don't) if the fact of my StarImportException do not was an RecognitionException (Or any other ANTLR exception) matters to this problem. But, as a desperate mesure, I made StarImportException extends RecognitionException, and the problem simply vanished. I don't know if that is the correct procedure about exception handling in antlr programming or if it is a shabby workaround, but, at least, it works for the time being... Please, let me know if it was a workaround or not, ok?

Thank you all, specially Luber.

My best regards!
Mantu

luciano mantuaneli <mantu_lists at yahoo.com.br> escreveu: I was looking in that documentation too, and also finds your tip clearly apropriated. Thats why I'm pretty confused... 
Abou the possible other syntax problem, iguess it's not a problem. The point is that identifierStar rule returns a String object. Then, It's legal to use "imp=importStar" in order to retrieve the returned String from importStar rule. If I lable that rule with "imp", that variable must be an AST object.
Further, i found that the "imp" variable have no impact in the exception handling problem. I rewrote the rule like this:

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

And the translation is still re-throwing my exception...

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

Now i'm being pretty worried about that problem...

Johannes Luber <jaluber at gmx.de>   escreveu: luciano mantuaneli wrote:
> 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 ?

While I have used the PDF for ANTLR 3, the following link about ANTLR
2.7
http://www.doc.ic.ac.uk/lab/secondyear/Antlr/err.html#SpecifyingParserException-Handlers
doesn't seem to contradict my chosen syntax. So I took a closer look and
discovered possibly another syntax error. You wrote "imp=identifierStar"
- if I read the old ANTLR syntax right, it has to be
"imp:identifierStar", if imp is supposed to be label. But as imp is
defined as a String, maybe you forgot to assign the variable within an
action itself. Otherwise I don't know what went wrong.

Best regards,
Johannes Luber




Luciano Mantuaneli
Analist-Programmer

Luciano Mantuaneli
Analist-Programmer __________________________________________________
Fale com seus amigos  de graça com o novo Yahoo! Messenger  
http://br.messenger.yahoo.com/ 



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/a22bbd00/attachment.html 


More information about the antlr-interest mailing list