I've tried to use boost semantic actions. In my case boost::bind
was the easiest solution. The first example is working well; here I'm using only one arguments in semantic action.
#include <boost/config/warning_disable.hpp>
#include <boost/spirit/include/qi.hpp>
#include <boost/bind.hpp>
#include <iostream>
namespace qi = boost::spirit::qi;
// A plain function
void print(int const& i)
{
std::cout << i << std::endl;
}
int main()
{
using boost::spirit::qi::int_;
using boost::spirit::qi::parse;
char const *first = "{44}", *last = first + std::strlen(first);
parse(first, last, '{' >> int_[boost::bind(&print, _1)] >> '}');
return 0;
}
I've tried to expand my code. In the second case I want to pass two arguments to the bound function, but the compiler won't compile this code. What does fail? I haven't find any example. The second code is here:
#include <boost/config/warning_disable.hpp>
#include <boost/spirit/include/qi.hpp>
#include <boost/bind.hpp>
#include <iostream>
namespace qi = boost::spirit::qi;
// A plain function
void print(int const& i1, int const& i2)
{
std::cout << i1 << "," << i2 << std::endl;
}
int main()
{
using boost::spirit::qi::int_;
using boost::spirit::qi::parse;
char const *first = "{44,55}", *last = first + std::strlen(first);
parse(first, last, '{' >> (int_ >> "," >> int_)[boost::bind(&print, _1,_2)] >> '}');
return 0;
}
You cannot compile this code because there is only one input parameter - boost::fusion::vector < int, int > - formed by (int_ >> "," >> int_) sequence. Try this
#include <boost/config/warning_disable.hpp>
#include <boost/spirit/include/qi.hpp>
#include <boost/bind.hpp>
#include <iostream>
namespace qi = boost::spirit::qi;
// A plain function
void print(boost::fusion::vector < int, int > arg_)
{
std::cout << boost::fusion::at_c < 0 > (arg_) << "," << boost::fusion::at_c < 1 > (arg_) << std::endl;
}
int _tmain(int argc, _TCHAR* argv[])
{
using boost::spirit::qi::int_;
using boost::spirit::qi::parse;
char const *first = "{44,55}", *last = first + std::strlen(first);
parse(first, last, '{' >> (int_ >> "," >> int_)[boost::bind(&print, _1)] >> '}');
return 0;
}