Note that there are some explanatory texts on larger screens.

plurals
  1. POBoost Spirit Qi: Omit element in Kleene Star parser
    text
    copied!<p>I want to parse special constructs and throw the rest away. But I don't want to use a skipper.</p> <p>I want to get a vector of these constructs, so I use a Kleene Star parser as main rule. But, everytime something gets thrown away, a default constructed element is inserted into the vector.</p> <p>Here is a made up example. It just looks for the string <code>Test</code> and throws the rest away, at least this is the plan. But every time the rule <code>garbage</code> succeeds it adds a default constructed item to the vector in the rule <code>all</code>, giving an output of 7 insteat of 1. How can I tell Spirit to just add to the vector if the rule <code>item</code> succeeds?</p> <pre><code>#define BOOST_SPIRIT_USE_PHOENIX_V3 #include &lt;boost/config/warning_disable.hpp&gt; #include &lt;boost/spirit/include/qi.hpp&gt; #include &lt;boost/fusion/adapted/struct.hpp&gt; #include &lt;boost/spirit/include/phoenix.hpp&gt; #include &lt;iostream&gt; #include &lt;string&gt; #include &lt;vector&gt; namespace qi = boost::spirit::qi; struct container { std::string name; bool dummy; }; BOOST_FUSION_ADAPT_STRUCT(::container, (std::string, name) (bool, dummy)) int main() { typedef std::string::const_iterator iterator; qi::rule&lt;iterator, std::vector&lt;container&gt;()&gt; all; qi::rule&lt;iterator, container()&gt; item; qi::rule&lt;iterator, std::string()&gt; string_rule; qi::rule&lt;iterator&gt; garbage; all = *(garbage | item); garbage = qi::char_ - qi::lit("Test"); string_rule = qi::string("Test"); item = string_rule &gt;&gt; qi::attr(true); std::vector&lt;container&gt; ast; std::string input = "blaTestbla"; iterator first = input.begin(); iterator last = input.end(); bool result = qi::parse(first, last, all, ast); if (result) { result = first == last; } if (result) { std::cout &lt;&lt; "Parsed " &lt;&lt; ast.size() &lt;&lt; " element(s)" &lt;&lt; std::endl; } else { std::cout &lt;&lt; "failure" &lt;&lt; std::endl; } } </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