[antlr-interest] Reporting errors from rules

Florian Weimer fw at deneb.enyo.de
Tue Sep 2 11:06:12 PDT 2008


* Terence Parr:

> On Sep 2, 2008, at 3:54 AM, Florian Weimer 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.
>
> The problem is that ANTLR Is controlling what tokens are attempted. If
> the input is not consistent with IPV4_ADDRESS, ANTLR will never call
> that rule. Hence you cannot cause an error is there.

Sorry, I don't follow.  IPV4_ADDRESS matches "1....2", but the
IPv4Address constructor throws an exception for that.  I want to turn it
into something which is reported through the regular framework.


More information about the antlr-interest mailing list