Package org.antlr

Class Tool

java.lang.Object
org.antlr.Tool

public class Tool extends Object
The main ANTLR entry point. Read a grammar and generate a parser.
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
     
    private boolean
     
    protected boolean
     
    private boolean
     
    private static boolean
     
    private boolean
     
     
    private boolean
     
    private boolean
     
    private boolean
     
    private List<String>
     
    private String
     
    private boolean
     
    private boolean
     
    private String
     
    static boolean
     
    static boolean
     
    static boolean
     
    static boolean
     
    private String
     
    private boolean
    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
     
    static final String
     
    private boolean
     
    final String
     
  • Constructor Summary

    Constructors
    Constructor
    Description
     
    Tool(String[] args)
     
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    addGrammarFile(String grammarFileName)
     
    boolean
    buildRequired(String 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).
    void
     
    protected void
     
    protected void
    Create NFA, DFA and generate code for grammar.
    int
    Provide the current setting of the conversion timeout on DFA creation.
    static String
    Return a time stamp string accurate to sec: yyyy-mm-dd hh:mm:ss
    Return the directory containing the grammar file for this grammar.
    Provide the List of all grammar file names that the ANTLR tool will process or has processed.
    Return a File descriptor for vocab file.
    Return the Path to the directory in which ANTLR will search for ancillary files such as .tokens vocab files and imported grammar files.
    Name a file from the -lib dir.
    boolean
    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.
    Returns the current setting of the message format descriptor
    int
    Returns the number of errors that the analysis/processing threw up.
    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.
    getOutputDirectory(String fileNameWithPath)
    Return the location where ANTLR will generate output files for a given file.
    This method is used by all code generators to create new output files.
    getRootGrammar(String grammarFileName)
    Get a grammar mentioned on the command-line and any delegates
    private static void
     
    boolean
    Indicate if ANTLR has generated, or will generate a debug version of the recognizer.
    boolean
    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.
    boolean
    Indicates whether ANTLR will force all files to the output directory, even if the input files have relative paths from the input directory.
    boolean
    Indicates whether ANTLR has generated or will generate a description of all the NFAs in Dot format
    boolean
    Indicates whether ANTLR has gnerated or will generate a description of all the NFAs in Dot format
    boolean
    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.
    boolean
    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.
    boolean
    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.
    boolean
    Indicate whether ANTLR has generated, or will generate a version of the recognizer that prints trace messages on entry and exit of each rule.
    boolean
    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
    Load the properties file org/antlr/antlr.properties and populate any variables that must be initialized from it, such as the version of ANTLR.
    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.
    void
    If the tool needs to panic/exit, how do we do that?
    void
     
    void
     
    void
    setDebug(boolean debug)
    Indicate whether ANTLR should generate a debug version of the recognizer.
    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.
    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.
    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.
    void
    setGenerate_DFA_dot(boolean generate_DFA_dot)
    Indicates whether ANTLR should generate a description of all the NFAs in Dot format
    void
    setGenerate_NFA_dot(boolean generate_NFA_dot)
    Indicate whether ANTLR should generate a description of all the NFAs in Dot format
    void
    setGrammarFileNames(List<String> grammarFileNames)
    Provide the List of all grammar file names that the ANTLR tool should process.
    void
    setInputDirectory(String inputDirectory)
    Set the base location of input files.
    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.
    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.
    void
    setMaxSwitchCaseLabels(int maxSwitchCaseLabels)
    Set the threshold of case labels beyond which ANTLR will not instruct the target template to generate switch() { case xxx: ...
    void
    Set the message format to one of ANTLR, gnu, vs2005
    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.
    void
    setOutputDirectory(String outputDirectory)
    Set the location (base directory) where output files should be produced by the ANTLR tool.
    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.
    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.
    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.
    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
    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.
    void
     
    private static void
     
    protected void
     
    protected void
     
    private static void
     

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • antlrSettings

      public final Properties antlrSettings
    • VERSION

      public final String VERSION
    • UNINITIALIZED_DIR

      public static final String UNINITIALIZED_DIR
      See Also:
    • grammarFileNames

      private List<String> grammarFileNames
    • generate_NFA_dot

      private boolean generate_NFA_dot
    • generate_DFA_dot

      private boolean generate_DFA_dot
    • outputDirectory

      private String outputDirectory
    • haveOutputDir

      private boolean haveOutputDir
    • inputDirectory

      private String inputDirectory
    • parentGrammarDirectory

      private String parentGrammarDirectory
    • grammarOutputDirectory

      private String grammarOutputDirectory
    • haveInputDir

      private boolean haveInputDir
    • libDirectory

      private String libDirectory
    • debug

      private boolean debug
    • trace

      private boolean trace
    • profile

      private boolean profile
    • report

      private boolean report
    • printGrammar

      private boolean printGrammar
    • depend

      private boolean depend
    • forceAllFilesToOutputDir

      private boolean forceAllFilesToOutputDir
    • forceRelativeOutput

      private boolean forceRelativeOutput
    • deleteTempLexer

      protected boolean deleteTempLexer
    • verbose

      private boolean verbose
    • make

      private boolean make
      Don't process grammar file if generated files are newer than grammar
    • showBanner

      private boolean showBanner
    • exitNow

      private static boolean exitNow
    • return_dont_exit

      private static boolean return_dont_exit
    • forcedLanguageOption

      public String forcedLanguageOption
    • internalOption_PrintGrammarTree

      public static boolean internalOption_PrintGrammarTree
    • internalOption_PrintDFA

      public static boolean internalOption_PrintDFA
    • internalOption_ShowNFAConfigsInDFA

      public static boolean internalOption_ShowNFAConfigsInDFA
    • internalOption_watchNFAConversion

      public static boolean internalOption_watchNFAConversion
  • Constructor Details

    • Tool

      public Tool()
    • Tool

      public Tool(String[] args)
  • Method Details

    • main

      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;
    • loadResources

      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.
    • processArgs

      public void processArgs(String[] args)
    • buildRequired

      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 - The grammar file we are checking
      Throws:
      IOException
    • process

      public void process()
    • sortGrammarFiles

      public void sortGrammarFiles() throws IOException
      Throws:
      IOException
    • getRootGrammar

      public Grammar getRootGrammar(String grammarFileName) throws IOException
      Get a grammar mentioned on the command-line and any delegates
      Throws:
      IOException
    • generateRecognizer

      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.
    • generateDFAs

      public void generateDFAs(Grammar g)
    • generateNFAs

      protected void generateNFAs(Grammar g)
    • writeDOTFile

      protected void writeDOTFile(Grammar g, Rule r, String dot) throws IOException
      Throws:
      IOException
    • writeDOTFile

      protected void writeDOTFile(Grammar g, String name, String dot) throws IOException
      Throws:
      IOException
    • version

      private static void version()
    • help

      private static void help()
    • Xhelp

      private static void Xhelp()
    • setMaxSwitchCaseLabels

      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 - Maximum number of case lables that ANTLR should allow the target code
    • setMinSwitchAlts

      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 - the minimum number of alts required to use a switch staement
    • setOutputDirectory

      public void setOutputDirectory(String outputDirectory)
      Set the location (base directory) where output files should be produced by the ANTLR tool.
      Parameters:
      outputDirectory -
    • setForceRelativeOutput

      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 - true if output files hould always be relative to base output directory
    • setInputDirectory

      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 - Input source base directory
    • getOutputFile

      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.
      Throws:
      IOException
    • getOutputDirectory

      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 - path to input source
    • getLibraryFile

      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 - input name we are looking for
      Returns:
      Path to file that we think shuold be the import file
      Throws:
      IOException
    • getFileDirectory

      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.
    • getImportedVocabFile

      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.
    • panic

      public void panic()
      If the tool needs to panic/exit, how do we do that?
    • getCurrentTimeStamp

      public static String getCurrentTimeStamp()
      Return a time stamp string accurate to sec: yyyy-mm-dd hh:mm:ss
    • getGrammarFileNames

      public List<String> getGrammarFileNames()
      Provide the List of all grammar file names that the ANTLR tool will process or has processed.
      Returns:
      the grammarFileNames
    • isGenerate_NFA_dot

      public boolean isGenerate_NFA_dot()
      Indicates whether ANTLR has gnerated or will generate a description of all the NFAs in Dot format
      Returns:
      the generate_NFA_dot
    • isGenerate_DFA_dot

      public boolean isGenerate_DFA_dot()
      Indicates whether ANTLR has generated or will generate a description of all the NFAs in Dot format
      Returns:
      the generate_DFA_dot
    • getOutputDirectory

      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:
      the output Directory
    • getLibraryDirectory

      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:
      the lib Directory
    • isDebug

      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:
      the debug flag
    • isTrace

      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:
      the trace flag
    • isProfile

      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:
      the profile
    • isReport

      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:
      the report flag
    • isPrintGrammar

      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:
      the printGrammar flag
    • isDepend

      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:
      the depend flag
    • isForceAllFilesToOutputDir

      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:
      the forceAllFilesToOutputDir flag
    • isVerbose

      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:
      the verbose flag
    • getConversionTimeout

      public int getConversionTimeout()
      Provide the current setting of the conversion timeout on DFA creation.
      Returns:
      DFA creation timeout value in milliseconds
    • getMessageFormat

      public String getMessageFormat()
      Returns the current setting of the message format descriptor
      Returns:
      Current message format
    • getNumErrors

      public int getNumErrors()
      Returns the number of errors that the analysis/processing threw up.
      Returns:
      Error count
    • getMake

      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:
      true if the tool is currently configured to analyze and sort grammar files.
    • setMessageFormat

      public void setMessageFormat(String format)
      Set the message format to one of ANTLR, gnu, vs2005
      Parameters:
      format -
    • setGrammarFileNames

      public void setGrammarFileNames(List<String> grammarFileNames)
      Provide the List of all grammar file names that the ANTLR tool should process.
      Parameters:
      grammarFileNames - The list of grammar files to process
    • addGrammarFile

      public void addGrammarFile(String grammarFileName)
    • setGenerate_NFA_dot

      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 - True to generate dot descriptions
    • setGenerate_DFA_dot

      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 - True to generate dot descriptions
    • setLibDirectory

      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 - the libDirectory to set
    • setDebug

      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 - true to generate a debug mode parser
    • setTrace

      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 - true to generate a tracing parser
    • setProfile

      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 - true to generate a profiling parser
    • setReport

      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 - true to generate the analysis report
    • setPrintGrammar

      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 - true to generate a stripped file
    • setDepend

      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 - true to get depends set rather than process the grammar
    • setForceAllFilesToOutputDir

      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 - true to force files to output directory
    • setVerbose

      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 - true to be verbose
    • setMake

      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.
      Parameters:
      make -