tags:

views:

133

answers:

4

I recently took a class at school where we had to learn Scheme to build a parser for a simple made up scheme-like language. As the semester went on, we added to our parser to make it more and more interesting.

Since then, on my own time, I've started writing my own parser that's quite a bit neater than what I did in class, but it parses my C++ code, gathers a list of all the data structures and public members, and generates a recursive descent parser for it. For example, if I have the the following

class Bar
{
public: 
    int a;
}

class Foo
{
public: 
    Bar* myBar;
    int x;
}

and I run my parser generator on it, it spits out a new class simply called Parser that I can use like this to read from a file:

Parser p;
Foo* f = p.parseFoo("example.txt");

where example.txt would be something like this:

Foo
{
    myBar
    {
        a 5
    }
    x 10
}

(This is just a simple example, there are some other neat things too like recognizing when it should be push_back-ing onto a vector, and being able to assign function callbacks)

This seems like the type of thing that other (probably smarter) people ought to have done before me. I did some quick Google searches to see what was out there, but there's a lot of stuff to sift though. So my question is this: are there tools out there that do what my parser generator does right now? Is this project worth continuing* or are there better tools out there already?

*of course it's always worth continuing as a learning experience. Rather, I mean from the point of view as a user who would want to read data structures to and from text.

+2  A: 

lex and yacc (or rather, flex and bison) are powerful tools that will help you generate parsers for regular languages with ease.

Can Berk Güder
+2  A: 

Boost.Spirit is very impressive and useful.

Documentation on this can be found here

But be aware that parsing C++ is really not easy.

Benoît
My current program is designed to only parse through the public members of classes, which it handles fine as long as your code is well formed and you don't use any macros. It even handles inheritance properly.
Alex
Even when only considering public members of classes, there are many caveats to think of. For instance : namespaces, template instantiations, functions pointers.
Benoît
A: 

lex/yacc or flex/bison.
or Boost.Spirit (http://spirit.sourceforge.net/).

bb
+1  A: 

Qt has a free tool out there, only a little bit of documentation but it does not have a lot of overhead. I have used it to create a parser for a full blown DSL. QLALR

Harald Scheirich