【发布时间】:2012-09-20 20:44:21
【问题描述】:
我有两个具有相同属性的规则。
是否可以将matrix_规则的属性传递给matrixBlock_子规则? 我想阻止重复指令创建表单向量的属性。相反,它应该继续写入 matrix_ 的属性(numBlocks 的时间)。 我试图将属性作为继承属性传递给子规则并编译(见下文)。但是我的向量中有几个“幽灵”条目不是来自 phoenix::push_back。 此外,这似乎不是我的最佳方式。我们是否可以在 matrixBlock_ 中自动传播属性而不是语义动作?
typedef vector<columnT> Matrix;
matrix_ = repeat(numBlocks)[ matrixBlock_(_val) ];
matrixBlock_ = *column[phoenix::push_back(_r1, _1)];
qi::rule<Iterator, Matrix(), ascii::space_type> matrix_;
qi::rule<Iterator, void(Matrix&), ascii::space_type> matrixBlock_;
更新
澄清问题:
如果我编写没有语义动作的规则,matrix_ 的综合属性将是
vector< vector< columnT > >
-
typedef vector<columnT> Matrix;
matrix_ = repeat(numBlocks)[ matrixBlock_ ];
matrixBlock_ = *column;
qi::rule<Iterator, Matrix(), ascii::space_type> matrix_;
qi::rule<Iterator, Matrix(), ascii::space_type> matrixBlock_;
我希望它具有与 matrixBlock_ 相同的属性类型,一个一维数组。
我的实际解决方案是只使用一个规则。 (看起来很容易:-))
typedef vector<columnT> Matrix;
matrix_ = repeat(numBlocks)[ *column_[ phoenix::push_back(_val, _1) ] ];
//matrixBlock_ = *column;
qi::rule<Iterator, Matrix(), ascii::space_type> matrix_;
//qi::rule<Iterator, Matrix(), ascii::space_type> matrixBlock_;
更新
我能够在 vs2010 和 boost 1.46.1 中使用此代码重现幻影条目
http://liveworkspace.org/code/505091dc4631a379763567168a728e0c
输出为:42、45、-9、3、2、1、12、34、56、0、0、0
我的错误是使用旧的 Boost 版本。 1.5 中没有 phontoms。
现在我的语法有两个工作版本。是否可以在不使用 push_back 语义动作的情况下重新设计语法?
【问题讨论】:
-
您能查看/编辑问题标题吗?我觉得它不能准确反映实际问题
-
我看不到你在这里问什么。你能举一个工作的例子吗?请参阅sscce.org(例如,可能以我现有示例中的 两个 SSCCE 为例)
-
感谢您的样品。现在答案出奇地简单
-
或多或少令人惊讶的是,您的答案不适用于 boost 1_46_1...
-
是的,这在 V2.5 中得到了改进 - boost 1_47 Change note:
"The attribute handling for container attributes of sequences and container components (list, Kleene, Plus, and repeat) has been completely rewritten. It now supports many more use cases and behaves much more predictable than the older version. Thanks to Thomas Taylor, Richard Crossley, Semen, Adalberto Castelo, and many others for reporting bugs and helping in making the new code behave as expected."
标签: c++ boost boost-spirit