删除 ANTLR 生成的 java 类中的(绝对)路径: [英] Remove (absolute) paths in ANTLR generated java classes:

查看:21
本文介绍了删除 ANTLR 生成的 java 类中的(绝对)路径:的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 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() 中,以及
  • 在这两个文件中多次作为评论.

我看到以下缺点:

  • 如果其他人在他的开发环境中使用不同的路径重新生成这些文件,即使没有对语法文件进行任何更改,也会引入很多更改.
  • 没有人,尤其是在开源项目中,需要知道我的语法文件的确切存储位置.例如,C:\\Users\\simon\\customerA\\crap_software\\[语法文件的其余路径]

有没有办法在 ANTLRWorks 或 ANTLR s.th. 中控制它?至少只使用相对路径?

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):

  1. 下载 命令行的 Antlr 并将其复制到例如

  1. 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[eclipse项目路径]\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 类中的(绝对)路径:的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