[antlr-interest] hollerith string

Ric Klaren klaren at cs.utwente.nl
Fri Aug 15 04:06:19 PDT 2003


On Fri, Aug 15, 2003 at 07:04:10AM -0000, marcschellens wrote:
> 5Habcde  is 'abcde'
>
> I did the following (using antlr c++ mode) in the lexer:
>
> NUMBER
> 	: (DIGIT)+
>         (H!
>             {
>                 string text=$getText;
>                 int len=atoi(text.c_str());
>                 $setText("");
>                 for( int i=0; i<len; i++)
>                 {
>                     consume(); // appends char
>                 }
>                 _ttype=STRING;
>             }
>         )?
>     ;
>
> I think it should work, but generally I prefer not to call
> internals like 'consume()' directly. So is there a way of avoiding
> it?

You can 'abuse' the initaction of a closure for it. This should work I
think starting at antlr 2.7.1. 2.7.0 placed the initaction outside in front
of the loop which is not very usefull.

NUMBER { size_t n, i = 0; } :
   num:DIGITS
   {
      istringstream s(num->getText());
      s >> n;
   }
   ( 'H'! { $setType(STRING); }
      (
       { // init action gets executed even in guessing mode
          if( i == n )
             break;
          i++;	// count chars here so that guessing mode works
       }:
       CHAR
      )+
   )?
   ;

Cheers,

Ric
--
-----+++++*****************************************************+++++++++-------
    ---- Ric Klaren ----- j.klaren at utwente.nl ----- +31 53 4893722  ----
-----+++++*****************************************************+++++++++-------
  "You can't expect to wield supreme executive power just because some
   watery tot throws a sword at you!"
  --- Monty Python and the Holy Grail


 

Your use of Yahoo! Groups is subject to http://docs.yahoo.com/info/terms/ 




More information about the antlr-interest mailing list