使用boost::phoenix::bind结合boost::spirit::qi::symbols::add
Using boost::phoenix::bind together with boost::spirit::qi::symbols::add
我想解析文本文件中的浮点数,并将其插入符号表中;解析器和符号表由spirit::qi.
提供。下面是我的代码:#include <boost/spirit/include/qi.hpp>
#include <boost/spirit/include/phoenix.hpp>
#include <stdint.h>
#include <iostream>
#include <string>
template<typename VTYPE>
struct VTable : boost::spirit::qi::symbols<char, VTYPE> {
VTable() {} // empty
};
int main() {
using boost::spirit::qi::_1;
using boost::spirit::qi::eps;
using boost::spirit::qi::rule;
using boost::spirit::qi::ascii::space;
using boost::spirit::qi::space_type;
using boost::spirit::qi::real_parser;
using boost::spirit::qi::int_parser;
using boost::spirit::qi::strict_real_policies;
VTable<double> floatDecs;
floatDecs.add("hallo", 15.26)("duDa", 18.5);
const std::string some = "some";
rule<std::string::iterator, double> value = real_parser<double, strict_real_policies<double>>() [ boost::phoenix::bind(&VTable<double>::add, floatDecs, boost::phoenix::cref(some), _1) ];
std::cout << boost::spirit::qi::phrase_parse(test.begin(), test.end(), value, space);
return 0;
}
这里的问题在于boost::phoenix::bind,但我不知道如何解决这个问题(我对这个库相当陌生)。
这是一个工作版本。之后,我会列出你的问题。
#include <boost/spirit/include/qi.hpp>
#include <boost/spirit/include/phoenix.hpp>
#include <stdint.h>
#include <iostream>
#include <string>
template<typename VTYPE>
struct VTable : boost::spirit::qi::symbols<char, VTYPE> {
VTable() {} // empty
};
int main() {
using boost::spirit::qi::_1;
using boost::spirit::qi::eps;
using boost::spirit::qi::rule;
using boost::spirit::qi::ascii::space;
using boost::spirit::qi::space_type;
using boost::spirit::qi::real_parser;
using boost::spirit::qi::int_parser;
using boost::spirit::qi::strict_real_policies;
VTable<double> floatDecs;
floatDecs.add("hallo", 15.26)("duDa", 18.5);
const char some[] = "some";
rule<std::string::iterator, double()> value =
real_parser<double, strict_real_policies<double> >()
[ boost::phoenix::bind(floatDecs.add, boost::phoenix::val(some), _1) ];
std::string test("10.6");
std::string::iterator b(test.begin()), e(test.end());
std::cout << boost::spirit::qi::phrase_parse(b, e, value, space);
return 0;
}
首先,需要固定要用作add()参数的变量。
const char some[] = "some";
接下来,您需要在规则模板参数中修改语法:
rule<std::string::iterator, double()>
请注意括号,为了使双精度型声明为函数函数而不是普通的双精度型,用于延迟求值。
接下来,您需要为您的语义操作使用上面的语法。
[ boost::phoenix::bind(floatDecs.add, boost::phoenix::val(some), _1) ];
老实说,我不知道为什么这个工作,因为我不是一个凤凰专家,但上面的"她"评论中的链接提供了答案。此外,您对cref
进行了错误的搜索,从add()
的声明中没有迹象表明需要引用。val
是您需要将some
变量转换为凤凰快乐的懒惰参数。
最后,您只需要提供一些合理的测试数据。
上面的代码现在应该可以工作了
相关文章:
- 在 boost::qi 中使用过多的替代运算符会导致分段错误
- 如何在 boost::spirit::qi 中将某些语义操作排除在 AST 之外
- Boost.Spirit.Qi 语法,用于 Boost.Fusion 自适应结构中的默认值
- Qi::p arse Boost Change开始迭代器
- 我可以在 Boost.Spirit.Qi 中向列表运算符 (%) 提供内存分配提示吗?
- 如何在 Boost.Sprit.Qi 中从向量制作组合的 ascii::string 规则<string>?
- boost::spirit::qi and boost::phoenix::push_back
- BOOST.SPIRIT.QI:定义太多解析器的组合达到模板实例化限制
- boost::spirit::qi::phrase_parser() into std::map error
- Boost :: Spirit :: Qi语法使用具有不同迭代剂类型的语法
- 如何编写一个 boost::spirit::qi 解析器来解析从 0 到 std::numeric_limits:<int>:max() 的整数范围?
- boost.spirit:将字符串对从Qi到X3
- boost::spirit::qi对输出进行重复解析
- 为什么 boost::qi 规则的属性必须用括号声明?
- boost :: Spirit :: Qi-与语法结构中的实例成员一起工作
- 如何使用Qi :: Hold []解析器指令.(带有boost ::交换的属性类型的问题)
- Boost::Spirit::QI解析器:已解析元素的索引
- Boost Qi::p hrase_parse 只读取第一个元素
- 用boost::spirit::qi解析替身列表
- Boost qi属性以qi::unused_type的形式出现