3

I don't like to have the ANTLR generated files in the same location as my .g4 grammar and other version-controlled documents. I would like to put all the generated files in a separate nested folder that can be excluded via .gitignore. What I have tried so far is to create a folder <someFolder> inside the main folder and from inside that folder ran:

cd <someFolder>
antlr ../<grammarFile.g4>

however, it still spills all the generated files in the parent folder.

P.S. It shouldn't matter but my environment is macOS.

Foad S. Farimani
  • 12,396
  • 15
  • 78
  • 193

1 Answers1

4

From https://github.com/antlr/antlr4/blob/master/doc/tool-options.md:

-o outdir

ANTLR generates output files in the current directory by default. This option specifies the output directory where ANTLR should generate parsers, listeners, visitors, and tokens files.

in the example above, from the main folder run

antlr <grammarFile.g4> -o <someFolder>
Foad S. Farimani
  • 12,396
  • 15
  • 78
  • 193
jurez
  • 4,436
  • 2
  • 12
  • 20
  • 9
    Additional note: if you run the antlr4 jar with a path to your grammar(s) (not using a grammar in the same folder) it will recreate the same folder path structure in the output dir (a reminiscence to Java where bundles are represented by folders). If you don't want that use the additional parameter `-Xexact-output-dir` – Mike Lischke Apr 04 '21 at 14:50
  • @MikeLischke do you think that's the reason I'm stuck with [this issue](https://stackoverflow.com/q/66942445/4999991)? – Foad S. Farimani Apr 04 '21 at 14:59
  • I can't say, as I'm not using Java that much. – Mike Lischke Apr 05 '21 at 09:17
  • @MikeLischke I followed your instructions from [here](https://stackoverflow.com/a/66957324/4999991) and ran it on the results from this page, but I get [these error messages](https://pastebin.com/MR1Y9XNQ). I tried the `-packge` option from [this page](https://stackoverflow.com/questions/47121549/antlr4-testrig-grun-throws-java-lang-noclassdeffounderror-exception), but it didn't work. – Foad S. Farimani Apr 05 '21 at 18:28
  • 1
    @MikeLischke Thanks a lot! -Xexact-output-dir is exactly what i'm looking for! – Igor Sep 08 '21 at 08:38