0README ./scrunch |-- 0NOTE port to mac os x from irix: lex differences |-- 0Postings directory - statement of problem; responses .... | |-- postings.txt text file summary of postings | `-- scrunch-[12]-[12].html 3 html files of postings |-- 0README this file |-- Lex-trials direcetory - different lex solutions to problem |-- Makefile to test awk and lex solutions, run: make test |-- scrunchAwk awk solution of problem - (stephen peckham) |-- scrunchLex.l lex solution - (john rupley) `-- scrunchLex.l.irix irix version, ported to mac os x as above file STATEMENT OF PROBLEM: scrunch cpp output to make it more readable. Compress runs of "#" lines and blank lines, or runs of two or more blank lines: (\n*# lineno "file"\n+)+ or \n\n+ into a single 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. Two solutions: an awk version (by Stephen Peckham) and a Lex version (by myself). Both solutions are comparably simple. Here the character-stream processing of Lex has no advantage over the line-oriented processing of awk. Thus, IMHO, awk is preferable. C-code or sed-script solutions would be more complicated. awk solution (#From: peckham@svax.cs.cornell.edu (Stephen Peckham): awk ' {if (NF == 0) blanks++ else if ($1=="#") {l_no = $2-1; f = $3; blanks = 2;} else { if (blanks > 1) print "#", l_no, f; else if (blanks == 1) print ""; blanks = 0; print $0; } l_no++; }' lex solution (John Rupley rupley!local@cs.arizona.edu): %} char f[2048]; int x; int xxlineno; W [ \t]*\n %% #.+\n {sscanf(yytext,"#%d%s",&xxlineno,f); x++;} {W}/{W}|# x++; xxlineno++; {W} if(!x) ECHO; xxlineno++; .+\n {if(x) printf("# %d %s\n",xxlineno,f); ECHO; xxlineno++; x=0;} To test the solutions, run: make clean make test The test consists of (see Makefile): 1. creating a test cpp file from the lex program output, lex.yy.c (lex.yy.c is compiled with gcc to give the lex executable); 2. then "scrunching" the cpp file with the awk or the lex executable, and comparing the scrunched outputs (no significant differences); 3. finally, showing that the scrunched output is "good", i.e., it will compile to give a working executable identical in operation to the lex or awk executables. For discussion, see the comp.lang.c postings: 0Postings/postings.txt or, more readable, scrunch-1-1.html scrunch-2-[12].html John Rupley rupley@u.arizona.edu -or- jar@rupley.com 30 Calle Belleza, Tucson AZ 85716 - (520) 325-4533; fax - (520) 325-4991 Dept. Biochemistry & Molecular Biophysics, Univ. Arizona, Tucson AZ 85721