Want a way to strip comments from a

Leo de Wit leo at philmds.UUCP
Mon Mar 20 22:52:15 AEST 1989


In article <3114 at nunki.usc.edu> jeenglis at nunki.usc.edu (Joe English) writes:
|
|leo at philmds.UUCP (Leo de Wit) writes:
|>
|>    puts(" A comment /* in here */");
|>
|>And you can give more examples showing it isn't that trivial; a challenge
|>for the sed adept, perhaps ...
|
|Does it *have* to be done in sed/awk/other text processor?
|This problem is fairly difficult to solve using regexp/editor
|commands, but it's a piece of cake to do in C:

Piece of cake? Your program can't even strip its own comments (try it)!
Reason:

|	case '"' :
|	  instring = !instring;
|	  break;

This is both a defect in your program, and the cause that subsequent
comments aren't detected when using the source as input. After the
sequence '"' instring is 1. Besides it doesn't handle multiple
character char constants (e.g. '/*', though one could perhaps argue
whether it should).

|This hasn't been tested thoroughly; it's mostly 
|from memory.  

If your memory was ok, the program wasn't tested thoroughly 8-).
Though the problem isn't difficult, it isn't so trivial as you thought
it was.

	 Leo.



More information about the Comp.lang.c mailing list