Scrunch blank lines

John Rupley rupley at arizona.edu
Fri Mar 31 09:43:56 AEST 1989


In article <7472 at phoenix.Princeton.EDU>, bernsten at phoenix.Princeton.EDU (Dan
Bernstein) writes:
> Dave Brower asks for a filter ``that will take "blank line" style cpp
> output on stdin and send to stdout a scrunched version with appropriate
                                                              ^^^^^^^^^^^
> #line directives.'' If we may combine built-in utilities to handle the
> problem, then this 9-line shell script will do it (combine the last
> two lines to make it 8):
> 
>   #!/bin/sh
>   ( tr XY '\375\376' | sed 's/^\(.\)\(.*\)/X\1\2Y/
>   tend
>   i\
>   X#line
>   d
>   :end
>   =' | uniq | tr '\012X' ' \012'; echo ''; )
>   | sed 's/Y.*//' | tr '\375\376' XY | sed -n '1!p'

I am not sure this is what the original poster wanted, ie ``appropriate''
may refer to #line directives with line numbers that reference the 
source file, not the cpp output.  Regardless, the above script is 
truly trivial in Lex:

%%
\n\n+	printf("\n#line %d \n", yylineno);
.|\n	ECHO;

> Ahem? Are we forgetting sed here? (Then again, I hate awk, love sed,
> and prefer C to lex. I'd rather have a sed script twice as slow as an
> awk script. But that's just personal bias.)

How could one forget sed (:-)?  But for matching patterns that cross
line boundaries, Lex is a natural, because it sees a file as a stream of
characters rather than as a stream of records. Sed and awk are record-based
and thus seem forced for multi-line matching.  Prefer C to Lex? Hmmm... Lex
is just the machinery for a pattern-based switch statement, with the user
supplying ``case'' statements written in C.

John Rupley
rupley!local at megaron.arizona.edu



More information about the Comp.lang.c mailing list