home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #31 / NN_1992_31.iso / spool / comp / lang / c / 19098 < prev    next >
Encoding:
Internet Message Format  |  1992-12-31  |  2.0 KB

  1. Path: sparky!uunet!gatech!usenet.ins.cwru.edu!agate!dog.ee.lbl.gov!horse.ee.lbl.gov!torek
  2. From: torek@horse.ee.lbl.gov (Chris Torek)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Wanted: program to extract comments from C
  5. Date: 31 Dec 1992 23:18:36 GMT
  6. Organization: Lawrence Berkeley Laboratory, Berkeley CA
  7. Lines: 30
  8. Message-ID: <28184@dog.ee.lbl.gov>
  9. References: <1h580nINN11p@life.ai.mit.edu> <1992Dec26.095028.29823@netcom.com>
  10. NNTP-Posting-Host: 128.3.112.15
  11.  
  12. In article <1992Dec26.095028.29823@netcom.com> uniteq@netcom.com
  13. (Uniteq Application Systems) writes:
  14. >... you could also do it with a lot fewer lines (and a much
  15. >more complicated engine, but we let lex worry about that stuff):
  16. >
  17. >%%
  18. >"//".*                {printf("%s\n", yytext); /* C++ comments */ }
  19. >"/*"([^*]|\*+[^*/])*\*+\/    {printf("%s\n", yytext); /* std C comments */ }
  20. >.|\n                { /* ignore everything else */ }
  21. >
  22. >I am uncertain about the correct semantics when mixing the two commenting
  23. >styles - the above code allows `//' to comment out a `/*' but not a `*/'.
  24. >The code Kenneth presented took the rather strange (IMO) stance of making
  25. >either type of comment able to override the other (`//' remains active
  26. >within a `/* ... */' pair).
  27.  
  28. All of these points are valid, but there are two problems with this
  29. approach.  The first is that lex (including its improved cousin flex)
  30. has a fairly small limit on the size of `yytext'.  Comments may often
  31. exceed this limit.  One can raise it in any lex program (simply
  32. redefine YY_BUF_SIZE), but any limit may prove too small (I have seen
  33. truly enormous comments in some code).  The second is that regular
  34. expressions for recognizing /* ... */ comments are notoriously
  35. difficult.  The one above appears correct, but anyone trying to
  36. reproduce this from memory may get it wrong.  For these reasons it
  37. usually seems better to use start states (easier in flex, with its
  38. exclusive start states) to handle this.
  39. -- 
  40. In-Real-Life: Chris Torek, Lawrence Berkeley Lab CSE/EE (+1 510 486 5427)
  41. Berkeley, CA        Domain:    torek@ee.lbl.gov
  42.