Matching strings using Boost Spirit
I am learning Boost Spirit and simply modified an example given in the documentation to match strings instead of doubles. But the code doesn't compile and I get errors which I am unable to debug. Below is the code and the printed errors. Can you please help me debug this problem ?
PS: I am guessing the problem lies in using phoenix::ref for vector string, but not exactly sure how and why.
#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_stl.hpp>
#include <boost/config/warning_disable.hpp>
#include <iostream>
#include <string>
#include <vector>
namespace client
{
namespace qi = boost::spirit::qi;
namespace ascii = boost::spirit::ascii;
namespace phoenix = boost::phoenix;
template <typename Iterator>
bool parse_data(Iterator first, Iterator last, std::vector<std::string>& v)
{
using qi::double_;
using qi::char_;
using qi::phrase_parse;
using qi::_1;
using ascii::space;
using phoenix::ref;
using phoenix::push_back;
bool r = phrase_parse(
first,
last,
+(char_)[push_back(ref(v), _1)],
char_('/')
);
if (first != last)
return false;
return r;
}
}
int
main()
{
std::string str;
while (getline(std::cin, str))
{
if (str.empty())
break;
std::vector<std::string> v;
if(client::parse_data(str.begin(), str.end(), v))
{
std::cout << std::endl << "Parsing done" << std::endl;
std::cout << "Numbers are " ;
for(std::vector<std::string>::iterator i = v.begin(); i < v.end(); i++)
{
std::cout << *i <<" ";
}
std::cout << std::endl;
}
else
{
std::cout << "Parsing Failed" << std::endl;
}
}
return 0;
}
This is the error I get:
/usr/local/include/boost_1_46_1/boost/spirit/home/phoenix/stl/container/container.hpp:492:
error: invalid conversion from ‘const char’ to ‘const char*’
/usr/local/include/b开发者_StackOverflow社区oost_1_46_1/boost/spirit/home/phoenix/stl/container/container.hpp:492:
error: initializing argument 1 of ‘std::basic_string<_CharT, _Traits, _Alloc>::basic_string(const _CharT*, const _Alloc&) [with _CharT = char, _Traits = std::char_traits<char>, _Alloc = std::allocator<char>]’
If you write it as
bool r = phrase_parse(
first, last, +(char_[push_back(ref(v), _1)]), char_('/')
);
it will work. But writing
bool r = phrase_parse(
first, last, +char_, '/', v
);
is even simpler (and runs faster).
精彩评论