title: Parsing a grammar with Boost Spirit
body: <p>I am trying to parse a C-function like tree expressions like the following (using the <a href="http://en.wikipedia.org/wiki/Spirit_Parser_Framework" rel="noreferrer">Spirit Parser Framework</a>):</p>
  
  <pre><code>F( A() , B( GREAT( SOME , NOT ) ) , C( YES ) )
  </code></pre>
  
  <p>For this I am trying to use the three rules on the following grammar:</p>
  
  <pre><code>template&lt; typename Iterator , typename ExpressionAST &gt;
  struct InputGrammar : qi::grammar&lt;Iterator, ExpressionAST(), space_type&gt; {
  
      InputGrammar() : InputGrammar::base_type( ) {
         tag = ( qi::char_("a-zA-Z_")  &gt;&gt; *qi::char_("a-zA-Z_0-9") )[ push_back( at_c&lt;0&gt;(qi::_val) , qi::_1 ) ];
         command =  tag [ at_c&lt;0&gt;(qi::_val) = at_c&lt;0&gt;(qi::_1) ] &gt;&gt; "(" &gt;&gt; (*instruction &gt;&gt; ",")
                                          [ push_back( at_c&lt;1&gt;(qi::_val) , qi::_1 ) ]  &gt;&gt; ")";
         instruction = ( command | tag ) [qi::_val = qi::_1];
      }
      qi::rule&lt; Iterator , ExpressionAST() , space_type &gt; tag;
      qi::rule&lt; Iterator , ExpressionAST() , space_type &gt; command;
      qi::rule&lt; Iterator , ExpressionAST() , space_type &gt; instruction;
  };
  </code></pre>
  
  <p>Notice that my tag rule just tries to capture the identifiers used in the expressions (the 'function' names). Also notice that the signature of the tag rule returns a <code>ExpressionAST</code> instead of a <code>std::string</code>, like in most examples. The reason I want to do it like this is actually pretty simple: I hate using variants and I will avoid them if possible. It would be great to keep the cake and eat it too I guess.</p>
  
  <p>A command should start with a tag (the name of the current node, first string field of the AST node) and a variable number of arguments enclosed by parentheses, and each of the arguments can be a tag itself or another command.</p>
  
  <p>However, this example does not work at all. It compiles and everything, but at run time it fails to parse all my test strings. And the thing that really annoys me is that I can't figure how to fix it, since I can't really debug the above code, at least in the traditional meaning of the word. Basically the only way I see I can fix the above code is by knowing what I am doing wrong.</p>
  
  <p>So, the question is that I don't know what is wrong with the above code. How would you define the above grammar?</p>
  
  <p>The <code>ExpressionAST</code> type I am using is:</p>
  
  <pre><code>struct MockExpressionNode {
      std::string name;
      std::vector&lt; MockExpressionNode &gt; operands;
  
      typedef std::vector&lt; MockExpressionNode &gt;::iterator iterator;
      typedef std::vector&lt; MockExpressionNode &gt;::const_iterator const_iterator;
  
      iterator begin() { return operands.begin(); }
      const_iterator begin() const { return operands.begin(); }
      iterator end() { return operands.end(); }
      const_iterator end() const { return operands.end(); }
  
      bool is_leaf() const {
          return ( operands.begin() == operands.end() );
      }
  };
  
  BOOST_FUSION_ADAPT_STRUCT(
      MockExpressionNode,
      (std::string, name)
      (std::vector&lt;MockExpressionNode&gt;, operands)
  )
  </code></pre>