有没有办法知道ANTLR解析器当前处于哪个替代规则中? [英] Is there a way to know which alternative rule ANTLR parser is currently in?
本文介绍了有没有办法知道ANTLR解析器当前处于哪个替代规则中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想知道antlr当前在哪个替代规则中,它使用ctx.getChildCount()或ctx.id()!=NULL来了解antlr当前在哪个替代规则中。
但我想知道有没有方法可以给出备选规则的具体指标。
我看到此问题How to know which alternative rule ANTLR parser is currently in during visit,但它没有答案。
推荐答案
使用alt labels了解您的选择,或者尝试使用 options {contextSuperClass=org.antlr.v4.runtime.RuleContextWithAltNum;}
(link),但这可能不是您想要的。contextSuperClass
将仅在子规则中告诉您其父规则中的索引是什么:
parent
: child_a
| child_b
| child_c
| child_d
| child_e
;
child_d
: ... // in a listener, you can now get index 3 from the parent context
;
因此您无法执行以下操作:
parent
: child_a
| child_b
| child_c
| child_d // get index 3 here
| child_e
;
请注意,contextSuperClass
仅在Java中可用,通过查看链接中的备注:我只是将其放入Java运行时,因为我确信我是唯一真正使用它的人。&q;
这篇关于有没有办法知道ANTLR解析器当前处于哪个替代规则中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文