Python的片段删除C和C ++评论 [英] Python snippet to remove C and C++ comments
问题描述
我在寻找的Python code,从一个字符串中删除C和C ++注释。 (假定字符串包含整个C源文件。)
I'm looking for Python code that removes C and C++ comments from a string. (Assume the string contains an entire C source file.)
我意识到,我可以.match()子用正则表达式,但这并不解决嵌套 / *
,或者具有 / /
A 内/ * * /
。
I realize that I could .match() substrings with a Regex, but that doesn't solve nesting /*
, or having a //
inside a /* */
.
在理想情况下,我会preFER非天真的实现,正确处理案件的尴尬
Ideally, I would prefer a non-naive implementation that properly handles awkward cases.
推荐答案
我不知道,如果你熟悉 SED
,基于UNIX(但Windows - 可选)文本解析程序,但我已经找到了一个sed脚本这里这将删除C / C ++从文件的意见。这是非常聪明的;例如,它将忽略//和/ *,如果在字符串中声明等。从内的Python发现,它可以用下面的code可以使用
I don't know if you're familiar with sed
, the UNIX-based (but Windows-available) text parsing program, but I've found a sed script here 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:
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()
在这个程序中, source_ code
是变量按住C / C ++源$ C $ C,并最终 stripped_ code
将举行C / C ++ code除去了意见。当然,如果你有磁盘上的文件,你可以有输入
和输出
变量文件句柄指向这些文件(输入
在读模式,在写模式输出
)。 remccoms3.sed
是从上面的链接文件,并应保存在磁盘上的可读位置。 SED
也可在Windows,并附带在默认情况下大多数GNU / Linux发行版和Mac OS X安装。
In this program, source_code
is the variable holding the C/C++ source code, and eventually stripped_code
will hold C/C++ code with the comments removed. Of course, if you have the file on disk, you could have the input
and output
variables be file handles pointing to those files (input
in read-mode, output
in write-mode). remccoms3.sed
is the file from the above link, and it should be saved in a readable location on disk. sed
is also available on Windows, and comes installed by default on most GNU/Linux distros and Mac OS X.
这可能会比纯Python解决方案更好;没有必要推倒重来。
This will probably be better than a pure Python solution; no need to reinvent the wheel.
这篇关于Python的片段删除C和C ++评论的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!