Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

General utility to remove/strip all comments from source code in various languages?

Tags:

python

c

php

I am looking for a command-line tool that removes all comments from an input file and returns the stripped output. It'd be nice it supports popular programming languages like c, c++, python, php, javascript, html, css, etc. It has to be syntax-aware as opposed to regexp-based, since the latter will catch the pattern in source code strings as well. Is there any such tool?

I am fully aware that comments are useful information and often leaving them as they are is a good idea. It's just that my focus is on different use cases.

like image 404
OTZ Avatar asked Jul 28 '10 00:07

OTZ


People also ask

How do you turn off comments in CPP?

We will parse the string line by line as an ideal compiler does. We will ignore all the characters between and after these block quotes when we encounter// or '/* /*. ' A function removeString(vector<string>&source) takes a source code as an input and returns the code after removing its comments.


1 Answers

cloc, a free Perl script, can do this.

Remove Comments from Source Code

How can you tell if cloc correctly identifies comments? One way to convince yourself cloc is doing the right thing is to use its --strip-comments option to remove comments and blank lines from files, then compare the stripped-down files to originals.

It supports a lot of languages.

like image 72
Mark Rushakoff Avatar answered Sep 26 '22 17:09

Mark Rushakoff