Want a way to strip comments from a

John Rupley rupley at arizona.edu
Sun Mar 26 12:51:19 AEST 1989


In article <893 at m10ux.UUCP>, mnc at m10ux.UUCP (Michael Condict) writes:
> Oops, the previous lex script I posted for deleting comments from
> C source code is incorrect -- it doesn't recognize:  /***...**/
> Here is a better one (simpler, too):
> 
> 	%%
> 	\"([^\\"]*\\(.|\n))*[^\\"]*\"	ECHO;
> 	"/*"([^*]|"*"+[^/*])*"*"*"*/"	;
> 	.				ECHO;

You indeed fixed the /***/ error, but two errors remain.

First, no handling of single-quoted double quotes:

        main() {printf("%c\n", '"');/*gotcha*/printf("%c\n", '"');}

Second, your program crashes when uncommenting a real source file, with
a sizeable change history or whatever inside a comment.  You need at
least one state change, so a comment can be matched line-by-line, and
so not overflow a Lex buffer.  Both previous Lex postings did it
right.  A third state, to handle quoted strings line-by-line, is perhaps
optional, and the previous postings differ here. Apparently you missed
the previous Lex postings, which I will be happy to email you on request.

My argument, that it's difficult to make a logical error in coding this
problem in Lex, has now been demonstrated wrong (sob :-). But at least 
Lex is still outscoring straight C (faint praise :-?).

John Rupley
rupley!local at megaron.arizona.edu



More information about the Comp.lang.c mailing list