当我尝试使用pycparser来解析带有注释的文件时,我得到了ParseError
import pycparser
parser = pycparser.CParser()
parser.parse("int main(void){return 0;}")
parser.parse("/* comment */ int main(void){return 0;}")
Traceback (most recent call last):
File "test_pycparser.py", line 18, in <module> parser.parse("/* comment */ int main(void){return 0;}")
File "build\bdist.win32\egg\pycparser\c_parser.py", line 124, in parse
File "build\bdist.win32\egg\pycparser\ply\yacc.py", line 265, in parse
File "build\bdist.win32\egg\pycparser\ply\yacc.py", line 1047, in parseopt_notrack
File "build\bdist.win32\egg\pycparser\c_parser.py", line 1423, in p_error
File "build\bdist.win32\egg\pycparser\plyparser.py", line 54, in _parse_error
pycparser.plyparser.ParseError: :1:1: before: /
解决方案:当前版本的pycparser不支持源代码中的注释,但this fork允许它,或者您可以使用问题Python snippet to remove C and C++ comments中的配方从源代码中删除注释.
import pycparser
import re
def comment_remover(text):
def replacer(match):
s = match.group(0)
if s.startswith('/'):
return ""
else:
return s
pattern = re.compile(
r'//.*?$|/\*.*?\*/|\'(?:\\.|[^\\\'])*\'|"(?:\\.|[^\\"])*"',
re.DOTALL | re.MULTILINE
)
return re.sub(pattern, replacer, text)
parser = pycparser.CParser(keep_comment=True)
parser.parse("int main(void){return 0;}")
parser.parse("/* comment */ int main(void){return 0;}")
parser_2 = pycparser.CParser()
parser.parse(comment_remover("/* comment */ int main(void){return 0;}"))
解决方法:
实际上,pycparser不会解析注释,也不解析与C预处理器相关的任何内容(在常规C编译器中,预处理器会在编译器看到之前删除注释).
要阻止注释搞乱解析,请首先通过预处理器运行代码,如pycparser README中所述.为了真正有意义地解析注释(并获取其内容),不幸的是,pycparser不是正确的工具.