comment contest

This contest stems from David’s post regarding a script to comment out words in a C file. The general applicability of this script is questionable, but it’s still a fun exercise. Here are the instructions:

  1. Write a command-line filter which will comment out all matches of a pattern given on the command-line, reading a C program from stdin and filtering to stdout. Whether the pattern is a literal, glob, regular expression is up to the contestant.
  2. The filter should ignore matches that are already contained within a comment (including C++ style comments) or double-quoted string.

The existing gawk implementation on David’s blog is an interesting starting point, though it doesn’t fulfill all the requirements above.

Samples:

  1. simple: input / output (“add”)
  2. wacky: input / output (“wacky”)
  3. deranged: input / output (“int”)