是否可以在同一个项目中将maven项目中的类用于antlr语法?

时间:2014-02-24 09:07:37

标签: java maven antlr

我正在尝试使用antlr3创建一个解析器,我在同一个项目中使用java类时遇到了问题。

我已经构建了第二个项目,在我的语法的标题和lexer :::标题中添加了maven依赖项和导入,一切正常。

但我希望能够在一个项目中拥有所有东西。

我是否必须在.pom或我的语法标题中包含一些内容才能使其正常工作? 我尝试将导入放在标题中,但这不起作用。有什么想法吗?

目前我在我的pom中有这些:

<dependencies><dependency>
    <groupId>org.antlr</groupId>
    <artifactId>antlr</artifactId>
    <version>3.3</version>
    <type>jar</type>
    <scope>compile</scope>
</dependency></dependencies>

<build>

    <plugins>
        <plugin>
            <groupId>org.antlr</groupId>
            <artifactId>antlr3-maven-plugin</artifactId>
            <version>3.3</version>
            <executions>
                <execution>
                    <id>run antlr</id>
                    <phase>generate-sources</phase>
                    <goals>
                        <goal>antlr</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>

        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>build-helper-maven-plugin</artifactId>
            <executions>
                <execution>
                    <id>add-source</id>
                    <phase>generate-sources</phase>
                    <goals>
                        <goal>add-source</goal>
                    </goals>
                    <configuration>
                        <sources>
                            <source>${basedir}/target/generated-sources/antlr3</source>
                        </sources>
                    </configuration>
                </execution>
            </executions>
        </plugin>

    </plugins>

</build>

我还包括语法的第一行。注释中的行是我在使用包含我的自定义类的第二个项目时使用的行:

语法GCQLParser;

@header {
    package gr.uoa.di.madgik.search.parser.gcqlparser;
    import java.util.HashMap;
    import java.util.ArrayList;
    import java.util.List;
    import gr.uoa.di.madgik.search.parser.gcqlparser.tree.*;
    //import search.library.util.cql.query.tree.*;
}
@lexer::header {
    package gr.uoa.di.madgik.search.parser.gcqlparser;
    import java.util.HashMap;
    import java.util.ArrayList;
    import java.util.List;
    import gr.uoa.di.madgik.search.parser.gcqlparser.tree.*;
    //import search.library.util.cql.query.tree.*;
}

这是一个使用自定义类的规则:

booleanOp returns[GCQLBooleanNode node]
: AND  { $node = new GCQLAndNode(); }
| OR   { $node = new GCQLOrNode(); } 
| NOT  { $node = new GCQLNotNode(); }
| PROX { $node = new GCQLProxNode(); }
;

1 个答案:

答案 0 :(得分:0)

也许你错过了@header声明

示例grammar.g:

@header {
    package test.grammar;

    import test.other.*;
}

@header::lexer {
    package test.grammar;
}

@members {
     public void test() {
          new TestClass(); // test.other.TestClass
     }
}

确保您的pom.xml包含以下内容:

<build>
  <plugins>
    <plugin>
      <groupId>org.antlr</groupId>
      <artifactId>antlr3-maven-plugin</artifactId>
      <version>3.3</version>
      <executions>
        <execution>
          <goals>
            <goal>antlr</goal>
          </goals>
        </execution>
      </executions>
    </plugin>
  </plugins>
</build>

<dependencies>
  <dependency>
    <groupId>org.antlr</groupId>
    <artifactId>antlr-runtime</artifactId>
    <version>3.1</version>
  </dependency>
</dependencies>

编辑:如果您使用的是antlr3-maven-plugin,则至少需要3.1 antlr运行时