为什么我的 Java ANTLR 语法文件无法编译?

发布于 2024-10-21 01:17:38 字数 737 浏览 2 评论 0 原文

我获得了 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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

与风相奔跑 2024-10-28 01:17:38

antlr-2.7.7.jar 是错误的:您的语法采用 ANTLR v3+ 语法。 删除所有:

  • antlr-2.7.7.jar
  • stringtemplate-3.2.1.jar
  • antlr.jar
  • antlr-runtime-3.0.1.jar

从项目/类路径中 ,并且仅粘贴 这个 ANTLR v3 JAR (其中包含您需要的一切:运行时、字符串模板、作品!)。

祝你好运!


编辑

就个人而言,我使用 Ant 构建脚本进行 IDE 集成。我生成一个像这样的词法分析器和解析器:

<target name="generate.parser" depends="init" description="Generates the lexer, parser and tree-walker from the grammar files.">
    <echo>Generating the lexer and parser...</echo>
    <java classname="org.antlr.Tool" fork="true" failonerror="true">
        <arg value="-fo" />
        <arg value="${main.src.dir}/${parser.package}" />
        <arg value="${parser.grammar.file}" />
        <classpath refid="classpath" />
    </java>
    <!-- snip -->
</target>

并且 classpath refid 看起来像:

<path id="classpath">
    <!-- snip -->
    <fileset dir="lib">
        <include name="*.jar" />
    </fileset>
</path>

并且 lib/ 目录包含 ANTLR v3 JAR(没有其他ANTLR JAR!(抱歉,敲击了 :)))

antlr-2.7.7.jar is wrong: your grammar is in ANTLR v3+ syntax. Remove all:

  • antlr-2.7.7.jar
  • stringtemplate-3.2.1.jar
  • antlr.jar
  • antlr-runtime-3.0.1.jar

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:

<target name="generate.parser" depends="init" description="Generates the lexer, parser and tree-walker from the grammar files.">
    <echo>Generating the lexer and parser...</echo>
    <java classname="org.antlr.Tool" fork="true" failonerror="true">
        <arg value="-fo" />
        <arg value="${main.src.dir}/${parser.package}" />
        <arg value="${parser.grammar.file}" />
        <classpath refid="classpath" />
    </java>
    <!-- snip -->
</target>

and the classpath refid looks like:

<path id="classpath">
    <!-- snip -->
    <fileset dir="lib">
        <include name="*.jar" />
    </fileset>
</path>

and the lib/ directory contains the ANTLR v3 JAR (no other ANTLR JARs! (sorry for hammering on that :)))

眼睛会笑 2024-10-28 01:17:38

您可能已经安装了 AntlrEclipse 插件。这确实使用了 Antlr 2.7.something。不过,有一个可与 v3 配合使用的插件,Antlr IDE。这个插件稍微先进一点,因为它允许您配置 Antlr 版本(3+),并且可以显示铁路视图并具有解释器。

注意如果您使用 Eclipse Juno,则需要从 Indigo 存储库安装 DLTK,因为 4.0.0 对于 antlr 插件来说是新的

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.

Note If you are using Eclipse Juno, you will need to install the DLTK from the Indigo repositorties, as 4.0.0 is to new for the antlr plugin

2024-10-28 01:17:38

我也有这个问题。

在我尝试运行 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文