Why is my ANTLR grammar file for Java not compiling?

I was provided with ANTLR grammar for a subset of the Java compiler known as the static Java compiler. I am trying to expand the grammar to include more Java features, for example, I just added a grammar for For Loops.

Using the Eclipse and ANTLR plugin, I then "Compiled ANTLR Grammar". Instead of compiling, he made two errors in 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.

First error on line 1: “Unexpected token: grammar” Second error on line 5: “Unexpected char: @ '

Why doesn't it recognize this basic ANTLR syntax? My first thought was that I was missing something on the class path, but I went into the project properties and made sure that the following JAR libraries were included in the libraries:

  • ANTLR-2.7.7.jar
  • StringTemplate-3.2.1.jar
  • antlr.jar
  • ANTLR--3.0.1.jar

?

+3
3

antlr-2.7.7.jar : ANTLR v3+. :

  • ANTLR-2.7.7.jar
  • StringTemplate-3.2.1.jar
  • antlr.jar
  • ANTLR--3.0.1.jar

project/classpath ANTLR v3 JAR ( , : runtime, stringtemplate, works!).

!


IDE Ant build script. :

<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!) ( , :)))

+5

, AntlrEclipse. Antlr 2.7.-. , v3, Antlr IDE. , Antlr (3+) .

. Eclipse Juno, DLTK Indigo, 4.0.0 antlr

+2

.

- antlr jar Windows . .

+1

All Articles