Library for parsing type C declarations?

I'm trying to find an open source library (written in C

or C++

) to help me parse arbitrary type declarations C/C++

(like type declarations void *(*(*foo[])( int, void * [] ))[123]

) which I will then metamodel.

Can anyone recommend an open source parsing library C/C++

?

Note. I cannot use the GPL licensed code (like a tool cdecl

) as it is a closed source application and I am hoping for a more robust solution that I can write myself.

+3


source to share


1 answer


Parsing C is notoriously heavy, and in order to do what you want, you probably need name and type resolution. You need to have what constitutes the complete end of the compiler to do this right; realistically, you even need a preprocessor, because the code you read will contain preprocessor directives. These devices are a huge job to combine; you're right that you really don't want to write yourself if you don't have free time.

Your realistic choices are open source packages such as GCC (difficult to reverse for its task), GCCXML (wants to create declarations from well-formed programs), or Clang (similar); Eclipse CDT has a C parser (similarly). I can't tell if they have APIs or licenses that satisfy you. Closed source packages can be more tailored to your needs; EDG provides C and C ++ interfaces (still only wants to handle fully-formed programs) and also my company (Semantic Designs) through our DMS Software Reengineering Toolkit.



Of this set, only the DMS is likely to make it easy to parse type declarations in isolation; it can parse any nonterminal of its grammars (even including ambiguous ones ). More importantly, if you really want to "go to metamode", you will most likely want to parse the declaration and determine the types of subtypes / reference types (for example, if it uses a typedef), so you want to combine full parsing with name resolution / type to define background definitions, followed by parsing of the specific ads you care about, then resolving the names and types of specific ads using the symbol table context provided by the background definitions. The DMS engine provides all of these capabilities.

+1


source







All Articles