如何在 x3 中使用 qi::_1/qi::_N 重写 qi 解析器
How to rewrite qi parsers with qi::_1/qi::_N in x3?
假设我们要解析一个内部产品表达式并得到结果。
"SUM({1, 2, 3} .* {4, 5, 6})"
qi::_1
和qi::_2
在解析器中引用第 i个属性非常方便。
void Test(std::string const& input) {
qi::rule<decltype(boost::begin(input)), int(), qi::ascii::space_type> ruleSum =
(
qi::lit("SUM") >> '(' >>
'{' >> (qi::int_ % ',') >> '}' >>
".*" >>
'{' >> (qi::int_ % ',') >> '}' >>
')'
)[qi::_pass=phx::bind(
[](std::vector<int> const& vec1, std::vector<int> const& vec2, int& result) {
if(vec1.size() != vec2.size())
return false;
result = std::inner_product(boost::begin(vec1), boost::end(vec1), boost::begin(vec2), 0);
return true;
},
qi::_1, qi::_2, qi::_val
)];
int attr = 0;
if( qi::phrase_parse(boost::begin(input), boost::end(input), ruleSum >> qi::eoi, qi::ascii::space, attr) ) {
std::cout<<"Match! result = "<<attr<<std::endl;
} else {
std::cout<<"Not match!"<<std::endl;
}
}
在精神x3中,没有这样的气::_1...qi::_N. 我应该使用一对向量作为属性吗?解析类似此内部产品的推荐方法是什么?
namespace inner_product {
x3::rule<struct idInnerProduct, int> const ruleInnerProduct("InnerProduct");
auto const ruleInnerProduct_def =
(
x3::rule<struct _, std::pair<std::vector<int>, std::vector<int>>>{} =
x3::lit("SUM") >> '(' >>
'{' >> (x3::int_ % ',') >> '}' >>
".*" >>
'{' >> (x3::int_ % ',') >> '}' >>
')'
)[([](auto& ctx){
if(_attr(ctx).first.size() != _attr(ctx).second.size()) {
_pass(ctx) = false;
} else {
x3::_val(ctx) = std::inner_product(
boost::begin(x3::_attr(ctx).first),
boost::end(x3::_attr(ctx).first),
boost::begin(x3::_attr(ctx).second),
0
);
}
})];
BOOST_SPIRIT_DEFINE(ruleInnerProduct)
}
该属性是一个融合序列。其成员vector<int>
.
融合序列不是一对,所以你不能使用.first
或.second
。
我会写:
void Test(std::string const& input) {
namespace x3 = boost::spirit::x3;
auto ruleSum = x3::rule<struct _, int> {} =
(
x3::lit("SUM") >> '(' >>
'{' >> (x3::int_ % ',') >> '}' >> ".*" >>
'{' >> (x3::int_ % ',') >> '}' >>
')'
)[( [](auto& ctx) {
using boost::fusion::at_c;
// dissect context
auto& pass = x3::_pass(ctx);
auto& result = x3::_val(ctx);
std::vector<int> const& vec1 = at_c<0>(x3::_attr(ctx));
std::vector<int> const& vec2 = at_c<1>(x3::_attr(ctx));
// do the work
pass = (vec1.size() == vec2.size());
if (pass)
result = std::inner_product(boost::begin(vec1), boost::end(vec1), boost::begin(vec2), 0);
})
];
int attr = 0;
if( x3::phrase_parse(boost::begin(input), boost::end(input), ruleSum >> x3::eoi, x3::ascii::space, attr) ) {
std::cout<<"Match! result = "<<attr<<std::endl;
} else {
std::cout<<"Not match!"<<std::endl;
}
}
在科里鲁现场观看
指纹:
Match! result = 32
相关文章:
- 如何创建一个CMake变量,除非显式重写,否则使用默认值
- 将OpenCV C++重写为EmguCV C#-如何使用指针
- 在 C++ 中用派生类型重写成员函数
- 在模板基类中为继承类中的可选重写生成虚拟方法
- 方法重写线程C++中的概念
- 为重写std::exception的库生成swig接口时出错
- 如何强制从重写方法调用重写的方法基方法?
- 用于C++的静态二进制检测或二进制重写工具和框架
- 如何将 if else 语句重写为 switch 语句
- 如何重写全局方法名称以在调用原始方法之前将我的代码推到前面
- 用 C 重写C++类
- 是否总是可以将使用递归编写的程序重写为不使用递归的程序C++,性能观点是什么?
- 重写虚拟函数和继承
- 在 boost::qi 中使用过多的替代运算符会导致分段错误
- C++调用使用重写函数的父类函数
- 重写打印函数而不是覆盖基类
- 提升精神 V2 Qi 语法线程安全吗?
- 不允许在类定义之外重写
- C++有没有办法强制重写一组方法,如果其中一个方法在子类中具有重写?
- 如何在 x3 中使用 qi::_1/qi::_N 重写 qi 解析器