Antlr4 Visitor几个规则上下文 [英] Antlr4 Visitor several rule contexts

查看:65
本文介绍了Antlr4 Visitor几个规则上下文的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这样的语法:

search
 : K_SEARCH entity
 ( K_QUERY expr )?
 ( K_FILTER expr )?
;

如您所见,我有两个可选的 expr .

As you can see I've two optional expr.

我已经创建了访问者,并且可以访问实体 K_QUERY K_FILTER . SearchContext 提供了一个 List< ExprContext> ,以获取所有 expr 的列表.但是,如何通过表达式知道 K_QUERY expr 还是 K_FILTER expr ?

I've created my Visitor, and I'm able to get access to entity, K_QUERY and K_FILTER. SearchContext provides a List<ExprContext> in order to get a list of all expr. However, how can I know with expression is a K_QUERY expr or a K_FILTER expr?

public class LivingQueryVisitor extends LivingDSLBaseVisitor<Void> {

    @Override
    public Void visitSearch(SearchContext ctx) {
        this.query = search(this.getEntityPath(ctx));
        //???????????????????????
        List<ExprContext> exprs = ctx.expr();
        //???????????????????????
        return super.visitSearch(ctx);
    }
}

有什么想法吗?

推荐答案

只需标记两个 expr 术语.

search : K_SEARCH entity
       ( K_QUERY  q=expr )?
       ( K_FILTER f=expr )?
;

Antlr将在SearchContext类内生成两个其他变量:

Antlr will generate two additional variables within the SearchContext class:

ExprContext q;
ExprContext f;

值将为非空值 iff 匹配的相应子项.

The values will be non-null iff the corresponding subterms matched.

这篇关于Antlr4 Visitor几个规则上下文的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