使用Boost Spirit匹配字符串
Matching strings using Boost Spirit
我正在学习Boost Spirit,并简单地修改了文档中给出的一个示例,以匹配字符串而不是双打。但是代码没有编译,我会遇到无法调试的错误。以下是代码和打印的错误。你能帮我调试这个问题吗?
PS:我猜问题在于使用phoenix::ref作为向量字符串,但不确定如何使用以及为什么使用。
#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;
}
这是我得到的错误:
/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/boost_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>]’
如果你把它写成
bool r = phrase_parse(
first, last, +(char_[push_back(ref(v), _1)]), char_('/')
);
它会起作用的。但是写
bool r = phrase_parse(
first, last, +char_, '/', v
);
更简单(运行速度更快(。
相关文章:
- boost::spirit::karma 替代生成器,带有 boost::variant 由字符串和字符串别名组成
- 你如何从 Boost Spirit X3 词法解析器中获取字符串?
- boost::spirit::x3 中的简单字符串解析器不起作用
- boost.spirit:将字符串对从Qi到X3
- 使用 spirit 将日期时间字符串解析为time_t值
- 如何用boost :: Spirit从字符串中提取多个结构
- Boost Spirit Parser用三个字符串的矢量编译成一个结构,自适应不工作
- boost spirit X3解析器,该解析器将偏移量生成到原始字符串中
- 使用boost::spirit phrase_Parse解析字符串
- Boost.Spirit解析字符串的语义操作不起作用
- 如何使 Boost.Spirit.Lex 令牌值成为匹配序列的子字符串(最好通过正则表达式匹配组)
- Boost::Spirit 在尝试解析带引号的字符串文字时编译失败
- 使用 boost::spirit 解析带引号的字符串
- 使用boost::spirit将字符串解析为列表
- boost::spirit:如何编写一个解析器来解析 2 个字符串并将它们'combined'为一个字符串?
- Boost Spirit(X3)符号表产生UTF8字符串
- 解析字符串(带空格),但忽略(Spirit)末尾的空格
- Boost::spirit::qi解析器不消耗整个字符串
- 对于c风格字符串,boost::spirit::qi和qi::alnum会导致模板编译错误
- 在c++中使用json-spirit读取json字符串