in reply to removing C style comments from text files

Moving beyond the whole perl/regex thing, what about just running the c pre processor on it?

The only downside there I see is possible text matching #defines that you don't want to get replaced, but I would think matching what cpp considers a #define would be vastly simpler then matching comments..
  • Comment on Re: removing C style comments from text files

Replies are listed 'Best First'.
Re: removing C style comments from text files
by Abigail-II (Bishop) on Jan 06, 2004 at 09:30 UTC
    No, you don't want the preprocessor output. Consider the following classical program:
    # include <stdlib.h> # include <stdio.h> int main (int argc, char * argv []) { printf ("Hello, world\n"); /* Print 'Hello, world' */ exit (0); }

    Assume this is in the file hello.c.

    $ gcc -E hello.c | wc -l 1566 $ gcc -E hello.c | grep -v '^$' | wc -l 853
    Even with blank lines removed, the 7 line hello.c expands to 853 lines of pre processor output.

    Abigail

      Well yes, but it expands because you #include files. From his description (text files) I assumed that they weren't actual C programs and wouldn't be using the rest of the pre-processor commands. So there wouldn't be any #includes to expand the file size so dramatically. Beyond that I also suggested that it might be easier to match anything the pre-processor would consider a #define/#include, since the rules for that are fairly strict as I recall.