Perl script to remove comments from C Program using regular expressions

Many times we want to remove comments from a C-Program, may be for extracting some information from the code or some other code instrumentation. This is a handy Perl code snippet which I found from this forum.

1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
#!/usr/bin/perl -w

$/ = undef;   # no line delimiter
$_ = <>;   # read entire file

s! ((['"]) (?: \\. | .)*? \2) | # skip quoted strings
   /\* .*? \*/ |  # delete C comments
   // [^\n\r]*   # delete C++ comments
 ! $1 || ' '   # change comments to a single space
 !xseg;    # ignore white space, treat as single line
    # evaluate result, repeat globally
print;

This script will remove both C style comments /**/ and C++ comments //. Another advantage of this script is that, it will not remove /* ... */ which comes inside the double quotes like "This is /* a */string". This is acheived with the help of conditional operations in perl regular expressions(regexp). Here regular expression is used with "e" extented switch, inorder to put comments and spaces in between the regular expression. This makes it readable.

Thanks to Lorin.

No comments :

Post a Comment