[antlr-interest] Could antlr understand (group)3

Bryan Ewbank ewbank at gmail.com
Sat Sep 17 06:15:33 PDT 2005


No, there's no way to specify a specific number of repetitions;
however, it might be easier to write this way:

   protected HEX : '0'..'9'|'a'..'f'|'A'..'F';
   protected HEX3 : HEX HEX HEX;
   COLOR : '#' HEX3 ( HEX3 )? ;

Of course, this way would allow better detect/reject of illegal COLOR
groups (pardon my syntax; no manual handy):

   COLOR
   { int color = 0; }
   : '#' (HEX { count++; } )+
      {
          if (count != 3 && count != 6) { error bad COLOR; }
      }
   ;


On 9/17/05, Vidar Håkestad <vidar at hawkis.com> wrote:
> Is it possible for antlr to understand a digit as a group operator in addition
> to *, ? and +? This would help in situations where a specific number of
> elements are known.
> For example in CSS, a color value may be specified with either three or six
> hex numbers. It would be shorter to write
>
> HEX
>   : '0'..'9' | 'a'..'f' | 'A'..'F'
>   ;
>
> COLOR
>   :  '#' ((HEX)3 | (HEX)6)
>   ;
>
> instead of
>
> COLOR
>   : '#' ((HEX HEX HEX) | (HEX HEX HEX HEX HEX HEX))
>   ;


More information about the antlr-interest mailing list