Using PLY

Maurice LING mauriceling at acm.org
Sun Sep 19 19:18:23 EDT 2004


> 
> Here's a kludgy but quick solution- modify the LexToken class in
> lex.py to keep track of number of type occurences.
> 
> class LexToken(object):  # change to new style class
>     type_count = {}   # store the count here
>     def __setattr__(self, key, value):
>         if key == 'type':
>             # when type attribute is assigned, increment counter
>             if value not in self.type_count:
>                 self.type_count[value] = 1
>             else:
>                 self.type_count[value] += 1
>         object.__setattr__(self, key, value)
> 
>     # ... and proceed with the original definition of LexToken
> 
>     def __str__(self):
>         return "LexToken(%s,%r,%d)" %
> (self.type,self.value,self.lineno)
>     def __repr__(self):
>         return str(self)
>     def skip(self,n):
>         try:
>             self._skipn += n
>         except AttributeError:
>             self._skipn = n
> -----------------------------------------
> 
> After you've run the lexer, lex.LexToken.type_count will the contain
> number of occurences of each token type.
> 
> -----------------------------------------
> 
> (Caveats-  1. I haven't tested this code.   2. I've got PLY 1.3;
> syntax may have changed in newer versions.  In fact, I hope it's
> changed; while PLY works very well, its usage could be way more
> pythonic)

I may be an idiot here but I don't quite see how LexToken.__setattr__ is 
called. There seems to be a gap in my logics.

Please assist.

Thanks
Maurice



More information about the Python-list mailing list