1

在执行 java -cp C:\Tools\Libraries\antlr4-csharp-complete.jar org.antlr.v4.Tool Hello.g4 时,我得到以下文件:

HelloBaseListener.cs
Hello.tokens
HelloListener.cs
HelloParser.cs
HelloLexer.tokens
HelloLexer.java

我的问题是关于最后一个文件。为什么是 .java 而不是 .cs?我正在使用 antlr4-csharp-4.0.1-SNAPSHOT-complete.jar

语法是:

grammar Hello; // Define a grammar called Hello
options
{
    language=CSharp_v4_0;   
}


r : 'hello' ID ; // match keyword hello followed by an identifier
ID : [a-z]+ ; // match lower-case identifiers
WS : [ \t\r\n]+ -> skip ; // skip spaces, tabs, newlines, \r (Windows)

你好山姆!我只有 Visual Studio Express,所以我无法安装扩展。这是我正在使用的代码,但它仍在生成 HelloLexer.java。

AntlrClassGenerationTaskInternal a = new AntlrClassGenerationTaskInternal();
List<String> files = new List<string>();
files.Add(@"C:\Tools\Grammars\Hello.g4");
a.JavaVendor = "JavaSoft";
a.ToolPath = @"C:\Tools\Libraries\antlr4-csharp-complete.jar";
a.JavaInstallation = "Java Development Kit";
a.SourceCodeFiles = files;
a.OutputPath = @"C:\Tools\Grammars\CSharp\";

a.Execute();

顺便说一句,Visual Studio 抱怨,因为它无法找到 Antlr4ClassGenerationTask.IsFatalException(ex)

感谢您在这方面的帮助。

问候,奥马尔。

4

2 回答 2

2

Edit 11/20/13: Updated instructions are now available on the project wiki

https://github.com/sharwell/antlr4cs/wiki/Installation


Here are a few messages I sent over the past few months related to this issue. If you don't want to install the Visual Studio extension described below, you'll need to use the source code of Antlr4ClassGenerationTaskInternal.cs to determine a set of command line options that will work.

Also, you can remove the language=CSharp_v4_0; option because it's passed on the command line now.


The C# target wasn't designed for command line usage. You will need to integrate the code generation into your project file according to the instructions on the following page, and the parsers will be generated automatically when you build your project.

https://github.com/sharwell/antlr4cs


You do need to include the .g4 file in your project and configure a few properties of the file. If you install the following extension before adding the grammar to your project, all the other options will be configured for you automatically.
ANTLR Language Support for Visual Studio 2010- 2012

If you already have the .g4 file in your project, and want to still use the extension to automatically configure the proper settings, you can do the following:

  1. Install the extension.
  2. Click the project in Solution Explorer and enable Show All Files (button on the Solution Explorer toolbar). This step greatly simplifies step 4.
  3. Right click the .g4 file in the project, and select Exclude From Project.
  4. Right click the .g4 file again and select Include In Project.
  5. (Optional) You can disable Show All Files when you no longer need it.
于 2013-07-26T11:20:42.930 回答
2

我认为这是一个错误,我遇到了同样的问题,但有一个解决方案。

1)如果你愿意,你可以删除

options
{
    language=CSharp_v4_0;   
}

我认为被代码生成器忽略

2)使用以下代码创建一个BAT文件

@echo OFF
IF "%CLASSPATH%" == "" (SET CLASSPATH=.;.\antlr4-csharp-4.0.1-SNAPSHOT-complete.jar;%CLASSPATH%)
java org.antlr.v4.Tool %* -Dlanguage=CSharp_v4_5

将antlr4-csharp-4.0.1-SNAPSHOT-complete.jar 放在同一个路径下,现在就可以使用这个文件来编译了。要解决此问题,神奇的命令行参数是“-Dlanguage=CSharp_v4_5”或您正在使用的 C# 版本。

生成的文件现在位于 Lexer.cs 中

于 2013-10-31T15:47:14.233 回答