Match C Comments... the right answer (was Re: LEX rule, anyone???)

Randal Schwartz merlyn at iwarp.intel.com
Mon Dec 18 07:08:37 AEST 1989


In article <2191 at prune.bbn.com>, rsalz at bbn (Rich Salz) writes:
| In <601 at vice2utc.chalmers.se> d5kwedb at dtek.chalmers.se (Kristian Wedberg) writes:
| >A question from a friend of mine, P{r Eriksson:
| >	Does anyone know how to write a LEX rule for C comments,
| >	ie for everything between /* and */, nesting not allowed?
| 
| We go through this in comp.lang.c about once a year.  Almost everyone
| gets it wrong.  The best thing to do is to define a lex rule that
| catches "/*" and in the actions for that rule look for */ on your own.

OK, almost everyone got it wrong (and I took great delight at pointing
out what was wrong sometimes :-), but there was ONE correct answer --
mine ( :-)...

		"/*"(\**[^*/]|\/)*\*+\/

Don't accept any cheap imitations.  They're probably wrong.  (This one
is wrong in that it will match C comments inside of text strings, but
that's just plain pathological. :-)

(No, I don't have one in Perl... :-)

Just another regex hacker,
-- 
/== Randal L. Schwartz, Stonehenge Consulting Services (503)777-0095 ====\
| on contract to Intel's iWarp project, Hillsboro, Oregon, USA, Sol III  |
| merlyn at iwarp.intel.com ...!uunet!iwarp.intel.com!merlyn	         |
\== Cute Quote: "Welcome to Oregon... Home of the California Raisins!" ==/



More information about the Comp.lang.c mailing list