2

I use pycparser to parse c code and for my test files it works fine. I had some problems with the cpp and the header files so I use a external cpp and give the pycparser the output of the preprocessor. With my test files it all works but with files from a real software projects I get an error.

ast = parse_file("layer2.c.o", use_cpp=False)

Error message:

  File "/usr/local/lib/python2.7/dist-packages/pycparser/c_parser.py", line 1613, in p_error
column=self.clex.find_tok_column(p)))
  File "/usr/local/lib/python2.7/dist-packages/pycparser/plyparser.py", line 54, in _parse_error
    raise ParseError("%s: %s" % (coord, msg)) raise ParseError("%s: %s" % (coord, msg))

pycparser.plyparser.ParseError: /usr/include/stdint.h:58:1: before: __extension__

Does anybody knows this problem?

Rod
  • 52,748
  • 3
  • 38
  • 55
harald
  • 21
  • 4

1 Answers1

2

See the FAQ about __extension__. In general, I see you're trying to parse included standard headers. Your life will be considerably easier if you use the "fake" C headers that come with pycparser instead. Details here.

Eli Bendersky
  • 263,248
  • 89
  • 350
  • 412
  • It works fine with fake headers for _fake_defines.h and _fake_typedefs.h. All other header files in the project that are also necessary causes in problems. My first workaraound was to create a fake header file for each header file in the project but some typedefs are not available then. I can copy them into the _fake_typdefs.h but when i change to another Project i always have to change the file. The project also uses the socket library and some defines from there are not available in the fake headers. I am only interested to find out in which file and line a specific function is called. – harald Dec 05 '13 at 13:24
  • @harald: there is no way around telling pycparser which identifiers are types. You can extend the fake headers for your own project, or have a set per-project. – Eli Bendersky Dec 05 '13 at 13:49
  • Due to the fact that i am not interested in the information about data types or anything else. Is it possible to modify the parser so that he ignores such errors? When I delete a typedef for testing I get the following message: File "/usr/local/lib/python3.2/dist-packages/pycparser/plyparser.py", line 54, in _parse_error raise ParseError("%s: %s" % (coord, msg)) – harald Dec 06 '13 at 10:56
  • @harald: this article should help you understand the problem faced by the parser here: http://eli.thegreenplace.net/2011/05/02/the-context-sensitivity-of-c%E2%80%99s-grammar-revisited/ – Eli Bendersky Dec 06 '13 at 13:01
  • Thanks. Do you know any other parser who is able to avoid this problem or is the only possibility to write a parser by my self? – harald Dec 06 '13 at 13:36
  • @harald: i'm not sure you understand the problem fully. the parser needs to know which identifiers are types - this changes the whole parse tree completely. you can't just tell it that all ids are types. you can teach it that a given set of ids are types, and extract those somehow from the code base you want. – Eli Bendersky Dec 06 '13 at 14:33