ANTLR disable specific Userpath in Comments and re

2019-09-01 03:17发布

问题:

Always when i generate Parser and Lexer from my Grammarfile, everything works fine instead of the problem, that ANTLR automaticly adds the whole User directory to any returns and comments. That is not good. E.g.:

return "C:\\Users\\##\\workspace\\project\\src\\Grammar.g"

also this tool does the same with any comments:

// $ANTLR 3.4 C:\\Users\\##\\workspace\\project\\src\\Grammar.g 2012-06-18 18:25:20

i have to publish my project so is there any way to disable this "function" ?

Regards, redstreak.

回答1:

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.

This answer is from my own question Remove (absolute) paths in ANTLR generated java classes: which at the end I answered myself.