猿问

Antlr4 Listener 子树检查条件

我是 Antlr 的新手,请原谅我的基本问题,我正在尝试验证以下语句,例如if while_condition contains f_lastmove.. do somethingwhile_condition 也可以有其他条件。如何深入研究 while_condition?我正在使用带有 Golang 的侦听器模式。 


aluckdog
浏览 87回答 1
1回答

拉丁的传说

我不知道 Go,但在 Java 中你可以这样做:// In this example, the grammar is called `T.g4`class WhileLastMoveListener extends TBaseListener {    private boolean insideWhileCondition = false;        @Override     public void enterWhile_condition(TParser.While_conditionContext ctx) {         this.insideWhileCondition = true;    }    @Override     public void exitWhile_condition(TParser.While_conditionContext ctx) {        this.insideWhileCondition = false;    }    @Override     public void enterF_lastmove(TParser.F_lastmoveContext ctx) {        if (this.insideWhileCondition) {            // Found a `f_lastmove` rule inside a while `while_condition`        }    }}
随时随地看视频慕课网APP

相关分类

Go
我要回答