Path: utzoo!utgpu!jarvis.csri.toronto.edu!mailrus!tut.cis.ohio-state.edu!bloom-beacon!gatech!rutgers!ucsd!ames!ig!arizona!rupley From: rupley@arizona.edu (John Rupley) Newsgroups: comp.lang.c Subject: Re: Want a way to strip comments from a Summary: is this what may be wanted? Message-ID: <9887@megaron.arizona.edu> Date: 26 Mar 89 02:36:13 GMT References: <7150@siemens.UUCP> <9900010@bradley> <4896@cbnews.ATT.COM> <620@gonzo.UUCP> Distribution: na Organization: U of Arizona CS Dept, Tucson Lines: 74 > In article <620@gonzo.UUCP>, daveb@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@megaron.arizona.edu %{ /*---------------------------start of text---------------------------*/ /*- * SCRUNCH.l * * Scrunch cpp output. * In-Reply-To: daveb@gonzo.UUCP (Dave Brower) * Message-ID: <620@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.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@megaron.arizona.edu */ %} char file[BUFSIZ]; POUND #[ ]+[0-9]+[ ]+\".*$ TEXT [^#\n].*$ %START POUND TEXT %% . {unput(yytext[0]); BEGIN TEXT;} {POUND} sscanf(yytext, "# %d %s", &yylineno, &file[0]); {TEXT} {printf("#line %d %s\n", yylineno-1, file); ECHO; BEGIN TEXT;} \n ; {POUND} {sscanf(yytext, "# %d %s", &yylineno, &file[0]); BEGIN POUND;} \n{3,} {printf("\n"); BEGIN POUND;} {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-------------------------------*/