Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Current state of the art for c++ parsers?

I understand that it's a very hard thing to do, what with #ifdef, #define, and templates, but what is the state of the art of c++ parsers (be it open source, or proprietary?).

I mean, for a university project I'm thinking of creating a tool for analysing c++ code bases, but it seems very hard to find a good parser for it.

Should I give up and settle for java parsers? Similarly, what's the state of the art for java parsers? What about c#?

Also, would ripping the parser part of g++ apart from it ever work for the purposes of code analysis, or is it too much effort trying to do so?

like image 371
kamziro Avatar asked Dec 16 '22 14:12

kamziro


2 Answers

You're in luck! Clang just started being able to parse most c++ programs within the last few months: http://clang.llvm.org/ It's one of the few open source parsers actually able to parse most of C++. (Mostly just GCC and CLANG, I hear Oink(?) Can get pretty good sometimes) And it's built to be used as a library by IDEs and the like, even has architecture built to support code rewriting.

There are some proprietary parsers that get the job done, But none of them are really usable without source access.

Regarding ripping apart gcc, That's not very practical for code analysis depending on what you are trying to do, you could use the new plugin architecture to get some usable information out of it, however at a very early level in parsing, it does something called term folding, where the parser itself will optimize out things like "x = x" (A simplistic example) And other aspects of the compiler expects this to happen, so it's not trivial to remove. Thus making gcc nearly useless for anything resembling source rewriting.

like image 160
Arelius Avatar answered Dec 31 '22 20:12

Arelius


For C++ you can use GCC with -fdump-translation-unit & friends option to get AST from it.

See: http://www.manpagez.com/man/1/g++/

If you can compile something by g++ then you can get tree from it.

like image 35
c-smile Avatar answered Dec 31 '22 20:12

c-smile