0

So I have successfully used the calculator example to speed a compilation of a relatively simple grammar up by about 50% (?). The calculator example can be found here. However, there was one thing in the example, which I didn't really grasp, namely calc6c.cpp:

#include "calc6c.hpp"

// This is not really called. Its only purpose is to
// instantiate the constructor of the grammar.
void instantiate_statement()
{
    typedef std::string::const_iterator iterator_type;
    std::vector<int> code;
    statement<iterator_type> g(code);
}

I find the comment really confusing, especially, since when I tried it in my code (using my grammar) it didn't matter if such a function was included or not. I could successfully compile with and without this compilation unit. It seems to me the grammar is instantiated on ll. 74 in calc6.cpp:

typedef std::string::const_iterator iterator_type;
typedef statement<iterator_type> statement;

vmachine mach;                  //  Our virtual machine
std::vector<int> code;          //  Our VM code
statement calc(code);           //  Our grammar

So why is the function *instantiate_statement* needed at all? Or rather: What is the difference between having a compilation unit that contains the *instantiate_statement* function as opposed to not having such a compilation unit when compiling the final program?

Also, I looked far and wide, but there doesn't seem to be a page covering this example -- or, for that matter, a more generic example of splitting up a grammar into multiple compilation units -- in greater detail and the example is completely gone in more recent incarnations of the Boost::Spirit documentation.

FRob
  • 3,883
  • 2
  • 27
  • 40
  • Look at the other compiler examples. The conjure examples, in particular, is a terrific example of how to organize your code. http://www.boost.org/doc/libs/1_53_0/libs/spirit/example/qi/compiler_tutorial/ – sehe Jul 01 '13 at 16:46
  • 1
    Hi, thanks for your comment. I had focused exclusively on the calculator, because it seemed easier to understand. As I see it, the mysterious statement is also gone in the conjure example :) – FRob Jul 09 '13 at 01:34

0 Answers0