Sign in to follow this  
Symphonic

Getting a parse tree in C++

Recommended Posts

I've been looking for a way to get a C++ parse tree, I know GCCXML lets you do this, but I don't want to go through installing GCC. I'm using Visual Studio 2008, and I'm comfortable with python, so I searched for Visual C++ Parse Tree, and python C++ parser, and a bunch of other stuff but no luck. Any thoughts? It strikes me as something very useful to have, I'd be surprised if the compiler can't output the parse tree.

Share this post


Link to post
Share on other sites
take a look here: http://synopsis.fresco.org/index.html
havent tried it myself, but it might work for you.

Share this post


Link to post
Share on other sites
Quote:
Original post by Symphonic

It strikes me as something very useful to have, I'd be surprised if the compiler can't output the parse tree.


Except that it's one of the most mundane things you could imagine. C++ grammar is fixed. It doesn't depend on code or anything else.

Parse trees, in case of C++, are mostly useless, since the compilation process is top-down, meaning you get a string of symbols.

Here's a link which may help you parse the source, and do whatever you want with it, but usefulness is next to none. C++ doesn't support introspection, and it's not well organized. Functions, classes and everything else can be removed by compiler, or transformed and re-organized.

And there's an even bigger problem. With all the direct memory access, and the monolithic nature of C++ applications, parse tree really isn't useful for anything at all. Exceptions are custom C++ compilers which add introspection, and possibly reflection.

But long story short - C++ is completely unsuitable for things like this.

PS: why would it be useful?

Share this post


Link to post
Share on other sites
Quote:
Original post by AntheusPS: why would it be useful?

Who said anything about introspection?

The answer is maintenance.

Because declarations are syntax level constructs, it's possible to use the parse tree to create lists of all imported and exported symbols.

If you have a large code base that will not compile (because, for example, one of the core libraries is no longer used and must be replaced by something else), it's helpful to be able to see where in the code certain symbols are referenced, so you know where to focus your refactoring efforts.

Namespaces (and similar things) will break this sort of direct analysis, but that's not a big issue.

Share this post


Link to post
Share on other sites
If you want import/export information you can find that out from the compiled binaries. With MSVC you can use dumpbin with the /imports and /exports command line options. However, parse trees aren't particularly good things for that kind of information, since C++ has a particularly complex name lookup system, just knowing that a function foo() has been called at a given point doesn't tell you which foo will be called, especially with template overloads and ADL.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this