Path: utzoo!utgpu!jarvis.csri.toronto.edu!mailrus!ames!ig!arizona!rupley From: rupley@arizona.edu (John Rupley) Newsgroups: comp.lang.c Subject: Re: Scrunch blank lines Summary: mice and mountains Message-ID: <9996@megaron.arizona.edu> Date: 30 Mar 89 23:43:56 GMT References: <7150@siemens.UUCP> <9900010@bradley> <4896@cbnews.ATT.COM> <7472@phoenix.Princeton.EDU> Distribution: na Organization: U of Arizona CS Dept, Tucson Lines: 42 In article <7472@phoenix.Princeton.EDU>, bernsten@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@megaron.arizona.edu