When I try to use pycparser to parse comment files, I got 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: /
Solution : pycparser in the current version does not support comments in the source code, but this fork allows it, or you can use the recipe from the question Python snippet to remove C and C ++ comments to remove comments from the source code.
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;}"))
source share