Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I could solve the problem for this particular case (in fact <a href="http://boost.2283326.n4.nabble.com/struct-derived-struct-fusion-adapted-and-the-sequence-operator-tc4259090.html" rel="noreferrer">we discussed options on the list</a>), but really, this kind of 'enigmatic' error creeps up more often with Boost Spirit and it would be nice to get a handle on the general class of problems.</p> <p>Your first resource should be the <em>excellent</em> spirit documentation, which details exactly what the synthesized attribute will be for a given parser primitive, operator or directive. See <a href="http://www.boost.org/doc/libs/1_48_0/libs/spirit/doc/html/spirit/qi/reference.html" rel="noreferrer">the Reference section</a> to <a href="http://boost-spirit.com/home/doc/" rel="noreferrer">Spirit Qi Docs</a>.</p> <p>In some cases, I have taken to shifting the focus from 'trying to pry the information from the compiler error list' to 'actively querying Spirit for the types it passes'. The technique I use for this is the Polymorphic Callable Type (see Spirit/Fusion docs). </p> <p>Here is one that uses GCC specific APIs to <em>pretty [sic] print</em> the types it detects:</p> <h2>Functor <code>what_is_the_attr</code></h2> <pre><code>#include &lt;cxxabi.h&gt; #include &lt;stdlib.h&gt; #include &lt;string&gt; #include &lt;iostream&gt; template &lt;typename T&gt; std::string nameofType(const T&amp; v) { int status; char *realname = abi::__cxa_demangle(typeid(v).name(), 0, 0, &amp;status); std::string name(realname? realname : "????"); free(realname); return name; } struct what_is_the_attr { template &lt;typename&gt; struct result { typedef bool type; }; template &lt;typename T&gt; bool operator()(T&amp; attr) const { std::cerr &lt;&lt; "what_is_the_attr: " &lt;&lt; nameofType(attr) &lt;&lt; std::endl; return true; } }; </code></pre> <h2>Sample use: detect synthesized attribute type</h2> <p>You can use it to detect exactly what type the synthesized attribute type of a parser expression actually ends up being:</p> <pre><code>template &lt;typename Exp&gt; void detect_attr_type(const Exp&amp; exp) { using namespace boost::spirit::qi; const char input[] = "1 2 3 4"; auto f(std::begin(input)), l(std::end(input)-1); bool dummy = phrase_parse( f, l, exp [ what_is_the_attr() ], space); } </code></pre> <blockquote> <p>(<strong>Note:</strong> <em>this shows a limitation of the approach - the technique assumes you have an 'otherwise' working grammar and you know how to pass an input that satisfies the expression enough to trigger the semantic action. In most cases, this will be true when you are hacking on your Spirit parser, though</em>)</p> </blockquote> <p>Let's test it. E.g. let's see what the difference is between and expression of medium complexity, and the same wrapped inside a <code>qi::raw[]</code> directive:</p> <pre><code>int main() { detect_attr_type( -(int_ &gt;&gt; *int_) ); detect_attr_type( raw [ -(int_ &gt;&gt; *int_) ] ); } </code></pre> <p>Output:</p> <pre><code>what_is_the_attr: boost::optional&lt;boost::fusion::vector2&lt;int, std::vector&lt;int, std::allocator&lt;int&gt; &gt; &gt; &gt; what_is_the_attr: boost::iterator_range&lt;char const*&gt; </code></pre> <p>At the bottom, we will apply this to the question in the OP.</p> <h2>Sample use: detecting types passed into Semantic Actions</h2> <p>We could use the same unary function object (<code>what_is_the_attr</code>) to detect these, however, semantic actions can take any number of arguments, so we need to generalize. This would be tedious work, if it weren't for <strong><em>variadic template</em></strong> (woot! for c++0x):</p> <pre><code>struct what_are_the_arguments { template &lt;typename...&gt; struct result { typedef bool type; }; template &lt;typename... T&gt; bool operator()(const T&amp;... attr) const { std::vector&lt;std::string&gt; names { nameofType(attr)... }; std::cerr &lt;&lt; "what_are_the_arguments:\n\t"; std::copy(names.begin(), names.end(), std::ostream_iterator&lt;std::string&gt;(std::cerr, "\n\t")); std::cerr &lt;&lt; '\n'; return true; } }; </code></pre> <p>Repeated for the above test cases reveals that Spirit actually tries to call the semantic action with <em>three arguments</em> if possible (as <a href="http://www.boost.org/doc/libs/1_48_0/libs/spirit/doc/html/spirit/qi/reference/action.html#spirit.qi.reference.action.expression_semantics" rel="noreferrer">documented</a>):</p> <pre><code>what_are_the_arguments: boost::optional&lt;boost::fusion::vector2&lt;int, std::vector&lt;int, std::allocator&lt;int&gt; &gt; &gt; &gt; boost::spirit::unused_type bool what_are_the_arguments: boost::iterator_range&lt;char const*&gt; boost::spirit::unused_type bool </code></pre> <p>But the nice thing is, that you can now apply this to any semantic action:</p> <pre><code>template &lt;typename ExpWSA&gt; void test(const ExpWSA&amp; exp) { const char input[] = "1 2 3 4"; auto f(std::begin(input)), l(std::end(input)-1); qi::phrase_parse(f, l, exp, qi::space); } int main() { test(-(-double_ &gt;&gt; *int_) [ phx::bind(what_are_the_arguments(), _1, _2, _0, phx::ref(std::cout), 42) ]); } </code></pre> <p>Printing, for this (sorry) very contrived example:</p> <pre><code>what_are_the_arguments: boost::optional&lt;double&gt; std::vector&lt;int, std::allocator&lt;int&gt; &gt; boost::fusion::vector2&lt;boost::optional&lt;double&gt;, std::vector&lt;int, std::allocator&lt;int&gt; &gt; &gt; std::ostream int </code></pre> <h2>Applied to the OP</h2> <p>The synthesized attribute of the <code>derived</code> rule is <em>not</em> the same as for <code>int_&gt;&gt;int_&gt;&gt;int_&gt;&gt;int_</code>:</p> <pre><code>auto base_expr = int_ &gt;&gt; int_; // avoids assigning to struct attribute rule&lt;const char*, mybase(), space_type&gt; base_ = base_expr; test(base_ &gt;&gt; int_ &gt;&gt; int_ [ what_is_the_attr() ] ); test(base_expr &gt;&gt; int_ &gt;&gt; int_ [ what_is_the_attr() ] ); </code></pre> <p>Will print</p> <pre><code>what_is_the_attr: boost::fusion::vector3&lt;mybase, int, int&gt; what_is_the_attr: boost::fusion::vector4&lt;int, int, int, int&gt; </code></pre> <p>There is your problem. We discussed some workarounds based on this diagnostic in the <a href="http://boost.2283326.n4.nabble.com/struct-derived-struct-fusion-adapted-and-the-sequence-operator-tc4259090.html" rel="noreferrer">original thread</a> (and see the other answers here). But this post should help answering the general case question.</p> <h1>Full code listing</h1> <p>In integrated form, compiled with gcc 4.6.1 --std=c++0x and boost 1_48:</p> <pre><code>#include &lt;cxxabi.h&gt; #include &lt;iostream&gt; #include &lt;iterator&gt; #include &lt;stdlib.h&gt; #include &lt;string&gt; #include &lt;vector&gt; template &lt;typename T&gt; std::string nameofType(const T&amp; v) { int status; char *realname = abi::__cxa_demangle(typeid(v).name(), 0, 0, &amp;status); std::string name(realname? realname : "????"); free(realname); return name; } struct what_is_the_attr { template &lt;typename&gt; struct result { typedef bool type; }; template &lt;typename T&gt; bool operator()(T&amp; attr) const { std::cerr &lt;&lt; "what_is_the_attr: " &lt;&lt; nameofType(attr) &lt;&lt; std::endl; return true; } }; struct what_are_the_arguments { template &lt;typename...&gt; struct result { typedef bool type; }; template &lt;typename... T&gt; bool operator()(const T&amp;... attr) const { std::vector&lt;std::string&gt; names { nameofType(attr)... }; std::cerr &lt;&lt; "what_are_the_arguments:\n\t"; std::copy(names.begin(), names.end(), std::ostream_iterator&lt;std::string&gt;(std::cerr, "\n\t")); std::cerr &lt;&lt; '\n'; return true; } }; #include &lt;boost/fusion/adapted.hpp&gt; #include &lt;boost/spirit/include/phoenix.hpp&gt; #include &lt;boost/spirit/include/qi.hpp&gt; struct mybase { int a,b; }; struct myderived : mybase { int c,d; }; BOOST_FUSION_ADAPT_STRUCT(mybase, (int,a)(int,b)); BOOST_FUSION_ADAPT_STRUCT(myderived, (int,a)(int,b)(int,c)(int,d)); template &lt;typename ExpWSA&gt; void test(const ExpWSA&amp; exp) { using namespace boost::spirit::qi; const char input[] = "1 2 3 4"; auto f(std::begin(input)), l(std::end(input)-1); bool dummy = phrase_parse(f, l, exp, space); } int main() { using namespace boost::spirit::qi; // Diagnostics for the OP case auto base_expr = int_ &gt;&gt; int_; // avoids assigning to struct attribute rule&lt;const char*, mybase(), space_type&gt; base_ = base_expr; // Derived rule, different formulations test((base_ &gt;&gt; int_ &gt;&gt; int_) [ what_is_the_attr() ] ); test((base_expr &gt;&gt; int_ &gt;&gt; int_) [ what_is_the_attr() ] ); // Applied to attribute types test(raw [ -(int_ &gt;&gt; *int_) ] [ what_is_the_attr() ] ); test(-(int_ &gt;&gt; *int_) [ what_is_the_attr() ] ); // applied to semantic actions - contrived example namespace phx = boost::phoenix; test(-(-double_ &gt;&gt; *int_) [ phx::bind(what_are_the_arguments(), _1, _2, _0, phx::ref(std::cout), 42) ]); return 0; } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload