3

Using pycparser I encountered a problem with the included headers in the C code I want to parse. For some mysterious reason, some "fake header" works fine and other doesn't work as well. For example, if I include stdint.h, pycparser can parse the code without any problem. However, if I try with stdio.h, I get an error. So, the example code given with pycparser (using_gcc_E_libc.py in the example folder) does not work !

This is what is used in the cited example code :

ast = parse_file(filename, use_cpp=True,
        cpp_path='gcc',
        cpp_args=['-E', r'-I../utils/fake_libc_include'])

and this is the error I get :

    Traceback (most recent call last):
  File "/home/patatarte/pycparser/examples/using_gcc_E_libc.py", line 29, in <module>
    cpp_args=['-E', r'-I../utils/fake_libc_include'])
  File "/usr/lib/python3.4/site-packages/pycparser/__init__.py", line 93, in parse_file
    return parser.parse(text, filename)
  File "/usr/lib/python3.4/site-packages/pycparser/c_parser.py", line 138, in parse
    debug=debuglevel)
  File "/usr/lib/python3.4/site-packages/pycparser/ply/yacc.py", line 265, in parse
    return self.parseopt_notrack(input,lexer,debug,tracking,tokenfunc)
  File "/usr/lib/python3.4/site-packages/pycparser/ply/yacc.py", line 1047, in parseopt_notrack
    tok = self.errorfunc(errtoken)
  File "/usr/lib/python3.4/site-packages/pycparser/c_parser.py", line 1631, in p_error
    column=self.clex.find_tok_column(p)))
  File "/usr/lib/python3.4/site-packages/pycparser/plyparser.py", line 54, in _parse_error
    raise ParseError("%s: %s" % (coord, msg))
pycparser.plyparser.ParseError: /usr/lib/gcc/x86_64-unknown-linux-gnu/4.9.2/include/stdarg.h:40:27: before: __gnuc_va_list

Can somebody help me understand why this happens and how I can solve it ?

Thanks in advance !

patatarte
  • 43
  • 4

1 Answers1

3

The fake headers are there for a reason. While it's possible to make pycparser parse the system headers on Linux, it takes work and is unnecessary 99.9% of the time.

Are you sure the fake headers actually get included properly in the code you're parsing?

One way to debug this is to first preprocess the code with gcc -E and the relevant (fake) headers, and then just run pycparser on the preprocessed file. This lets you split the issue in two and see exactly where the problem is.

Eli Bendersky
  • 263,248
  • 89
  • 350
  • 412
  • Hello, I tried by using only `gcc -E` and including fake headers directly in the c code and it works ! Now I can't understand why the `-I`option fails... Also, can you tell me how one can make pycparser parse the "real" system header ? Thanks ! – patatarte Feb 04 '15 at 08:27