具有相同标签的多个替代方案,只有最后一个获得分配给它的任何内容
我有一些非保留关键字与以下规则匹配:
kFOO = {self.input.LT(1).text.lower() == 'foo'}? ID;
其中 ID
标记是标准的字母数字字符串。这些类型的规则效果很好,除非我尝试执行以下操作:
some_rule
@after { do_something_with($t.text) }
: t=kWORD1
| t=kWORD2
| t=kWORD3
;
在生成的解析器中,kWORD1
和 kWORD2
规则函数不返回任何内容,但kWORD3
函数可以。同样,在 some_rule
函数中,只有尝试匹配 kWORD3
的块才会将返回值分配给 t
。另外两个调用不以任何方式引用t
。
(另外,我期望以下内容能够工作,但事实并非如此,我怀疑出于同样的根本原因。
some_rule
@after { do_something_with($t.text) }
: t=( kWORD1
| kWORD2
| kWORD3)
;
在任何条件下都不会将任何内容分配给 t
。)
但是,以下内容确实按预期工作:
some_rule
@after { do_something_with($t1.text or $t2.text or $t3.text) }
: t1=kWORD1
| t2=kWORD2
| t3=kWORD3
;
每个生成匹配函数的返回值,并且每个与 some_rule
中的关键字规则匹配的块将返回值分配给其标签。这个解决方案的问题是,当有多个替代方案时,它会变得有点过分。
我的一半在喊“BUG!”但这是我做的第一个antlr项目,所以很可能有一些我不明白的地方。
完成我在这里尝试做的事情的最佳方法是什么?
I have some non-reserved keywords I'm matching with rules like:
kFOO = {self.input.LT(1).text.lower() == 'foo'}? ID;
Where the ID
token is a standard alpha-numeric string. These kinds of rules work great, except when I try to do something like this:
some_rule
@after { do_something_with($t.text) }
: t=kWORD1
| t=kWORD2
| t=kWORD3
;
In the generated parser, the kWORD1
and kWORD2
rule functions don't return anything, but the kWORD3
function does. As well, in the some_rule
function, only the block trying to match kWORD3
assign the return value to t
. The other two invocations don't reference t
in any way.
(Also, I expected the following to work, but it did not, I suspect for the same underlying reason.
some_rule
@after { do_something_with($t.text) }
: t=( kWORD1
| kWORD2
| kWORD3)
;
Nothing gets assigned to t
under any conditions.)
However, the following DOES work as expected:
some_rule
@after { do_something_with($t1.text or $t2.text or $t3.text) }
: t1=kWORD1
| t2=kWORD2
| t3=kWORD3
;
Each of the matching functions is generated to return a value, and each of the blocks matching the keyword rules in some_rule
assigns the return value to their label. The problem with this solution is it gets a little excessive when there are several more alternatives.
Half of me cries "BUG!" but this is the first antlr project I've done, so more likely there's something I don't understand.
What's the best way to do what it is I'm trying to do here?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
除了
.toLower()
永远不会匹配FOO
之外,我相信这是因为 kWORD1 是 kWORD2 的另一种类型等,但可能有效的是:
尽管未经测试。
Besides the fact that
.toLower()
never matchesFOO
,I believe this is due to the fact that kWORD1 is another type of kWORD2 etc. but something that might work is this:
untested though.