Antlr3 AST 创建:如何测试可选子规则?
我正在寻找一种方法来了解是否使用了可选子规则。例如:
my_rule returns [node* n = 0]:
(v = (optional_subrule)?)
{
$n = new node($v ? $v.n : MY_DEFAULT_VALUE);
}
;
但这不起作用。我尝试了很多方法来编写它,但如果不编写代码似乎什么都不可能...
my_rule returns [node* n = new node()]:
((optional_subrule { n->set_subrule(...); })?)
;
当您有野牛背景时,您喜欢在规则末尾添加 ast 节点构造函数...并且它会降低可读性(想象一下)更大的规则)。
有谁知道我错过了什么?
谢谢。
I am looking for a way to know if an optional sub rule has been used or not. For example:
my_rule returns [node* n = 0]:
(v = (optional_subrule)?)
{
$n = new node($v ? $v.n : MY_DEFAULT_VALUE);
}
;
But this does not work. I tried many ways to write it and nothing seems to be possible without writing code...
my_rule returns [node* n = new node()]:
((optional_subrule { n->set_subrule(...); })?)
;
And when you have a bison background, you like having your ast node constructors at the end of your rules... And it decreases the readability (imagine a much more bigger rule).
Does anyone know something I missed ?
Thank you.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
ANTLR 不允许这样的功能。 Bart Kiers 提出的解决方案不得使用,因为它会导致未定义的行为代码。
所以我必须将规则重写为:
我们现在拥有一个初始化良好变量的优势,并且我们仍然只有一个节点构造函数来包含所需的每个参数。
ANTLR does not allow such a feature. The solution proposed by Bart Kiers must not be used since it results in an undefined behavior code.
So I had to rewrite the rules as :
We now have the advantage of a well-initialised variable, and we still have only one node constructor with every arguments needed.