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

Johannes Luber jaluber at gmx.de
Mon Mar 12 07:06:57 PDT 2007


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



More information about the antlr-interest mailing list