[antlr-interest] How to remove this kind of warning

Gavin Lambert antlr at mirality.co.nz
Fri Sep 5 17:31:43 PDT 2008


At 04:01 6/09/2008, Edwards, Waverly wrote:
>I don't think I understand this.  Shouldn't IP address be defined 
>as
>
>IPFRAG '.' IPFRAG '.' IPFRAG ''.' IPFRAG
>
>where IPFRAG is fragment constrained to a maximum 3 digit integer 
>
>You could later check to see if that value is 255 or less.

As I said earlier, you can define it that way if it makes you 
happier, but there's no need to.  If the IP_Address rule is a 
fragment (which it needs to be to avoid ambiguity with the 
Floating_Point_Constant rule) then ANTLR will not call it 
itself.  So if you don't refer to it either then it'll never get 
called and the body of the rule is irrelevant.

In the OP's grammar, the IP_Address is being used only for its 
token value (as '$type = IP_Address;'), and this doesn't end up 
calling the rule.


There is another way to write the rules such that the IP_Address 
rule *does* get called -- something along these lines:

fragment DIGIT: '0'..'9';
fragment INT: DIGIT+;
fragment IP_Address: INT '.' INT '.' INT '.' INT;

Floating_Point_Constant
   :  (IP_Address) => IP_Address { $type = IP_Address; }
   |  INT
      (  '.' INT (('e'|'E') INT)?
      |  { $type = INT; }
      )
   ;

The use of a syntactic predicate in the first alt forces ANTLR to 
increase its lookahead to resolve the ambiguity.



More information about the antlr-interest mailing list