Top Description Fields Constructors Methods
org.antlr

public Class Tool

extends Object
Class Inheritance
Imports
org.antlr.analysis.*, org.antlr.codegen.CodeGenerator, org.antlr.misc.Graph, org.antlr.runtime.misc.Stats, org.antlr.tool.*, org.stringtemplate.v4.STGroup, java.io.*, java.util.*

The main ANTLR entry point. Read a grammar and generate a parser.

Field Summary

Modifier and TypeField and Description
public final Properties
private boolean
protected boolean
private boolean
private static boolean
private boolean
public String
private boolean
private boolean
private boolean
private List<String>
private String
private boolean
private boolean
private String
public static boolean
public static boolean
public static boolean
public static boolean
private String
private boolean
make

Don't process grammar file if generated files are newer than grammar

private String
private String
private boolean
private boolean
private boolean
private static boolean
private boolean
private boolean
public static final String
private boolean
public final String

Constructor Summary

AccessConstructor and Description
public
Tool()

public
Tool(String[] args)

Method Summary

Modifier and TypeMethod and Description
public void
addGrammarFile(String grammarFileName)

public boolean
buildRequired(String
The grammar file we are checking
grammarFileName
)

Checks to see if the list of outputFiles all exist, and have last-modified timestamps which are later than the last-modified timestamp of all the grammar files involved in build the output (imports must be checked).

public void
protected void
protected void
generateRecognizer(Grammar grammar)

Create NFA, DFA and generate code for grammar.

public int

Returns:

DFA creation timeout value in milliseconds
getConversionTimeout
()

Provide the current setting of the conversion timeout on DFA creation.

public static String
getCurrentTimeStamp()

Return a time stamp string accurate to sec: yyyy-mm-dd hh:mm:ss

public String
getFileDirectory(String fileName)

Return the directory containing the grammar file for this grammar.

public List<String>

Returns:

the grammarFileNames
getGrammarFileNames
()

Provide the List of all grammar file names that the ANTLR tool will process or has processed.

public File
getImportedVocabFile(String vocabName)

Return a File descriptor for vocab file.

public String

Returns:

the lib Directory
getLibraryDirectory
()

Return the Path to the directory in which ANTLR will search for ancillary files such as .tokens vocab files and imported grammar files.

public String

Returns:

Path to file that we think shuold be the import file
getLibraryFile
(String
input name we are looking for
fileName
)

Name a file from the -lib dir.

public boolean

Returns:

true if the tool is currently configured to analyze and sort grammar files.
getMake
()

Indicate whether the tool will analyze the dependencies of the provided grammar file list and ensure that grammars with dependencies are built after any of the other gramamrs in the list that they are dependent on.

public String

Returns:

Current message format
getMessageFormat
()

Returns the current setting of the message format descriptor

public int

Returns:

Error count
getNumErrors
()

Returns the number of errors that the analysis/processing threw up.

public File
getOutputDirectory(String
path to input source
fileNameWithPath
)

Return the location where ANTLR will generate output files for a given file.

public String

Returns:

the output Directory
getOutputDirectory
()

Return the Path to the base output directory, where ANTLR will generate all the output files for the current language target as well as any ancillary files such as .tokens vocab files.

public Writer
getOutputFile(Grammar g, String fileName)

This method is used by all code generators to create new output files.

public Grammar
getRootGrammar(String grammarFileName)

Get a grammar mentioned on the command-line and any delegates

private static void
help()

public boolean

Returns:

the debug flag
isDebug
()

Indicate if ANTLR has generated, or will generate a debug version of the recognizer.

public boolean

Returns:

the depend flag
isDepend
()

Indicates whether ANTLR has supplied, or will supply, a list of all the things that the input grammar depends upon and all the things that will be generated when that grammar is successfully analyzed.

public boolean

Returns:

the forceAllFilesToOutputDir flag
isForceAllFilesToOutputDir
()

Indicates whether ANTLR will force all files to the output directory, even if the input files have relative paths from the input directory.

public boolean

Returns:

the generate_DFA_dot
isGenerate_DFA_dot
()

Indicates whether ANTLR has generated or will generate a description of all the NFAs in Dot format

public boolean

