2

I need to parse a header file. My goal is to search the specific structure from the header file and extract the values and offsets of the structure variables.

Can any one please suggest the best way for parsing the header file by omitting the comments and how to parse macros from header too?

IPS
  • 81
  • 1
  • 12
  • http://clang.llvm.org/doxygen/group__CINDEX.html – deviantfan Mar 03 '15 at 06:46
  • @deviantfan can you please explain on how to use that API. Because i need to parse the complete header file which consists of more than 100 structures and find the structure which i need. Then i need to read the structure variables and its offset to store it in memory. – IPS Mar 03 '15 at 06:51
  • 1
    @HariPrabhakaran: don't expect the API for a device that deal with the complexity of C++ to be "simple". You are going to have to pay the cost of learning how to use a C++-parsing engine, and along with that goes the cost of learning about the dark corners of C++ in the code you have, and how the APIs handle that kind of stuff. – Ira Baxter Mar 03 '15 at 06:56
  • thanks for your info. But my issue is , i need to read the contents of the header file at the run time. At run time, i need to parse the structures from the header file and store in the memory. Can you please suggest any best way for doing this? – IPS Mar 03 '15 at 07:24
  • @HariPrabhakaran We did younderstand that you want to do it during runtime. Everything we wrote here is for runtime-solutions. If you want someone to do it for you because you don´t want to read how to use the suggested software, you should hire someone. SO is the wrong place for that. – deviantfan Mar 03 '15 at 08:34
  • If Clang is too complicated, [SWIG](http://www.swig.org/) does a halfway decent job with header files (as long as they're simple). Ask it for Common Lisp output and you get something you can easily load as data in an embedded Lisp or Scheme environment (*now you have two problems...*). – Alex Celeste Mar 06 '15 at 14:22

1 Answers1

4

Parsing C++ is tough. You'll likely want to use an existing parser. I know of 4 that are probably useful:

  • Edison Design Group front end
  • Clang's C++ parser
  • DMS Software Reengineering Toolkit and its C++14 front end
  • GCC (via Melt)

Most of these won't "parse" macros; they want to expand them using a preprocessor. So macros and PP conditionals disappear from the parse tree. DMS can do "limited" preprocessing, and collect/keep preprocessor directives and macros found in well structured places and a wide variety of places they commonly occur.

Parsing header files is really tough; they tend to be loaded with conditionals and junk from many previous versions of the software, and idioms from the specific vendor. (MS has some stunningly weird stuff in their headers). Unless you are talking about parsing your header files, make sure you check the tool you choose can handle the dialect of C++ that you are actually handling.

Ira Baxter
  • 93,541
  • 22
  • 172
  • 341
  • thanks for your info. But my issue is , i need to read the contents of the header file at the run time. At run time, i need to parse the structures from the header file and store in the memory. Can you please suggest any best way for doing this? – IPS Mar 03 '15 at 07:21
  • At run time? Then you need to figure out a way to load one of these puppies into your application space after your application starts (I'll bet that's really awkward), graft your application *into* of these (more likely), or invoke one of these as a subprocess on demand. What is it you are trying to accomplish that requires reading this data at runtime? – Ira Baxter Mar 03 '15 at 11:25