eliben / pycparser

:snake: Complete C99 parser in pure Python
Other
3.26k stars 612 forks source link

Parsing C files with comments throws error #460

Closed zeeshanr97 closed 2 years ago

zeeshanr97 commented 2 years ago

Whenever C files contain comment or comments then the parser halts and throws the following error: Traceback (most recent call last): File "testPa.py", line 21, in ast = parse_file(args.filename, use_cpp=False) File "C:\python3\lib\site-packages\pycparser__init__.py", line 90, in parse_file return parser.parse(text, filename) File "C:\python3\lib\site-packages\pycparser\c_parser.py", line 149, in parse return self.cparser.parse( File "C:\python3\lib\site-packages\pycparser\ply\yacc.py", line 331, in parse return self.parseopt_notrack(input, lexer, debug, tracking, tokenfunc) File "C:\python3\lib\site-packages\pycparser\ply\yacc.py", line 1199, in parseopt_notrack tok = call_errorfunc(self.errorfunc, errtoken, self) File "C:\python3\lib\site-packages\pycparser\ply\yacc.py", line 193, in call_errorfunc r = errorfunc(token) File "C:\python3\lib\site-packages\pycparser\c_parser.py", line 1858, in p_error self._parse_error( File "C:\python3\lib\site-packages\pycparser\plyparser.py", line 67, in _parse_error raise ParseError("%s: %s" % (coord, msg)) pycparser.plyparser.ParseError: main.c:24:3: before: /

kunzeng-ch commented 2 years ago

Hello! I get the same problem. Have you found the solution?

zeeshanr97 commented 2 years ago

No, I'll try to add the comment parsing code myself but in any case this issue should be fixed.

JulianKemmerer commented 2 years ago

Is the solution to run the c pre processor?

kunzeng-ch commented 2 years ago

Have you tried to give the choice of True to use_cpp? When I set the use_cpp True, I got the problem: subprocess.CalledProcessError: Command '['C:\MinGW\bin\gcc.exe', '-I D:\anaconda\envs\nlp38\Lib\site-packages\pycparser\utils\fake_libc_include', './c_files/basic.c']' returned non-zero exit status 1.

eliben commented 2 years ago

This is described right in the README: https://github.com/eliben/pycparser#3using

Also read the blog post linked in section 3.2