I've been working with ANTLR 2.7.5 (I haven't gotten around to building 2.7.6 yet) and CPP_grammarV3.1 from the ANTLR website, with some good success. Out-of-the-box, it'll correctly parse a GCC-preprocessed file if it doesn't use any GCC extensions or built-in types (i.e. didn't include any standard headers); most of my time has been spent getting to know CPP_parser.g to figure out where and how I should add them. After adding support for most gcc __attribute__ specifiers and adding __builtin_va_list to the basic types, I came VERY close to successfully parsing Quadratic.cpp which includes two standard headers. Of the few remaining errors, most are related to the lack of support for "using" declarations when doing AST resolution lookup, so right now I've moved back to building a custom AST container which I'll drop in before I correct them.
After reading all the installation and usage instructions, only one step wasn't completely obvious, which was that I had to rebuild libantlr.a rather than using the pre-packaged version. Basically,
- Make sure you have MinGW and MSys installed (MSys is only needed to build the antlr library; it's fully MinGW32 compatible after this)
- Download and unzip antlr-x.y.z.tar.gz
- Open MSys, go to the antlr directory, run "./configure --disable-examples" (I added "--prefix=/path/to/antlr-dir" because I wasn't sure where the default was)
- Run "make install"
- If you want to be able to run antlr from the Windows command prompt, also download antlr-x.y.z.exe
- Download and unzip CPP_parserV3.1.zip
- If you want extended trace functionality, copy LLkParser.hpp to the antlr include/antlr directory; otherwise, comment out all references to antlrTrace() in CPP_parser.g
- Run antlr on CPP_parser.g to generate CPPParser.(cpp,hpp), CPPLexer.(cpp,hpp), and STDCTokenTypes.(hpp,txt)
- Compile CPPParser.cpp, CPPLexer.cpp, Dictionary.cpp, LLkParser.cpp (if you want extended trace), and Support.cpp into whatever project you want to use the parser in
- For the test project included with the parser, also compile Main.cpp and MyCode.cpp with MYCODE #defined
That was all from memory, but any problems you run into should be trivial. If you compile the test project, you can run the resulting executable on any preprocessed code (use gcc -E) to get a list of defined functions and optionally a list of declarations.
Hope that helps,
Twilight Dragon