3

I am using ANTLRWorks 1.4.3 together with ANTLR 3.4 to generate a Java-based parser and lexer from a grammar file. The generated .java-files contain strings like

C:\\Users\\[path to the eclipse project]\\src\\some\\package\\name\\MyGrammar.g

This absolute path is used as

  • return string e.g. in method getGrammarFileName() of lexer and parser, and
  • throughout the both files various times as comment.

I see following disadvantages:

  • If somebody else with different paths in his development environment will regenerate these files, a lot of changes will be introduced even if no changes in the grammar file were done.
  • Nobody, especially in an open source project, needs to know where I exactly store my grammar files. E.g., what about C:\\Users\\simon\\customerA\\crap_software\\[rest of the path to grammar file]

Is there a way to control this in ANTLRWorks or ANTLR s.th. at least only relative paths are used?

SimonTheSorcerer
  • 500
  • 4
  • 13
  • I just noticed there is an related (and duplicate) question: http://stackoverflow.com/questions/11087097/antlr-disable-specific-userpath-in-comments-and-returns – SimonTheSorcerer Sep 12 '12 at 11:55

2 Answers2

2

Finally I found a way to solve my own problem.

Paths seem to depend from where and how you invoke ANTLR. I was not able to achieve this with ANTLRWorks, but using command line ANTLR you are able to perform this. You can do the following (example is for Windows but should be reproducible on other OSes, too):

  1. Download Antlr for command line and copy it to e.g.

    C:\Program Files (x86)\ANTLRworks\antlr-3.4-complete.jar.

  2. Open a Windows command line (cmd.exe) and change to the directory where your grammar file is located:

    cd C:\Users[path to the eclipse project]\src\some\package\name

  3. Invoke

    java -jar "C:\Program Files (x86)\ANTLRworks\antlr-3.4-complete.jar" MyGrammar.g
    

    from commandline.

The generated java files will only contain the name of your grammar file and no path anymore.

SimonTheSorcerer
  • 500
  • 4
  • 13
  • Worth mentioning, I've created an issue to address this with ANTLR: https://github.com/antlr/antlr4/issues/974 – Groostav Aug 14 '15 at 17:44
0

Is there a way to control this in ANTLRWorks or ANTLR s.th. at least only relative paths are used?

Short answer

No.

Long(er) answer (Containing highly subjective views! Proceed at own risk)

This is target-specific, but, AFAIK, no target allows you to specify the type (absolute or relative) of the path.

The "no" might be because getGrammarFileName() is only used while debugging generated lexers/parsers. And one should probably not check in generated source files into source control, so no one would ever see the path you see in your generated source file. One ought to check in the grammar, and let developers generate their own lexers/parsers from it.

Again, this is all speculation on my part.

Bart Kiers
  • 166,582
  • 36
  • 299
  • 288
  • IMHO there is no definite "you should not check in generated files to version control", see http://stackoverflow.com/questions/893913/should-i-store-generated-code-in-source-control. In the case I am working on those files are under version control. – SimonTheSorcerer Sep 12 '12 at 11:52
  • @SimonTheSorcerer, sure, there are different views on whether to put generated files in source control (let's not get into that). I merely mentioned it to (possibly?) account for the fact you can't change this path (which is what your question is/was, and which is answered). – Bart Kiers Sep 12 '12 at 11:59
  • You are right. Sorry, if you misunderstood. I just wanted to point out, that I do not have the possibility to decide whether this will be taken out of version control. So for the time being I have to live with the answer "No". :( – SimonTheSorcerer Sep 12 '12 at 12:03
  • @SimonTheSorcerer, yeah, I'm afraid you do :( – Bart Kiers Sep 12 '12 at 12:12
  • I have found a solution, just take a look at my own answer. Thank you for your effort. – SimonTheSorcerer Sep 14 '12 at 09:38