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!
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.
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
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