如何知道在访问期间ANTLR解析器当前处于哪个替代规则

How to know which alternative rule ANTLR parser is currently in during visit

如果我们查看 bash 来源,特别是 yacc 语法,我们可以看到所有重定向都是这样定义的:

redirection
        :   GREATER WORD
        |   LESS WORD
        |   NUMBER GREATER WORD
        |   NUMBER LESS WORD
        |   REDIR_WORD GREATER WORD
        |   REDIR_WORD LESS WORD
        |   GREATER_GREATER WORD
        |   NUMBER GREATER_GREATER WORD
        |   REDIR_WORD GREATER_GREATER WORD
        |   GREATER_BAR WORD
        |   NUMBER GREATER_BAR WORD
        |   REDIR_WORD GREATER_BAR WORD
        |   LESS_GREATER WORD
        |   NUMBER LESS_GREATER WORD
        |   REDIR_WORD LESS_GREATER WORD
        |   LESS_LESS WORD
        |   NUMBER LESS_LESS WORD
        |   REDIR_WORD LESS_LESS WORD
        |   LESS_LESS_MINUS WORD
        |   NUMBER LESS_LESS_MINUS WORD
        |   REDIR_WORD  LESS_LESS_MINUS WORD
        |   LESS_LESS_LESS WORD
        |   NUMBER LESS_LESS_LESS WORD
        |   REDIR_WORD LESS_LESS_LESS WORD
        |   LESS_AND NUMBER
        |   NUMBER LESS_AND NUMBER
        |   REDIR_WORD LESS_AND NUMBER
        |   GREATER_AND NUMBER
        |   NUMBER GREATER_AND NUMBER
        |   REDIR_WORD GREATER_AND NUMBER
        |   LESS_AND WORD
        |   NUMBER LESS_AND WORD
        |   REDIR_WORD LESS_AND WORD
        |   GREATER_AND WORD
        |   NUMBER GREATER_AND WORD
        |   REDIR_WORD GREATER_AND WORD
        |   GREATER_AND DASH
        |   NUMBER GREATER_AND DASH
        |   REDIR_WORD GREATER_AND DASH
        |   LESS_AND DASH
        |   NUMBER LESS_AND DASH
        |   REDIR_WORD LESS_AND DASH
        |   AND_GREATER WORD
        |   AND_GREATER_GREATER WORD
        ;

在我的访问者中,当调用 visitRedirection 时,几乎不可能知道访问者当前处于哪个备选方案中。我可以使用 # 和标签标记每个备选方案,但添加 43 次访问仅针对单一产生式规则的方法似乎有些过分。

通常我会通过 ctx.GREATER() != null 来做一些 null 检查以了解是否选择了第一个备选方案,但在这个例子中几乎总是有 2 个冲突的备选方案,例如 :

GREATER WORD
NUMBER GREATER WORD

那么我应该 ctx.NUMBER() != null && ctx.GREATER() != null 来匹配第二个选项,ctx.NUMBER() == null && ctx.GREATER() != null 来匹配第一个吗?

是否有更简单或更简洁的方法来了解访问者当前处于哪个特定替代方案?

重组语法以减少备选方案。他们中的许多人都有共同的前导或尾随部分,例如:

redirection
        :   GREATER WORD
        |   LESS WORD
        |   NUMBER (GREATER | LESS) WORD
        |   REDIR_WORD (GREATER | LESS | LESS_LESS_MINUS) WORD
        |   ...

这样你在每个 alt 中都有一个唯一的第一个标记,然后你可以将其分配给一个局部变量:

redirection
        :   op = GREATER WORD
        |   op = LESS WORD
        |   op = NUMBER subOp= (GREATER | LESS) WORD
        |   op = REDIR_WORD subOp =(GREATER | LESS | LESS_LESS_MINUS) WORD
        |   ...

有了它,您可以轻松检查您在 listener/visitor:

中的 alt
public exitRedirection(RedirectionContext ctx) {
    switch (ctx.op.getType()) {
        case YourParser.GREATER_WORD: {
            break;
        }

        case YourParser.REDIR_WORD: {
            switch (ctx.supOp.getType()) {
                case YourParser.LESS_LESS_MINUS: {
                    break;
                }
            }
            break;
        }
    }