From: div@NAD.3Com.COM (Dinesh Venkatachalam x5498)
Newsgroups: comp.unix.questions
Subject: Re: Good way to strip K&R style comments
Date: 30 Jul 92 00:50:44 GMT
Reply-To: div@NAD.3Com.COM
Organization: 3Com Corp

1.The shortest perl solution was from  guru and was as follows

perl -e 'undef($/);$_=<>;s/\/\*[^\000]*\*\///g;print'   filename

2.The most popular method was 

pipe it through cpp

3. The lex solution ( flex ?) did not work on my Sun OS lex processor but is as follows
   from johnl

%x COMMENT

%%

"/*"		BEGIN COMMENT;
<COMMENT>"*/"	BEGIN INITIAL;
<COMMENT>.	/* ignore */ ;
<COMMENT>\n	/* ignore */ ;

In AT&T lex, I ususally write explicit code to eat the comment once I've
seen the /*, like this:

"/*"		{	int c1 = 0, c2 = 0;

			for(;;) {
				c1 = c2; c2 = input();
				if(c1 == '*' && c2 == '/')
					break;
				if(c2 == EOF)
					/* complain about unterm'ed comment */
			}
		}


Is there any way to make this work on standard lex ??

4. The C program solution from matt via bhuff was

/******************************************************************************
**  File Name:		uncommentc.c
**  Procedure Name:	uncommentc
**  Purpose:		Remove comments from C source.
**  Created by:		Matthew Rabuzzi
**
**  Calling Example:	uncommentc foo.c bar.c baz.c
**			more $* | uncommentc | awk '...'
**
**  Parameters:		Optional filename(s).  If none, reads from stdin.
**   
**  Description:	Uncomments C code, that is, any text enclosed in
**			the comment delimiters '/*' and its inverse,
**			including the delimiters, except within quotes.
**			I.e., text of 'x /* y *\ z' (pretend the \ is a / )
**			becomes 'x  z', but 'x "/* y *\" z' is output as is.
**
**  Bugs:		Does not yet handle C++ // end-of-line comments.
**
******************************************************************************/

#include <stdio.h>

void uncomment (void);

main (int argc, char **argv)
{
    int		i, rtnstatus = 0;

    if (argc > 1)
		for (i=1; i < argc; i++) {
	    if (!freopen(argv[i],"r",stdin)) {
		rtnstatus = 2;
		fprintf(stderr,"%s: could not open file %s\n",
		    argv[0], argv[i]);
	    }
	    else uncomment();
	}
    else uncomment();

    exit(rtnstatus);
}

void uncomment (void)
{
    int		c, d, comment=0, quote=0;
    long	lineno=1;

    while ((c = getchar()) != EOF) {

	if (comment) {
	    if (c == '*') {
		if ((d = getchar()) == '/') comment = 0;
		else ungetc(d,stdin);
	    }

	} else if (quote) {
	    if (c == '"') quote = 0;
	    putchar(c);

	} else if (c == '/') {
	    if ((d = getchar()) == '*') comment = lineno;
	    else {
		ungetc(d,stdin);
		putchar(c);
	    }

	} else if (c == '"') {
	    quote = lineno;
	    putchar(c);

	} else {
	    putchar(c);
	}

	if (c == '\n') lineno++;

    }

    /* Sanity checks.  If the code compiled, these should never occur. */
    if (comment) printf ("/* Begin-comment on line %d, no end-comment */\n",
			 comment);
    if (quote)   printf ("/* Begin-quote on line %d, no end-quote */\n",
			 quote);
}


-------------------------------------------------------------------------------------

Well thats all, and thanks for the suggestions and replies. I am still looking for
a lex solution as it will help me not running through a pre-processor.


Dinesh