[antlr-interest] Reporting errors from rules

Thomas Brandon tbrandonau at gmail.com
Tue Sep 2 11:35:32 PDT 2008


On Tue, Sep 2, 2008 at 8:54 PM, Florian Weimer <fw at deneb.enyo.de> wrote:
> I'd like to report errors from rules because I want to reuse existing,
> non-ANTLR parsers and make the lexer rules a bit more lenient than
> what's actually in the language.  For instance, in
>
> IPV4_ADDRESS : ('0' .. '9' | '.')+;
>
> address returns [IPv4Address address]
>    : IPV4_ADDRESS
>        {
>            try {
>                $address = new IPv4Address($IPV4_ADDRESS.text);
>            } catch (IllegalArgumentException e) {
>                ???
>            }
>        };
>
> I want to report an error at the location of the IPV4_ADDRESS token if
> the token is not actually an IPv4 address.
>
> Do I have to throw a special exception to achieve this?
>
> List: http://www.antlr.org/mailman/listinfo/antlr-interest
> Unsubscribe: http://www.antlr.org/mailman/options/antlr-interest/your-email-address
>
>
You can throw a subclass of RecognitionException. Then ANTLR's error
reporting should return it's text or you can override
BaseRecognizer.getErrorMessage to provide text. However you will
likely quickly find ANTLRs error reporting mechanisms to be inadequate
to a final application and add your own that you send ANTLR errors to.
So, it may be best to just send your error straight to your own error
handling facilities and save sending it through ANTLRs error reporting
mechanisms only to send it onto your own.

Tom.


More information about the antlr-interest mailing list