hendryau
hendryau

Reputation: 456

Embed Visitor/Listener Options in an ANTLR4 Grammar

I want to enable/disable visitor/listener generation from the g4 file.

Is there a way to embed Visitor/Listener options in an ANTLR4 grammar? I'm looking for something like this:

grammar foo;

options {
    visitor=false;
    listener=false;
}

...

Upvotes: 2

Views: 2009

Answers (1)

Bart Kiers
Bart Kiers

Reputation: 170158

No, not inside the options block.

You can provide the Tool, which generates these classes, command line options to make sure these classes are not generated.

For example, you generate classes for your grammar T as follows:

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

And to make sure no listener or visitor files are being generated, do this:

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

For the record, these are ANTLR4's command line options:

$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

Upvotes: 3

Related Questions