title: How is it possible to pass attributes to child rules in boost spirit karma
body: <p>I am parsing text into an AST via qi and generates text again via karma. This is working as expected, but wants some method to pass on an attribute from one rule to another.</p>
  
  <p><strong>Ported from the comments</strong>:</p>
  
  <p><kbd><a href="http://coliru.stacked-crooked.com/a/68becc156e4bee4b" rel="nofollow">Current Code On Coliru</a></kbd></p>
  
  <pre><code>#include &lt;boost/spirit/include/qi.hpp&gt;
  #include &lt;boost/spirit/include/karma.hpp&gt;
  
  #include &lt;boost/fusion/include/adapt_struct.hpp&gt;
  #include &lt;boost/fusion/include/adapt_adt.hpp&gt;
  #include &lt;fstream&gt;
  
    struct c_struct
    {
      int value1;
    };
  
    struct b_struct
    {
      std::string value1;
      std::vector&lt; c_struct &gt; value2;
    };
  
    struct a_struct
    { 
      std::string value1;
      std::vector&lt; b_struct &gt; value2;
    };
  
  BOOST_FUSION_ADAPT_STRUCT( c_struct,    
      (int, value1)
  )
  
  BOOST_FUSION_ADAPT_STRUCT( b_struct,    
      (std::string, value1)
      (std::vector&lt; c_struct &gt;, value2)
  )
  
  BOOST_FUSION_ADAPT_STRUCT( a_struct,    
      (std::string, value1)
      (std::vector&lt; b_struct &gt;, value2)
  )
  
  using namespace boost::spirit;
  using namespace boost::spirit::qi;
  using namespace boost::spirit::karma;
  using namespace boost::spirit::ascii;
  
  template &lt;typename Iterator&gt;
  struct grammarB : karma::grammar&lt;Iterator, a_struct()&gt;
  {
    grammarB() : grammarB::base_type(ruleA)
    {
      ruleA %= karma::string &lt;&lt; karma::lit(' ') &lt;&lt; +ruleB &lt;&lt; eps;
      ruleB %= karma::string &lt;&lt; +ruleC &lt;&lt; karma::lit(' ') &lt;&lt; eps;    
      ruleC %= lit("  -&gt;  ") &lt;&lt; karma::int_;
    }
  
    karma::rule&lt;Iterator, a_struct()&gt; ruleA;
    karma::rule&lt;Iterator, b_struct()&gt; ruleB;
    karma::rule&lt;Iterator, c_struct()&gt; ruleC;
  };
  
  template &lt;typename Iterator&gt;
  struct grammarA : qi::grammar&lt;Iterator, a_struct(), boost::spirit::ascii::space_type&gt;
  {
    grammarA() : grammarA::base_type(ruleA)
    {
      ruleA %= ruleString &gt;&gt; omit[+qi::char_('.')] &gt;&gt; +ruleB;
      ruleB %= ruleString &gt;&gt; omit[qi::char_(',')] &gt;&gt; (ruleC % qi::char_(',')) &gt;&gt; omit[qi::char_(';')];
      ruleC %= qi::int_;
  
      ruleString %= +qi::char_("a-z");
    }           
    qi::rule&lt;Iterator, a_struct(), boost::spirit::ascii::space_type&gt; ruleA;   
    qi::rule&lt;Iterator, b_struct(), boost::spirit::ascii::space_type&gt; ruleB;  
    qi::rule&lt;Iterator, c_struct(), boost::spirit::ascii::space_type&gt; ruleC;
  
    qi::rule&lt;Iterator, std::string(), boost::spirit::ascii::space_type&gt; ruleString;
  };    
  
  int main(int argc, char **argv)
  {   
    std::string storage("parent ... whee,4,5,6;ahhhh,5,6;"); // We will read the contents here.
  
    typedef grammarA&lt;std::string::const_iterator&gt; grammarA_t;
    grammarA_t grammar;
    a_struct ast;
  
    std::string::const_iterator iter = storage.begin();
    std::string::const_iterator end = storage.end();
  
    bool r = phrase_parse(iter, end, grammar, boost::spirit::ascii::space, ast);
  
    if (r &amp;&amp; iter == end)
    {    
      std::cout &lt;&lt; "Parsing succeeded" &lt;&lt; std::endl;
  
      typedef std::back_insert_iterator&lt;std::string&gt; output_iterator_type;
  
      std::string generated;    
      output_iterator_type sink(generated);
  
      typedef grammarB&lt;output_iterator_type&gt; grammarB_t; 
      grammarB_t generator;
  
  
      if ( generate(sink, generator, ast) )
          std::cout &lt;&lt; generated &lt;&lt; std::endl;
      else
          std::cout &lt;&lt; "fail" &lt;&lt; std::endl;
    }
  
    return 0;
  }
  </code></pre>
  
  <p>It prints</p>
  
  <pre><code>Parsing succeeded
  parent whee  -&gt;  4  -&gt;  5  -&gt;  6 ahhhh  -&gt;  5  -&gt;  6 
  </code></pre>
  
  <p>But I'd prefer it to print</p>
  
  <pre><code>Parsing succeeded
  parent parent whee -&gt; parent 4 -&gt; parent 5 -&gt; parent 6 ahhhh -&gt; parent 5 -&gt; parent 6
  </code></pre>
  
  <p>Is this possible, and how?</p>