regex for C comments

Theodore Stevens Norvell norvell at
Mon Feb 1 19:28:53 AEST 2016

In article <502 at chem.ucsd.EDU> tps at (Tom Stockfisch) writes:
>In article <19365 at> schmidt at (Doug Schmidt) writes:
>>In their book ``Introduction to Compiler Construction with UNIX,''
>>Schreiner and Friedman provide the following LEX regular expression
>>for recognizing C comments:
>This expression fails on each of the following:
>	/*****//hello world */
Is that really a C comment?  I think only the first 7 characters are.
>So, who has the shortest single LEX expression that correctly
>matches C comments --
>ignoring string and character constants,
>and disallowing start conditions?
>Mine is
>	"/*"\/*([^/]|{[^*/]\/+})*"*/"


or more legibly

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

Though I haven't proved it.

Theo Norvell     U of Toronto

More information about the Comp.lang.c mailing list