如何在带有 Boost Spirit 的 AST 中使用只有一个属性的类?
我想使用 Boost Spirit 将文件解析为 AST。
我的 AST 的根是一个只有一个属性的类:
typedef boost::variant<FunctionDeclaration, GlobalVariableDeclaration> FirstLevelBlock;
struct Program {
std::vector<FirstLevelBlock> blocks;
};
BOOST_FUSION_ADAPT_STRUCT(
::Program,
(std::vector<eddic::FirstLevelBlock>, blocks)
)
如果我使用单个规则进行解析:
program %= *(function | globalDeclaration);
它不会编译,但如果我向 Program 添加单个字符串名称,它会很好地工作。我可以使用向量作为根,但我想使用该类,因为我想向 Program 类添加一些方法。
编辑:
如果我用大括号括住我的程序,它运行良好:
program %= lexer.left_brace >> *(function | globalDeclaration) >> lexer.right_brace;
编译并运行良好,但是:
program %= *(function | globalDeclaration);
不编译...
Boost Spirit 中是否有某些内容阻止使用如此简单的规则?
I want to parse a file into an AST using Boost Spirit.
The root of my AST is a class with only one attribute :
typedef boost::variant<FunctionDeclaration, GlobalVariableDeclaration> FirstLevelBlock;
struct Program {
std::vector<FirstLevelBlock> blocks;
};
BOOST_FUSION_ADAPT_STRUCT(
::Program,
(std::vector<eddic::FirstLevelBlock>, blocks)
)
If I parse using a single rule :
program %= *(function | globalDeclaration);
it doesn't compiles, but if I add a single string name to Program, it works well. I could use the vector as the root, but I want to use the class, because I want to add some methods to the Program class.
EDIT :
If I surround my program with braces, it works well :
program %= lexer.left_brace >> *(function | globalDeclaration) >> lexer.right_brace;
compiles and works fine, but :
program %= *(function | globalDeclaration);
does not compile...
Is there something in Boost Spirit that prevent using such simple rules ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
编辑后的问题版本2
首先,如果没有
function
和globalDeclaration
的定义,我们就无法真正判断。其次,我尝试将我的 PoC 行更改为
瞧,我收到了编译器错误!现在我当然同意这看起来非常像一个属性转换错误。另外,这是一个初步的解决方法:
如您所见,
qi::eps
来救援原始问题的答案版本 1
嗯。我认为您需要发布一个最小的工作示例。这是从您的问题开始的概念证明,一切都很好。
请注意,我使用
g++ -std=c++0x
进行编译,以便在testAttr
参数参数代码> 函数。输出:
Edited question version 2
Firstly, we can't really tell without the defintion for
function
andglobalDeclaration
.Secondly I tried, changing my PoC lines to
Lo and behold, I get your compiler error! Now I would certainly agree that this looks very much like an attribute conversion bug. Also, Here is a preliminary workaround:
As you can see,
qi::eps
to the rescueAnswer to original question version 1
Mmm. I think you need to post a minimal working sample. Here is a proof of concept starting from your question, and it all works rather nicely.
Note that I compiled with
g++ -std=c++0x
in order to get the defaultAttr
parameter argument on thetest
function.Output: