在ANTLR4语法中嵌入访客/听众选项

时间:2014-07-23 14:59:47

标签: antlr4

我想从g4文件启用/禁用访客/监听器生成。

有没有办法在ANTLR4语法中嵌入Visitor / Listener选项?我正在寻找这样的东西:

grammar foo;

options {
    visitor=false;
    listener=false;
}

...

1 个答案:

答案 0 :(得分:2)

不,不在options区块内。

您可以提供生成这些类的Tool命令行选项,以确保不生成这些类。

例如,您为语法T生成类,如下所示:

java -cp antlr-4.4-complete.jar org.antlr.v4.Tool T.g4

要确保没有生成任何侦听器或访问者文件,请执行以下操作:

java -cp antlr-4.4-complete.jar org.antlr.v4.Tool T.g4 -no-listener -no-visitor

对于记录,这些是ANTLR4的命令行选项:

$java -cp antlr-4.4-complete.jar org.antlr.v4.Tool

ANTLR Parser Generator  Version 4.4
 -o ___              specify output directory where all output is generated
 -lib ___            specify location of grammars, tokens files
 -atn                generate rule augmented transition network diagrams
 -encoding ___       specify grammar file encoding; e.g., euc-jp
 -message-format ___ specify output style for messages in antlr, gnu, vs2005
 -long-messages      show exception details when available for errors and warnings
 -listener           generate parse tree listener (default)
 -no-listener        don't generate parse tree listener
 -visitor            generate parse tree visitor
 -no-visitor         don't generate parse tree visitor (default)
 -package ___        specify a package/namespace for the generated code
 -depend             generate file dependencies
 -D<option>=value    set/override a grammar-level option
 -Werror             treat warnings as errors
 -XdbgST             launch StringTemplate visualizer on generated code
 -XdbgSTWait         wait for STViz to close before continuing
 -Xforce-atn         use the ATN simulator for all predictions
 -Xlog               dump lots of logging info to antlr-timestamp.log