[antlr-interest] Customizing errors in antlr3

James Briant jab.antlr at species.org
Sun Jan 7 09:33:12 PST 2007


Seems like, from the point of view of your grammar, that "[foo] class bar;"
is valid, i.e. recognizable, and only then do you want to tell the user that
its invalid.

On 1/7/07, Benoit Miller <fulg at iname.com> wrote:
>
> What is the best way to customize reported errors in antlr3?
>
> For example, in the following snippet:
>
> start
>      :   forwardDecl* objectDecl*
>      ;
>
> forwardDecl
>      :   classOrStruct IDENTIFIER ';'
>          -> ^(classOrStruct IDENTIFIER)
>      ;
>
> objectDecl
>      :   attrList? classOrStruct IDENTIFIER '{' property* '}' ';'
>          -> ^(classOrStruct IDENTIFIER attrList? property*)
>      ;
>
> [unedited snippet, so this time it should be representative :)]
>
> Given an input such as "[foo] class Bar;", I want to report to the tool
> user something like "cannot specify attributes on forward declarations",
> but it's unclear to me how to do this.
>
> I tried using a syntactic predicate to throw my error from the parser:
>
> objectDecl
>      :   attrList? classOrStruct IDENTIFIER
>          ( {input.LA(1)==';'}? { throw new Exception("foo"); }
>          | '{' property* '}' ';'
>            -> ^(classOrStruct IDENTIFIER attrList? property*)
>          )
>      ;
>
> ..but that didn't work, the parser still throws a NoViableAlt exception
> instead of my own:
>
> [objectDecl]: line 1:20  state 0 (decision=5) no viable alt;
> token=[@7,20:20=';',<30>,1:20]
>
> Any suggestions?
>
> Thanks,
> Ben.
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.antlr.org/pipermail/antlr-interest/attachments/20070107/e9462608/attachment.html 


More information about the antlr-interest mailing list