CuriousGeorge
CuriousGeorge

Reputation: 7410

How can I parse C++ to create an AST?

I'm trying to parse C++ code, and create an AST. What I want to do is extract some simple reflection information(class names, member variables and their types, etc..). I don't need to compile the code, or generate binaries. I am looking for the simplest possible way to do this. Ideally, I would like a small parser, in a single static library, with no dependencies.

I've been looking around, and it appears that a Bison parser may be able to do this for me. I've tried to find an open source parser, but all google will give me is C++ wrappers for bison, and not a bison parser for C++. Typing "C++ parser" also fails, by giving results for parsers for everything else, that are written in C++.

Is there an open source project that will do what I need?

Upvotes: 5

Views: 3179

Answers (2)

John Zwinck
John Zwinck

Reputation: 249652

You can use GCC-XML to generate a fairly easy to parse XML representation of most (but not all) C++ code.

Upvotes: 4

perreal
perreal

Reputation: 98118

clang can do this:

clang -Xclang -ast-dump -fsyntax-only test.cc

also see the docs.

Upvotes: 12

Related Questions