Boost :: spirit: several statements in a semantic action block

boost :: phoenix defines statement blocks using the "," operator (see forcing phoenix block statements ). I am trying to use this construct in the semantic part of the boost :: spirit rule. However, it seems that only the last statement in the statement block is executed. The following is a minimal compiled example that shows the problem:

#include <boost/config/warning_disable.hpp> #include <boost/spirit/include/qi.hpp> #include <boost/spirit/include/phoenix_core.hpp> #include <boost/spirit/include/phoenix_operator.hpp> #include <boost/spirit/include/phoenix_fusion.hpp> #include <boost/spirit/include/phoenix_stl.hpp> #include <boost/spirit/include/phoenix_object.hpp> #include <boost/spirit/include/phoenix_operator.hpp> int main() { using boost::spirit::qi::int_; using boost::phoenix::ref; using boost::spirit::qi::phrase_parse; using boost::spirit::ascii::space; int a = 0; int b = 0; const std::string s("1"); bool f = phrase_parse(s.begin(),s.end(), int_[ ref(a)=1, ref(b)=2 ], space); std::cout << f << ": a=" << a << ", b=" << b << std::endl; } 

This program (using boost 1.52) prints

1: a = 0, b = 2

but I was expecting a = 1, b = 2. So should this work? Why?

Thanks!

+4
source share
1 answer

In light of the fact that compiletimes will never use Spirit quickly, I would suggest sticking to "highlevel includes" for utility libraries:

 #include <boost/spirit/include/qi.hpp> #include <boost/spirit/include/phoenix.hpp> int main() { namespace qi = boost::spirit::qi; int a = 0, b = 0; const std::string s("1"); bool f = qi::phrase_parse(s.begin(),s.end(), qi::int_[ boost::phoenix::ref(a)=1, boost::phoenix::ref(b)=2 ], qi::space); std::cout << f << ": a=" << a << ", b=" << b << std::endl; } 

Similarly, I usually suggest boost/fusion/adapted.hpp over boost/fusion/adapted/struct.hpp ea or boost/range/algorithm.hpp .

Your mileage may vary, but the TU that defines Spirit parsers is usually not optimal for compilation times in my projects.

+4
source

All Articles