Want a way to strip comments from a

John Rupley rupley at arizona.edu
Sun Mar 26 12:36:13 AEST 1989


> In article <620 at gonzo.UUCP>, daveb at gonzo.UUCP (Dave Brower) writes:
> So, I offer this week's challenge:  Smallest program that will take
> "blank line" style cpp output on stdin and send to stdout a scrunched
> version with appropriate #line directives.  [f]lex, Yacc, [na]awk, sed,
> perl, c, c++ are all acceptable.  This will be an amusing excercise in
> typical text massaging that can be enlightening for many people.

"Scrunching" is probably a matter of taste, with regard to the format
of the ouput.  So I am not sure what you, yourself, want.  But below
is a guess.  Lex, of course.  May not be portable, but it should work
with minor mods on other Unices.  Should be easy to modify for different
output format.

John Rupley
rupley!local at megaron.arizona.edu


%{ /*---------------------------start of text---------------------------*/
/*-
 * SCRUNCH.l
 *
 * Scrunch cpp output.
 * 	In-Reply-To: daveb at gonzo.UUCP (Dave Brower)
 * 	Message-ID: <620 at gonzo.UUCP>			#comp.lang.c
 * 
 * Compress runs of "#" lines and blank lines, or runs of two or more
 * blank lines:
 * 	(\n*# lineno "file"\n+)*  or  \n\n\n+
 * into a single line:
 *	#line lineno "file"\n
 * which is output before the next line of program text 
 * (corresponding to line "lineno" of the source "file").
 * The values of "lineno" and "file" are adjusted for changes in
 * source resulting from #include statements.
 * Lines with whitespace are not considered blank and are passed.
 *
 * Compilation:
 *	lex scrunch.l
 *	cc -O lex.yy.c -ll -o scrunch
 *
 * Minimally tested with UNIX sys5r2 cpp only, as follows:
 * (a)	/lib/cpp -Dprocessor=1 lex.yy.c >scruch.cpp	#specify your processor
 *	scrunch <scrunch.cpp >scrunch.cpp.c
 *	cc -O scrunch.cpp.c -ll
 *	cmp -l a.out scrunch		#should give date/name diffs only
 * (b)	compare line numbers in scrunch.cpp.c with lex.yy.c and scrunch.cpp
 *		(no differences stood out)
 *
 * Possible bugs:
 *	escaped newlines in macros.
 *	????
 *
 * John Rupley
 * rupley!local at megaron.arizona.edu
 */
%}
	char		file[BUFSIZ];

POUND	#[ ]+[0-9]+[ ]+\".*$
TEXT	[^#\n].*$
%START	POUND TEXT
%%
<INITIAL>.	{unput(yytext[0]); BEGIN TEXT;}
<POUND>{POUND}	sscanf(yytext, "# %d %s", &yylineno, &file[0]);
<POUND>{TEXT}	{printf("#line %d %s\n", yylineno-1, file); ECHO; BEGIN TEXT;}
<POUND>\n	;
<TEXT>{POUND}	{sscanf(yytext, "# %d %s", &yylineno, &file[0]); BEGIN POUND;}
<TEXT>\n{3,}	{printf("\n"); BEGIN POUND;}
<TEXT>{TEXT}|\n	ECHO;
.		printf("\nERROR: file %s, line %d, char 0x%x=%c\n",
			file, yylineno, (unsigned int) yytext[0], yytext[0]);
%%
/*----------------------------end of text-------------------------------*/



More information about the Comp.lang.c mailing list