Antlr4: ANTLR generate code into incorrect path

Created on 23 Aug 2014  路  5Comments  路  Source: antlr/antlr4

If I use following string to generate lexer and parser from grammar:

$ java -cp antlr4-complete-4.4-bin.jar org.antlr.v4.Tool -o src/my/package -package my.package MyGrammar.g4

code will be placed into correct path -- into src/my/package directory.
But if I use following string:

$ java -cp antlr4-complete-4.4-bin.jar org.antlr.v4.Tool -o src/my/package -package my.package grammars/MyGrammar.g4

code will be placed into src/my/package/grammars directory! Also one directory grammars will be added!

not-fixing

Most helpful comment

Hi Sam,
I totally agreed with Terence and others. The change would make easier to understand and save us lots unnecessary issue and workaround because of the implicit assumption by the Tool.

Thanks for understanding and hope it will be included in the next version and thanks the team for this wonderful parser generation tool
Cheers,
Paul

All 5 comments

ANTLR assumes you have your grammar itself in a folder structure similar to the structure javac uses for Java source files. For example, you could use src/antlr4/my/package/MyGrammar.g4 for your grammar, and gen/antlr4/my/package/MyGrammar.g4 for your generated code. If you have your grammar in a folder which does not correspond to the folder structure where your generated code is placed, you'll need to account for the folder structure created by the ANTLR 4 tool.

An example of this scenario can be seen in the ANTLRWorks 2 build script. In it I set the dir attribute for the <java> element, and then adjust the -o and -package command line arguments for ANTLR to account for the location in which the tool is running.

https://github.com/tunnelvisionlabs/antlrworks2/blob/44309a5e2c44e79020110304bbd30ab4cc41cae9/org-antlr-works-editor/build.xml#L95-L99

Closing as by-design.

Thanks for your response!

But I have a question about this situation:
why ANTLR Tool not set a correct package into generated Java-classes in this case?

I know that I can use -package option for this, but if ANTLR Tool create output path according to grammar path without addition options it can do the same for package values (I think that it will be expected behaviour).

I think our argument was that -o should specify exactly where the file goes. There are some languages that do not map packages to directory structures I would imagine.

Hi Sam,
I totally agreed with Terence and others. The change would make easier to understand and save us lots unnecessary issue and workaround because of the implicit assumption by the Tool.

Thanks for understanding and hope it will be included in the next version and thanks the team for this wonderful parser generation tool
Cheers,
Paul

Was this page helpful?
0 / 5 - 0 ratings