home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #27 / NN_1992_27.iso / spool / comp / unix / programm / 5303 < prev    next >
Encoding:
Text File  |  1992-11-16  |  1.7 KB  |  55 lines

  1. Path: sparky!uunet!ukma!asuvax!ncar!noao!amethyst!organpipe.uug.arizona.edu!news
  2. From: dave@cs.arizona.edu (Dave Schaumann)
  3. Newsgroups: comp.unix.programmer
  4. Subject: Re: FLEX question
  5. Message-ID: <1992Nov16.185206.28741@organpipe.uug.arizona.edu>
  6. Date: 16 Nov 92 18:52:06 GMT
  7. References: <1992Nov16.171445.26227@tc.cornell.edu>
  8. Sender: news@organpipe.uug.arizona.edu
  9. Reply-To: dave@cs.arizona.edu (Dave Schaumann)
  10. Organization: University of Arizona
  11. Lines: 41
  12. In-Reply-To: elan@tasha.cheme.cornell.edu (Elan Feingold)
  13.  
  14. In article <1992Nov16.171445.26227@tc.cornell.edu>, elan@tasha (Elan Feingold) writes:
  15. >How does one match things regardless of case? i.e If I want to match 
  16. >the keyword COOKIE, and I don't care what combination of upper and lower 
  17. >letters the person used...
  18. >
  19. >Anything but
  20. >
  21. >COOKIE        CreateCookie();
  22. >cOOKIE        CreateCookie();
  23.  
  24. There are two approaches you can take:
  25.  
  26. 1: Brute force
  27.  
  28. (C|c)(O|o)(O|o)(K|k)(I|i)(E|e)    { CreateCookie(); }
  29.  
  30. 2: 2-level recognition
  31.  
  32. Letter    [a-zA-Z]
  33. Word    {Letter}+
  34. %%
  35. {Word}    { if( is_cookie(yytext) ) CreateCookie() ;
  36.       else /* whatever */ ;
  37.     }
  38. %%
  39. int is_cookie( char *s ) {
  40.   char *t = "cookie" ;
  41.  
  42.   /* this may not work if you have a brain-damaged tolower() */
  43.   while( *s != '\0' && tolower(*s) == *t ) { s++ ; t++ }
  44.   return *s == *t ;
  45. }
  46.  
  47. I think #2 is almost always preferrable, since it makes for smaller lex
  48. tables, and it's easily extendable to other keywords.
  49.  
  50. -- 
  51. You unlock this door with the key of imagination.  Beyond it is another
  52. dimension: a dimension of sound, a dimension of sight, a dimension of mind.
  53. You're moving into a land of both shadow and substance, of things and ideas.
  54. You've just crossed over into... the Twilight Zone.
  55.