本文介绍了标记子规则后,用于规则的ANTLR4.7侦听器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这样的 antlr4.7 语法,其中所有子规则都标记了.

I have an antlr4.7 grammar like this, where all sub rules are labeled.

date_expr
    :   attr op=( '+' | '-' ) dt_interval=ISO8601_INTERVAL
        #dateexpr_Op
    |   DATETIME_NAME
        #dateexpr_Named
    |   d=( DATETIME_LITERAL | DATE_LITERAL | TIME_LITERAL )
        #dateexpr_Literal
    |   attr
        #dateexpr_Attr
    |   '(' date_expr ')'
        #dateexpr_Paren
    ;

date_expr 规则完成时,我想对树进行注释.但是,查看生成的侦听器类,我看不到 exitDate_expr .如何添加呢?或者,我是否必须使用访问者界面.我对语法工具不太熟悉.

I would like to annotate the tree when a date_expr rule completes. However, looking at the generated listener class, I see no exitDate_expr. How can I add this? Or, do I have to use a visitor interface for it. I am not much familiar with grammar tools.

谢谢.

推荐答案

要实现 beforeAllLabledAlts afterAllLabledAlts 访问点,请将带标签的alt规则包装在单例规则中:

To achieve beforeAllLabledAlts and afterAllLabledAlts visit points, wrap the labeled alt rule in a singleton rule:

anyDate : dateExpr ;
dateExpr
    :   attr op=( '+' | '-' ) dt_interval=ISO8601_INTERVAL    #dateexpr_Op
    |   DATETIME_NAME                                         #dateexpr_Named
    |   d=( DATETIME_LITERAL | DATE_LITERAL | TIME_LITERAL )  #dateexpr_Literal
    |   attr                                                  #dateexpr_Attr
    |   '(' date_expr ')'                                     #dateexpr_Paren
    ;

然后,ANTLR工具将使用 AnyDateContext onEntry onExit 方法生成侦听器接口(和/或访客接口).

The ANTLR tool will then generate the listener interface (and/or visitor interface) with AnyDateContext onEntry and onExit methods.

这篇关于标记子规则后,用于规则的ANTLR4.7侦听器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-19 08:15