[svlug] simple C parser in C/C++/python

Ivan Sergio Borgonovo mail at webthatworks.it
Tue Jun 7 14:30:09 PDT 2016


On 06/07/2016 08:10 PM, Tom Melendez wrote:
> TL;DR: more broad answer
>
> OK, probably not the answer you want, and I hate when people do this to
> me, but - I mention it just in case your requirements aren't "hard" and
> in the interest of building a sustainable solution.

> If you have some influence over the control over those header files and
> you're really only generating getter/setter type functions (I know you
> said C, but you get what I mean), I would suggest Protocol Buffers for this.

> There's a C implementation on GitHub
> <https://github.com/protobuf-c/protobuf-c>, but I haven't tried it.

I know the tool but never used it, it is an interesting suggestion.
But it doesn't solve the problem with the nested includes with the 
#define even if parsing #define is going to be easier than parsing the 
struct. These include come from another program, so I can't change the 
syntax.
Fortunately I've seen there seem to be several libraries to build C/C++ 
preprocessors.

I'm copying an existing C struct used by another library as 
configuration input so I'd prefer to maintain the same struct so that 
"users" (that's mainly me at this time) won't have to learn new stuff, 
but protobuf is enough Cish it may be worth a try.

I was starting to write a python script that contain the C source code 
that could also work as an installer, I'll finish it tomorrow morning.
Weired way to "simplify" the problem but it seems easy and should 
increase maintainability.

I've already written several parser with ply, lex/yacc and considering 
that the struct is not simple and the #define are nested and spread 
across some files I know for sure that parsing them with no help from 
some specialized library is going to take far more time than reasonable.

thanks

-- 
Ivan Sergio Borgonovo
http://www.webthatworks.it




More information about the svlug mailing list