Hannibal, a partial C++ grammar

This page contains links to some files that are part of a more or less abandoned project. Most notably, there is a spirit grammar that implements around 120 of the approximately 250 grammar rules as they can be found in The C++ Programming Language, Third Edition. The 120 rules allow a C++ source file to be parsed for all type information and declarations. In fact this grammar parses as good as anything, but ignores function bodies.

The grammar uses Wave (version 1.0.3, the pre-boost version) as preprocessor. Part of the grammar itself (the const_expression part, some 14 rules) has been copied from Wave and some files from the Wave driver program were used to create a working executable.

The grammar is highly untested. I just ran a few pre-processed C++ files that I happened to have on my disk.

History, Planning

The grammar was part of a feasibility study (that I performed privately) for a C++ reflection framework. It was intended to answer queries like 'give me all members of class X and their types'. I abandoned this approach in favour of simpler solutions after wrapping up the grammar.

As it is, this grammar just creates a syntax-tree of a parsed file. It may be a good starting point for a grammar that can be used for things like reverse engineering as some UML modelling tools do. Or whatever use you may find for a grammar that gives you a list of all classes in a file and their members.

I intend to return to this project one day. That day is not today.

Related work

I've only skimmed through other products that do something similar (and a lot more).


The grammar may not work in your environment. It was based on Wave, version 1.0.3, which is the pre-boost version of Wave. As a consequence all include statements in my C++ files refer to header files in the form "wave/..." instead of "boost/wave/...". Make sure your include path is set to cope with this. Additionally, the grammar has not been tested with Wave version 2 (has anybody referred to that as 'the second wave' yet?).


Zip file with a developer studio 2003 solution file and sources (23 k). The grammar is in translation_unit_parser.h