Returns:

the generate_NFA_dot
isGenerate_NFA_dot
()

Indicates whether ANTLR has gnerated or will generate a description of all the NFAs in Dot format

public boolean

Returns:

the printGrammar flag
isPrintGrammar
()

Indicates whether ANTLR has printed, or will print, a version of the input grammar file(s) that is stripped of any action code embedded within.

public boolean

Returns:

the profile
isProfile
()

Indicates whether ANTLR has generated or will generate a version of the recognizer that gathers statistics about its execution, which it prints when it terminates.

public boolean

Returns:

the report flag
isReport
()

Indicates whether ANTLR has generated or will generate a report of various elements of the grammar analysis, once it it has finished analyzing a grammar file.

public boolean

Returns:

the trace flag
isTrace
()

Indicate whether ANTLR has generated, or will generate a version of the recognizer that prints trace messages on entry and exit of each rule.

public boolean

Returns:

the verbose flag
isVerbose
()

Indicates whether ANTLR will be verbose when analyzing grammar files, such as displaying the names of the files it is generating and similar information.

private void
loadResources()

Load the properties file org/antlr/antlr.properties and populate any variables that must be initialized from it, such as the version of ANTLR.

public static void
main(String[] args)

A list of dependency generators that are accumulated aaaas (and if) the tool is required to sort the provided grammars into build dependency order.

public void
panic()

If the tool needs to panic/exit, how do we do that?

public void
public void
public void
setDebug(boolean
true to generate a debug mode parser
debug
)

Indicate whether ANTLR should generate a debug version of the recognizer.

public void
setDepend(boolean
true to get depends set rather than process the grammar
depend
)

Indicate whether ANTLR should supply a list of all the things that the input grammar depends upon and all the things that will be generated when that gramamr is successfully analyzed.

public void
setForceAllFilesToOutputDir(boolean
true to force files to output directory
forceAllFilesToOutputDir
)

Indicates whether ANTLR will force all files to the output directory, even if the input files have relative paths from the input directory.

public void
setForceRelativeOutput(boolean
true if output files hould always be relative to base output directory
forceRelativeOutput
)

Used by build tools to force the output files to always be relative to the base output directory, even though the tool had to set the output directory to an absolute path as it cannot rely on the workign directory like command line invocation can.

public void
setGenerate_DFA_dot(boolean
True to generate dot descriptions
generate_DFA_dot
)

Indicates whether ANTLR should generate a description of all the NFAs in Dot format

public void
setGenerate_NFA_dot(boolean
True to generate dot descriptions
generate_NFA_dot
)

Indicate whether ANTLR should generate a description of all the NFAs in Dot format

public void
setGrammarFileNames(List<String>
The list of grammar files to process
grammarFileNames
)

Provide the List of all grammar file names that the ANTLR tool should process.

public void
setInputDirectory(String
Input source base directory
inputDirectory
)

Set the base location of input files.

public void
setLibDirectory(String
the libDirectory to set
libDirectory
)

Set the Path to the directory in which ANTLR will search for ancillary files such as .tokens vocab files and imported grammar files.

public void
setMake(boolean make)

Indicate whether the tool should analyze the dependencies of the provided grammar file list and ensure that the grammars with dependencies are built after any of the other gramamrs in the list that they are dependent on.

public void
setMaxSwitchCaseLabels(int
Maximum number of case lables that ANTLR should allow the target code
maxSwitchCaseLabels
)

