boost::spirit 并生成不同的节点
问候。
我一直对如何在解析语法和生成 AST 时强制 boost::spirit 生成不同类的节点感兴趣。比如说,我想要不同的节点,例如 VariableNode(其成员为变量名称)、ValueNode(其成员为 value)等。
在处理树遍历器时,这将非常有用。在这种情况下,我们将编写一个基本抽象类来遍历所有不同的节点(应用“访问者”模式),并在处理语义检查阶段、代码生成阶段等时扩展它。
boost::spirit 允许我们参数化用于树木的工厂,但我一直无法找到调整其行为的正确方法。
有什么想法,代码吗?提前致谢。
greetings.
i've been interesting in how to force boost::spirit to produce nodes of different classes when parsing the grammar and generating AST. say, i want to have different nodes such as VariableNode (which has name of variable as its member), ValueNode (which has value as its member), etc.
it would be very useful when dealing with tree-walker. in this case we would write a base abstract class for walking all the different nodes (applying "the visitor" pattern) and extend it when dealing with semantics checking phase, code generation phase and the such.
boost::spirit allows us to parameterize factory being used for trees, but i've been unable to find a proper way to tune its behavior.
any ideas, code? thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我不确定我是否理解你的问题,你的意思是这样的吗? :
然后您可以将 boost::apply_visitor (请参阅 boost::variant 文档)与访问者类一起使用来执行您想要的行为。
I'm not sure I understand your question, do you mean something like this? :
You can then use boost::apply_visitor (see boost::variant documentation) with a visitor class to do the behavior you want.
要回答您的评论(您可能想为此开始一个新问题):标识符可能应该存储在 qi::symbols 派生类中,关键字将位于您的其他 qi::rules 中。
至于2)这将是这样的(未经测试):
..
如果您需要,我可能会花费更多,但如果您遇到具体问题,您可能应该开始一个新问题,以便其他人也可以提供帮助,因为我只是一个boost::spirit 的初学者用户可能会有更好的答案。
干杯
To answer your comment (you might want to start a new question for this): identifiers should probably be stored in a qi::symbols-derived class and keywords would be in your other qi::rules.
As for 2) this would be something like this (not tested):
..
I could probably expend more if you need but you should probably start a new question if you got specific issues so other people can help too as I'm just a beginner user of boost::spirit and they might have better answers.
Cheers