问题描述
我正在将ANTLRWorks 1.4.3与ANTLR 3.4一起使用,以从语法文件中生成基于Java的解析器和词法分析器.生成的.java
文件包含类似
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
此绝对路径用作
- 返回字符串,例如在词法分析器和解析器的方法
getGrammarFileName()
中,以及 - 在这两个文件中都多次作为注释.
- return string e.g. in method
getGrammarFileName()
of lexer and parser, and - throughout the both files various times as comment.
我看到以下缺点:
- 如果其他人在他的开发环境中具有不同的路径将重新生成这些文件,那么即使没有对语法文件进行任何更改,也会引入很多更改.
- 没有人,尤其是在开放源代码项目中,不需要知道我将语法文件确切存储在哪里.例如
C:\\Users\\simon\\customerA\\crap_software\\[rest of the path to grammar file]
在ANTLRWorks或ANTLR中是否有控制此方法的方法.至少只使用相对路径?
Is there a way to control this in ANTLRWorks or ANTLR s.th. at least only relative paths are used?
推荐答案
最后,我找到了解决自己的问题的方法.
Finally I found a way to solve my own problem.
路径似乎取决于调用ANTLR的位置和方式.我无法使用ANTLRWorks实现此功能,但是使用命令行ANTLR可以执行此操作.您可以执行以下操作(示例适用于Windows,但在其他操作系统上也应可重现):
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):
-
下载 Antlr作为命令行并将其复制到例如
Download Antlr for command line and copy it to e.g.
C:\Program Files (x86)\ANTLRworks\antlr-3.4-complete.jar
.
打开Windows命令行(cmd.exe
)并转到语法文件所在的目录:
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
调用
java -jar "C:\Program Files (x86)\ANTLRworks\antlr-3.4-complete.jar" MyGrammar.g
从命令行.
生成的Java文件将仅包含语法文件的名称,并且不再包含路径.
The generated java files will only contain the name of your grammar file and no path anymore.
这篇关于删除ANTLR生成的Java类中的(绝对)路径:的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!