Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I don't know if you're familiar with <code>sed</code>, the UNIX-based (but Windows-available) text parsing program, but I've found a sed script <a href="http://sed.sourceforge.net/grabbag/scripts/remccoms3.sed" rel="noreferrer">here</a> which will remove C/C++ comments from a file. It's very smart; for example, it will ignore '//' and '/*' if found in a string declaration, etc. From within Python, it can be used using the following code:</p> <pre><code>import subprocess from cStringIO import StringIO input = StringIO(source_code) # source_code is a string with the source code. output = StringIO() process = subprocess.Popen(['sed', '/path/to/remccoms3.sed'], input=input, output=output) return_code = process.wait() stripped_code = output.getvalue() </code></pre> <p>In this program, <code>source_code</code> is the variable holding the C/C++ source code, and eventually <code>stripped_code</code> will hold C/C++ code with the comments removed. Of course, if you have the file on disk, you could have the <code>input</code> and <code>output</code> variables be file handles pointing to those files (<code>input</code> in read-mode, <code>output</code> in write-mode). <code>remccoms3.sed</code> is the file from the above link, and it should be saved in a readable location on disk. <code>sed</code> is also available on Windows, and comes installed by default on most GNU/Linux distros and Mac OS X.</p> <p>This will probably be better than a pure Python solution; no need to reinvent the wheel.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload