Pycparser failed in the comments

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;}")) 
+4
source share
1 answer

Indeed, pycparser does not parse comments or anything related to the C preprocessor (in a regular C compiler, the preprocessor passes comments before the compiler sees them).

To stop comments from messy parsing, first run the code through the preprocessor, as shown in pycparser README . To actually meaningfully parse the comments (and get their contents), pycparser is not the most suitable tool, unfortunately.

+6
source

Source: https://habr.com/ru/post/1483581/


All Articles