且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

C++ Boost qi递归规则构建

更新时间:2022-06-27 01:05:29

注意 我只是用一些更多的信息链接扩展了我的答案.在这种特殊情况下,我有一种预感,您可以只是使用 Nabialek 技巧用相应的替换继承属性qi::locals 代替.如果我有足够的时间,我可能会在稍后进行演示.

Note I just extended my answer with a few more informational links. In this particular case I have a hunch that you could just get away with the Nabialek trick and replacing the inherited attribute with a corresponding qi::locals<> instead. If I have enough time, I might work out a demonstration later.

请注意,在复制原始表达式树和精神解析器表达式时存在问题,特别是 - 它创建悬空引用,因为内部不应超过包含完整表达式的末尾.在零到2 秒内达到每小时 60 英里!

Please be advised that there are issues when copying proto expression trees and spirit parser expressions in particular - it will create dangling references as the internals are not supposed to live past the end of the containing full expressions. See BOOST_SPIRIT_AUTO on Zero to 60 MPH in 2 seconds!

另请参阅这些答案,这些答案也与动态(在运行时)构建/编写规则有关:

Also see these answers which also concerns themselves with building/composing rules on the fly (at runtime):

  • Generating Spirit parser expressions from a variadic list of alternative parser expressions
  • Can Boost Spirit Rules be parameterized which demonstrates how to return rules from a function using boost::proto::deepcopy (like BOOST_SPIRIT_AUTO does, actually)

总的来说,我强烈建议反对在运行时组合规则.相反,如果您希望在运行时向规则添加替代项",您始终可以使用 qi::symbols 代替.诀窍是在符号表中存储规则并使用qi::lazy来调用规则.特别是,这被称为 Nabialek Trick.

In general, I'd very strongly advise against combining rules at runtime. Instead, if you're looking to 'add alternatives' to a rule at runtime, you can always use qi::symbols<> instead. The trick is to store a rule in the symbol-table and use qi::lazy to call the rule. In particular, this is known as the Nabialek Trick.

我在这里有一个玩具命令行参数解析器,它演示了如何使用这个习语来匹配运行时定义的一组命令行参数:

I have a toy command-line arguments parser here that demonstrates how you could use this idiom to match a runtime-defined set of command line arguments:

不幸的是,qi::lazy 不支持继承参数参见例如

Unfortunately, qi::lazy does not support inherited arguments see e.g.

您***编写自定义解析器组件,如下所述:

You might be better off writing a custom parser component, as documented here:

稍后我会尝试找一些时间来制定一个示例,用 qi::locals 替换继承的参数.