使用 boost::spirit 解析双打列表
这是一个代码示例。
// file temp.cpp
#include <iostream>
#include <vector>
#include <boost/spirit/include/qi.hpp>
namespace qi = boost::spirit::qi;
struct parser : qi::grammar<std::string::const_iterator, std::vector<double> >
{
parser() : parser::base_type( vector )
{
vector = +qi::double_;
}
qi::rule<std::string::const_iterator, std::vector<double> > vector;
};
int main()
{
std::string const x( "1 2 3 4" );
std::string::const_iterator b = x.begin();
std::string::const_iterator e = x.end();
parser p;
bool const r = qi::phrase_parse( b, e, p, qi::space );
// bool const r = qi::phrase_parse( b, e, +qi::double_, qi::space ); // this this it PASSES
std::cerr << ( (b == e && r) ? "PASSED" : "FAILED" ) << std::endl;
}
我想用 parser
p
解析 std::string
x
。
从struct parser
的定义来看,行
qi::phrase_parse( b, e, p, qi::space ); // PASSES
和
qi::phrase_parse( b, e, +qi::double_, qi::space ); // FAILS
应该是等效的。但是,第一个解析失败,第二个解析通过。
我在 struct parser 的定义中做错了什么?
Here is a code sample.
// file temp.cpp
#include <iostream>
#include <vector>
#include <boost/spirit/include/qi.hpp>
namespace qi = boost::spirit::qi;
struct parser : qi::grammar<std::string::const_iterator, std::vector<double> >
{
parser() : parser::base_type( vector )
{
vector = +qi::double_;
}
qi::rule<std::string::const_iterator, std::vector<double> > vector;
};
int main()
{
std::string const x( "1 2 3 4" );
std::string::const_iterator b = x.begin();
std::string::const_iterator e = x.end();
parser p;
bool const r = qi::phrase_parse( b, e, p, qi::space );
// bool const r = qi::phrase_parse( b, e, +qi::double_, qi::space ); // this this it PASSES
std::cerr << ( (b == e && r) ? "PASSED" : "FAILED" ) << std::endl;
}
I want to parse std::string
x
with parser
p
.
As follows from the definition of struct parser
, the lines
qi::phrase_parse( b, e, p, qi::space ); // PASSES
and
qi::phrase_parse( b, e, +qi::double_, qi::space ); // FAILS
should be equivalent. However, the with first one parsing fails and with the second one it passes.
What am I doing wrong at the definition of struct parser
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您应该“告知”有关跳过空格的语法 - 模板中的另一个参数。
我还做了一些小修正,例如您应该在参数中添加括号,它告诉属性类型:
std::vector()
。You should "inform" grammar about skipping spaces - one more argument in template.
I've also done few small corrections e.g. you should add brackets in arguments, which tells about attribute type:
std::vector<double>()
.