我获得了 Java 编译器子集(称为静态 Java 编译器)的 ANTLR 语法。我正在尝试扩展语法以包含更多 Java 功能,例如,我刚刚添加了 For 循环的语法。
然后,我使用 Eclipse 和 ANTLR 插件进行了“编译 ANTLR 语法”。它没有编译,而是在代码的第一位产生了两个错误:
grammar ExtendedStaticJava;
options { backtrack=true; memoize=true; }
@header
{
package sjc.parser.extended;
import java.math.BigInteger;
/**
* Extended StaticJava parser.
* @author <myname>
*/
}
// the rest of the grammar has been excluded.
第一个错误位于第 1 行:“意外的标记:语法”
第二个错误位于第 5 行:“意外的字符:@”
为什么它不能识别这个基本的 ANTLR 语法?我的第一个想法是我在类路径中丢失了某些内容,但我转到了项目的属性并确保以下 JAR 包含在库下:
- antlr-2.7.7.jar
- stringtemplate-3.2。 1.jar
- antlr.jar
- antlr-runtime-3.0.1.jar
有什么想法或建议吗?
I have been given an ANTLR grammar for a subset of the Java compiler known as the static Java compiler. I am trying to extend the grammar to include more of Java's features, for instance, I just added the grammar for For Loops.
Using Eclipse and an ANTLR plugin, I then did 'Compile ANTLR grammar'. Instead of compiling it produced two errors on the first bit of code:
grammar ExtendedStaticJava;
options { backtrack=true; memoize=true; }
@header
{
package sjc.parser.extended;
import java.math.BigInteger;
/**
* Extended StaticJava parser.
* @author <myname>
*/
}
// the rest of the grammar has been excluded.
The first error is on line 1: 'Unexpected Token: grammar'
The second error is on line 5: 'Unexpected char: @'
Why does it not recognize this basic ANTLR syntax? My first thought was that I was missing something in the classpath, but I went to the project's properties and made sure that the following JARs were included under Libraries:
- antlr-2.7.7.jar
- stringtemplate-3.2.1.jar
- antlr.jar
- antlr-runtime-3.0.1.jar
Any ideas or suggestions?
发布评论
评论(3)
antlr-2.7.7.jar
是错误的:您的语法采用 ANTLR v3+ 语法。 删除所有:从项目/类路径中 ,并且仅粘贴 这个 ANTLR v3 JAR (其中包含您需要的一切:运行时、字符串模板、作品!)。
祝你好运!
编辑
就个人而言,我使用 Ant 构建脚本进行 IDE 集成。我生成一个像这样的词法分析器和解析器:
并且
classpath
refid
看起来像:并且
lib/
目录包含 ANTLR v3 JAR(没有其他ANTLR JAR!(抱歉,敲击了:)
))antlr-2.7.7.jar
is wrong: your grammar is in ANTLR v3+ syntax. Remove all:from your project/classpath and only stick this ANTLR v3 JAR in it (which contains everything you need: runtime, stringtemplate, the works!).
Good luck!
EDIT
Personally, I do my IDE integration with an Ant build script. I generate a lexer and parser like this:
and the
classpath
refid
looks like:and the
lib/
directory contains the ANTLR v3 JAR (no other ANTLR JARs! (sorry for hammering on that:)
))您可能已经安装了 AntlrEclipse 插件。这确实使用了 Antlr 2.7.something。不过,有一个可与 v3 配合使用的插件,Antlr IDE。这个插件稍微先进一点,因为它允许您配置 Antlr 版本(3+),并且可以显示铁路视图并具有解释器。
You have probably installed the AntlrEclipse plugin. This indeed uses Antlr 2.7.something. There is however a plugin available that works with v3, Antlr IDE. This plugin is a little bit more advanced as it allows you to configure the Antlr version (3+) and can show a railroad view and has an interpreter.
我也有这个问题。
在我尝试运行 antlr 的过程中,我曾将某个版本的 jar 文件添加到我的类路径 Windows 环境变量中。我删除了此条目,它解决了我的问题。
I had this issue as well.
At some point during my attempts to get antlr running I had added some version of the jar file to my classpath windows environment variable. I removed this entry, and it resolved my issue.