Set the threshold of case labels beyond which ANTLR will not instruct the target template to generate switch() { case xxx: ...

public void
setMessageFormat(String format)

Set the message format to one of ANTLR, gnu, vs2005

public void
setMinSwitchAlts(int
the minimum number of alts required to use a switch staement
minSwitchAlts
)

Set the threshold of the number alts, below which ANTLR will not instruct the target template to use a switch statement.

public void
setOutputDirectory(String outputDirectory)

Set the location (base directory) where output files should be produced by the ANTLR tool.

public void
setPrintGrammar(boolean
true to generate a stripped file
printGrammar
)

Indicate whether ANTLR should print a version of the input grammar file(s) that is stripped of any action code embedded within.

public void
setProfile(boolean
true to generate a profiling parser
profile
)

Indicate whether ANTLR should generate a version of the recognizer that gathers statistics about its execution, which it prints when it terminates.

public void
setReport(boolean
true to generate the analysis report
report
)

Indicate whether ANTLR should generate a report of various elements of the grammar analysis, once it it has finished analyzing a grammar file.

public void
setTrace(boolean
true to generate a tracing parser
trace
)

Indicate whether ANTLR should generate a version of the recognizer that prints trace messages on entry and exit of each rule

public void
setVerbose(boolean
true to be verbose
verbose
)

Indicate whether ANTLR should be verbose when analyzing grammar files, such as displaying the names of the files it is generating and similar information.

public void
private static void
protected void
protected void
private static void
Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

antlrSettingsback to summary
public final Properties antlrSettings
debugback to summary
private boolean debug
deleteTempLexerback to summary
protected boolean deleteTempLexer
dependback to summary
private boolean depend
exitNowback to summary
private static boolean exitNow
forceAllFilesToOutputDirback to summary
private boolean forceAllFilesToOutputDir
forcedLanguageOptionback to summary
public String forcedLanguageOption
forceRelativeOutputback to summary
private boolean forceRelativeOutput
generate_DFA_dotback to summary
private boolean generate_DFA_dot
generate_NFA_dotback to summary
private boolean generate_NFA_dot
grammarFileNamesback to summary
private List<String> grammarFileNames
grammarOutputDirectoryback to summary
private String grammarOutputDirectory
haveInputDirback to summary
private boolean haveInputDir
haveOutputDirback to summary
private boolean haveOutputDir
inputDirectoryback to summary
private String inputDirectory
internalOption_PrintDFAback to summary
public static boolean internalOption_PrintDFA
internalOption_PrintGrammarTreeback to summary
public static boolean internalOption_PrintGrammarTree
internalOption_ShowNFAConfigsInDFAback to summary
public static boolean internalOption_ShowNFAConfigsInDFA
internalOption_watchNFAConversionback to summary
public static boolean internalOption_watchNFAConversion
libDirectoryback to summary
private String libDirectory
makeback to summary
private boolean make

Don't process grammar file if generated files are newer than grammar

outputDirectoryback to summary
private String outputDirectory
parentGrammarDirectoryback to summary
private String parentGrammarDirectory
printGrammarback to summary
private boolean printGrammar
profileback to summary
private boolean profile
reportback to summary
private boolean report
return_dont_exitback to summary
private static boolean return_dont_exit
showBannerback to summary
private boolean showBanner
traceback to summary
private boolean trace
UNINITIALIZED_DIRback to summary
public static final String UNINITIALIZED_DIR
verboseback to summary
private boolean verbose
VERSIONback to summary
public final String VERSION

Constructor Detail

Toolback to summary
public Tool()
Toolback to summary
public Tool(String[] args)
Annotations
@SuppressWarnings:OverridableMethodCallInConstructor

Method Detail

addGrammarFileback to summary
public void addGrammarFile(String grammarFileName)
buildRequiredback to summary
public boolean buildRequired(String grammarFileName) throws IOException

Checks to see if the list of outputFiles all exist, and have last-modified timestamps which are later than the last-modified timestamp of all the grammar files involved in build the output (imports must be checked). If these conditions hold, the method returns false, otherwise, it returns true.

Parameters
grammarFileName:String

The grammar file we are checking

generateDFAsback to summary
public void generateDFAs(Grammar g)
generateNFAsback to summary
protected void generateNFAs(Grammar g)
generateRecognizerback to summary
protected void generateRecognizer(Grammar grammar)

Create NFA, DFA and generate code for grammar. Create NFA for any delegates first. Once all NFA are created, it's ok to create DFA, which must check for left-recursion. That check is done by walking the full NFA, which therefore must be complete. After all NFA, comes DFA conversion for root grammar then code gen for root grammar. DFA and code gen for delegates comes next.

getConversionTimeoutback to summary
public int getConversionTimeout()

Provide the current setting of the conversion timeout on DFA creation.

Returns:int

DFA creation timeout value in milliseconds

getCurrentTimeStampback to summary
public static String getCurrentTimeStamp()

Return a time stamp string accurate to sec: yyyy-mm-dd hh:mm:ss

getFileDirectoryback to summary
public String getFileDirectory(String fileName)

Return the directory containing the grammar file for this grammar. normally this is a relative path from current directory. People will often do "java org.antlr.Tool grammars/*.g3" So the file will be "grammars/foo.g3" etc... This method returns "grammars". If we have been given a specific input directory as a base, then we must find the directory relative to this directory, unless the file name is given to us in absolute terms.

getGrammarFileNamesback to summary
public List<String> getGrammarFileNames()

Provide the List of all grammar file names that the ANTLR tool will process or has processed.

Returns:List<String>

the grammarFileNames

getImportedVocabFileback to summary
public File getImportedVocabFile(String vocabName)

Return a File descriptor for vocab file. Look in library or in -o output path. antlr -o foo T.g U.g where U needs T.tokens won't work unless we look in foo too. If we do not find the file in the lib directory then must assume that the .tokens file is going to be generated as part of this build and we have defined .tokens files so that they ALWAYS are generated in the base output directory, which means the current directory for the command line tool if there was no output directory specified.

getLibraryDirectoryback to summary
public String getLibraryDirectory()

Return the Path to the directory in which ANTLR will search for ancillary files such as .tokens vocab files and imported grammar files.

Returns:String

the lib Directory

getLibraryFileback to summary
public String getLibraryFile(String fileName) throws IOException

Name a file from the -lib dir. Imported grammars and .tokens files If we do not locate the file in the library directory, then we try the location of the originating grammar.

Parameters
fileName:String

input name we are looking for

Returns:String

Path to file that we think shuold be the import file

getMakeback to summary
public boolean getMake()

Indicate whether the tool will analyze the dependencies of the provided grammar file list and ensure that grammars with dependencies are built after any of the other gramamrs in the list that they are dependent on. Setting this option also has the side effect that any grammars that are includes for other grammars in the list are excluded from individual analysis, which allows the caller to invoke the tool via org.antlr.tool -make *.g and not worry about the inclusion of grammars that are just includes for other grammars or what order the grammars appear on the command line. This option was coded to make life easier for tool integration (such as Maven) but may also be useful at the command line.

Returns:boolean

true if the tool is currently configured to analyze and sort grammar files.

getMessageFormatback to summary
public String getMessageFormat()

Returns the current setting of the message format descriptor

Returns:String

Current message format

getNumErrorsback to summary
public int getNumErrors()

Returns the number of errors that the analysis/processing threw up.

Returns:int

Error count

getOutputDirectoryback to summary
public File getOutputDirectory(String fileNameWithPath)

Return the location where ANTLR will generate output files for a given file. This is a base directory and output files will be relative to here in some cases such as when -o option is used and input files are given relative to the input directory.

Parameters
fileNameWithPath:String

path to input source

getOutputDirectoryback to summary
public String getOutputDirectory()

Return the Path to the base output directory, where ANTLR will generate all the output files for the current language target as well as any ancillary files such as .tokens vocab files.

Returns:String

the output Directory

getOutputFileback to summary
public Writer getOutputFile(Grammar g, String fileName) throws IOException

This method is used by all code generators to create new output files. If the outputDir set by -o is not present it will be created. The final filename is sensitive to the output directory and the directory where the grammar file was found. If -o is /tmp and the original grammar file was foo/t.g then output files go in /tmp/foo. The output dir -o spec takes precedence if it's absolute. E.g., if the grammar file dir is absolute the output dir is given precendence. "-o /tmp /usr/lib/t.g" results in "/tmp/T.java" as output (assuming t.g holds T.java). If no -o is specified, then just write to the directory where the grammar file was found. If outputDirectory==null then write a String.

getRootGrammarback to summary
public Grammar getRootGrammar(String grammarFileName) throws IOException

Get a grammar mentioned on the command-line and any delegates

helpback to summary
private static void help()
isDebugback to summary
public boolean isDebug()

Indicate if ANTLR has generated, or will generate a debug version of the recognizer. Debug versions of a parser communicate with a debugger such as that contained in ANTLRWorks and at start up will 'hang' waiting for a connection on an IP port (49100 by default).

Returns:boolean

the debug flag

isDependback to summary
public boolean isDepend()

Indicates whether ANTLR has supplied, or will supply, a list of all the things that the input grammar depends upon and all the things that will be generated when that grammar is successfully analyzed.

Returns:boolean

the depend flag

isForceAllFilesToOutputDirback to summary
public boolean isForceAllFilesToOutputDir()

Indicates whether ANTLR will force all files to the output directory, even if the input files have relative paths from the input directory.

Returns:boolean

the forceAllFilesToOutputDir flag

isGenerate_DFA_dotback to summary
public boolean isGenerate_DFA_dot()

Indicates whether ANTLR has generated or will generate a description of all the NFAs in Dot format

Returns:boolean

the generate_DFA_dot

isGenerate_NFA_dotback to summary
public boolean isGenerate_NFA_dot()

Indicates whether ANTLR has gnerated or will generate a description of all the NFAs in Dot format

Returns:boolean

the generate_NFA_dot

isPrintGrammarback to summary
public boolean isPrintGrammar()

Indicates whether ANTLR has printed, or will print, a version of the input grammar file(s) that is stripped of any action code embedded within.

Returns:boolean

the printGrammar flag

isProfileback to summary
public boolean isProfile()

Indicates whether ANTLR has generated or will generate a version of the recognizer that gathers statistics about its execution, which it prints when it terminates.

Returns:boolean

the profile

isReportback to summary
public boolean isReport()

Indicates whether ANTLR has generated or will generate a report of various elements of the grammar analysis, once it it has finished analyzing a grammar file.

Returns:boolean

the report flag

isTraceback to summary
public boolean isTrace()

Indicate whether ANTLR has generated, or will generate a version of the recognizer that prints trace messages on entry and exit of each rule.

Returns:boolean

the trace flag

isVerboseback to summary
public boolean isVerbose()

Indicates whether ANTLR will be verbose when analyzing grammar files, such as displaying the names of the files it is generating and similar information.

Returns:boolean

the verbose flag

loadResourcesback to summary
private void loadResources()

Load the properties file org/antlr/antlr.properties and populate any variables that must be initialized from it, such as the version of ANTLR.

mainback to summary
public static void main(String[] args)

A list of dependency generators that are accumulated aaaas (and if) the tool is required to sort the provided grammars into build dependency order. protected Map<String, BuildDependencyGenerator> buildDependencyGenerators;

panicback to summary
public void panic()

If the tool needs to panic/exit, how do we do that?

processback to summary
public void process()
processArgsback to summary
public void processArgs(String[] args)
setDebugback to summary
public void setDebug(boolean debug)

Indicate whether ANTLR should generate a debug version of the recognizer. Debug versions of a parser communicate with a debugger such as that contained in ANTLRWorks and at start up will 'hang' waiting for a connection on an IP port (49100 by default).

Parameters
debug:boolean

true to generate a debug mode parser

setDependback to summary
public void setDepend(boolean depend)

Indicate whether ANTLR should supply a list of all the things that the input grammar depends upon and all the things that will be generated when that gramamr is successfully analyzed.

Parameters
depend:boolean

true to get depends set rather than process the grammar

setForceAllFilesToOutputDirback to summary
public void setForceAllFilesToOutputDir(boolean forceAllFilesToOutputDir)

Indicates whether ANTLR will force all files to the output directory, even if the input files have relative paths from the input directory.

Parameters
forceAllFilesToOutputDir:boolean

true to force files to output directory

setForceRelativeOutputback to summary
public void setForceRelativeOutput(boolean forceRelativeOutput)

Used by build tools to force the output files to always be relative to the base output directory, even though the tool had to set the output directory to an absolute path as it cannot rely on the workign directory like command line invocation can.

Parameters
forceRelativeOutput:boolean

true if output files hould always be relative to base output directory

setGenerate_DFA_dotback to summary
public void setGenerate_DFA_dot(boolean generate_DFA_dot)

Indicates whether ANTLR should generate a description of all the NFAs in Dot format

Parameters
generate_DFA_dot:boolean

True to generate dot descriptions

setGenerate_NFA_dotback to summary
public void setGenerate_NFA_dot(boolean generate_NFA_dot)

Indicate whether ANTLR should generate a description of all the NFAs in Dot format

Parameters
generate_NFA_dot:boolean

True to generate dot descriptions

setGrammarFileNamesback to summary
public void setGrammarFileNames(List<String> grammarFileNames)

Provide the List of all grammar file names that the ANTLR tool should process.

Parameters
grammarFileNames:List<String>

The list of grammar files to process

setInputDirectoryback to summary
public void setInputDirectory(String inputDirectory)

Set the base location of input files. Normally (when the tool is invoked from the command line), the inputDirectory is not set, but for build tools such as Maven, we need to be able to locate the input files relative to the base, as the working directory could be anywhere and changing workig directories is not a valid concept for JVMs because of threading and so on. Setting the directory just means that the getFileDirectory() method will try to open files relative to this input directory.

Parameters
inputDirectory:String

Input source base directory

setLibDirectoryback to summary
public void setLibDirectory(String libDirectory)

Set the Path to the directory in which ANTLR will search for ancillary files such as .tokens vocab files and imported grammar files.

Parameters
libDirectory:String

the libDirectory to set

setMakeback to summary
public void setMake(boolean make)

Indicate whether the tool should analyze the dependencies of the provided grammar file list and ensure that the grammars with dependencies are built after any of the other gramamrs in the list that they are dependent on. Setting this option also has the side effect that any grammars that are includes for other grammars in the list are excluded from individual analysis, which allows the caller to invoke the tool via org.antlr.tool -make *.g and not worry about the inclusion of grammars that are just includes for other grammars or what order the grammars appear on the command line. This option was coded to make life easier for tool integration (such as Maven) but may also be useful at the command line.

setMaxSwitchCaseLabelsback to summary
public void setMaxSwitchCaseLabels(int maxSwitchCaseLabels)

Set the threshold of case labels beyond which ANTLR will not instruct the target template to generate switch() { case xxx: ...

Parameters
maxSwitchCaseLabels:int

Maximum number of case lables that ANTLR should allow the target code

setMessageFormatback to summary
public void setMessageFormat(String format)

Set the message format to one of ANTLR, gnu, vs2005

setMinSwitchAltsback to summary
public void setMinSwitchAlts(int minSwitchAlts)

Set the threshold of the number alts, below which ANTLR will not instruct the target template to use a switch statement.

Parameters
minSwitchAlts:int

the minimum number of alts required to use a switch staement

setOutputDirectoryback to summary
public void setOutputDirectory(String outputDirectory)

Set the location (base directory) where output files should be produced by the ANTLR tool.

setPrintGrammarback to summary
public void setPrintGrammar(boolean printGrammar)

Indicate whether ANTLR should print a version of the input grammar file(s) that is stripped of any action code embedded within.

Parameters
printGrammar:boolean

true to generate a stripped file

setProfileback to summary
public void setProfile(boolean profile)

Indicate whether ANTLR should generate a version of the recognizer that gathers statistics about its execution, which it prints when it terminates.

Parameters
profile:boolean

true to generate a profiling parser

setReportback to summary
public void setReport(boolean report)

Indicate whether ANTLR should generate a report of various elements of the grammar analysis, once it it has finished analyzing a grammar file.

Parameters
report:boolean

true to generate the analysis report

setTraceback to summary
public void setTrace(boolean trace)

Indicate whether ANTLR should generate a version of the recognizer that prints trace messages on entry and exit of each rule

Parameters
trace:boolean

true to generate a tracing parser

setVerboseback to summary
public void setVerbose(boolean verbose)

Indicate whether ANTLR should be verbose when analyzing grammar files, such as displaying the names of the files it is generating and similar information.

Parameters
verbose:boolean

true to be verbose

sortGrammarFilesback to summary
public void sortGrammarFiles() throws IOException
versionback to summary
private static void version()
writeDOTFileback to summary
protected void writeDOTFile(Grammar g, Rule r, String dot) throws IOException
writeDOTFileback to summary
protected void writeDOTFile(Grammar g, String name, String dot) throws IOException
Xhelpback to summary
private static void Xhelp()