Where does ANTLR write output files?

Skip to end of metadata
Go to start of metadata

ANTLR writes files to the current directory by default. But, the output filename is sensitive to the output directory and the directory where the grammar file was found. If you reference a grammar with a (relative or absolute) path,
ANTLR will pull the grammar from that directory and write the output to that directory:

If you specify an output directory with -o then ANTLR will put the output files in that directory or underneath if you have a relative path on the grammar file:

The output directory -o value takes precedence over the grammar's path when the output directory is absolute.

Use -fo option to force output to go explicitly into a directory, ignoring any path on the input grammar name.

Note: If the outputDir set by -o is not present it will be created.

  1. Dec 03, 2008

    How does Anltr / AntlrWorks resolve "import" statements? The It Just Works approach fails. Somewhere, I need to specify which folders to search when attempting resolve imports...

  2. Dec 03, 2008

    It should look in the -lib dir.

  3. Dec 03, 2008

    Showing real ignorance here: and how do I get that lib option passed to Antlr from the "java -jar antlrworks-1.2.2.jar" command line?

  4. Dec 03, 2008

    Oh, in AW, use its preferences. Should be in there somewhere. Yep, put -lib foo in antlr options of general pane in preferences.

  5. Dec 03, 2008

    Passed by both these guideposts while googling for the answer--thanks for the human interaction!

  6. Dec 03, 2008

    Yep, all pilot error. I had a stray character in one of the grammar files, Deleting that and observing the import rules made all the difference.

  7. Mar 26, 2009

    I am new to java and when i run from command prompt i get the following :

    its the same when i run from the Visual studio solution...

    D:\antlr>java -jar antlr-3.1.3.jar org.antlr.Tool C.g
    error(10):  internal error:  : java.io.FileNotFoundException: org.antlr.Tool (The system cannot find the file specified)
    java.io.FileInputStream.open(Native Method)
    java.io.FileInputStream.<init>(Unknown Source)
    java.io.FileInputStream.<init>(Unknown Source)
    java.io.FileReader.<init>(Unknown Source)
    org.antlr.tool.GrammarSpelunker.parse(GrammarSpelunker.java:79)
    org.antlr.Tool.sortGrammarFiles(Tool.java:525)
    org.antlr.Tool.process(Tool.java:383)
    org.antlr.Tool.main(Tool.java:91)
    error(7):  cannot find or open file: org.antlr.Tool

    Please help.......

  8. Mar 26, 2009

    Sorry about that; depends on if jar file has a manifest. Try:

    java -jar antlr-3.1.3.jar C.g

  9. Aug 02, 2011

    I find a difference in how ANTLR 3.2 handles -o output generation between Linux and Windows.

    Consider a grammar in src/com/example/parser/A.g that contains package declarations:

    @parser::header {
    package com.example.parser;
    }
    @lexer::header {
    package com.example.parser;
    }
    

    On Windows,

    cd src
    java org.antlr.Tool -o ..\out com/example/parser/A.g
    

    will create

    • AParser.java
    • A.tokens
    • ALexer.java

    in the out directory whereas on Linux,

    java org.antlr.Tool -o ../out com/example/parser/A.g
    

    (using the same antlr 3.2 jar in the classpath) yields

    • A.tokens
    • com/example/parser/AParser.java
    • com/example/parser/ALexer.java

    That is, on Linux, ANTLR is creating files in package directories, but on Windows it is not.

    I'm using Java 1.6.0_24 in both.

    I have found I have to use

    -fo out/com/example/parser
    

    (that is, specify the package directory) in my Ant build.xml or Maven pom.xml - this works in both places. This means the parser and grammar must be in the same package.