Want a way to strip comments from a C file

The Polymath hollombe at ttidca.TTI.COM
Thu Mar 16 09:15:59 AEST 1989


In article <880 at m10ux.UUCP> mnc at m10ux.UUCP (Michael Condict) writes:
}I recently posted to this group a shell script that calls three sed scripts
}to extract function prototypes from (practically) any C source.  One of the
}three sed scripts consisted of little more than comment removal -- exactly
}what you are looking for.  Here is the relevant portion:
}
}[...]

}The disclaimers are that (1) it only works with BSD-derived sed, unless you
}get rid of all the comments; and (2) it will fail for programs that contain
}the extremely unlikely "Used#to%be..." strings used as markers in the script.

If I understood the original posting correctly, it will also fail if it
encounters a /* or */ within a quoted string constant.  E.g.:

     char *msg1 = "The symbol \"/*\" begins a comment in C. \n";
     char *msg2 = "The symbol \"*\\\" ends a comment in C. \n";

I deliberately added the escaped double-quotes to show that true, safe
comment detection and removal isn't a trivial problem.  There are probably
a number of other "special" cases that can cause a simple, scan-for-/*,
scan-for-*/ algorithm to fail.

}This has been tested on thousands of lines of source code from various sources,
}but no guarantees.  You get what you pay for.

Sound advice.

-- 
The Polymath (aka: Jerry Hollombe, hollombe at ttidca.tti.com)  Illegitimati Nil
Citicorp(+)TTI                                                 Carborundum
3100 Ocean Park Blvd.   (213) 452-9191, x2483
Santa Monica, CA  90405 {csun|philabs|psivax}!ttidca!hollombe



More information about the Comp.lang.c mailing list