Java 为什么mvn compile从我的语法文件生成两行程序包?
为什么我的语法在使用Java 为什么mvn compile从我的语法文件生成两行程序包?,java,maven,antlr,antlr4,Java,Maven,Antlr,Antlr4,为什么我的语法在使用mvncile编译后会产生两行包 我的文件夹结构如下所示: src/main/antlr/eu/niehus/parser/Lexer.g4 src/main/antlr/eu/niehus/parser/Parser.g4 My pom.xml: <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:
mvncile
编译后会产生两行包
我的文件夹结构如下所示:
src/main/antlr/eu/niehus/parser/Lexer.g4
src/main/antlr/eu/niehus/parser/Parser.g4
My pom.xml:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>eu.niehus</groupId>
<artifactId>parser</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>parser</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.7.0</version>
<configuration>
<source>1.9</source>
<target>1.9</target>
</configuration>
</plugin>
<plugin>
<groupId>org.antlr</groupId>
<artifactId>antlr4-maven-plugin</artifactId>
<version>4.7</version>
<configuration>
<visitor>true</visitor>
<treatWarningsAsErrors>true</treatWarningsAsErrors>
</configuration>
<executions>
<execution>
<goals>
<goal>antlr4</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.antlr</groupId>
<artifactId>antlr4-runtime</artifactId>
<version>4.7</version>
</dependency>
</dependencies>
</project>
my Parser.g4的内容:
parser grammar Parser;
options {
language = Java;
tokenVocab = Lexer;
}
@header {
package eu.niehus.parser;
}
addition: NUMBER ('+' NUMBER)+;
现在,如果我使用mvn compile生成它,我将获得以下源代码:
// Generated from eu\niehus\parser\Lexer.g4 by ANTLR 4.7
package eu.niehus.parser;
package eu.niehus.parser;
import org.antlr.v4.runtime.Lexer;
import org.antlr.v4.runtime.CharStream;
import org.antlr.v4.runtime.Token;
import org.antlr.v4.runtime.TokenStream;
import org.antlr.v4.runtime.*;
import org.antlr.v4.runtime.atn.*;
import org.antlr.v4.runtime.dfa.DFA;
import org.antlr.v4.runtime.misc.*;
它包含两条包装线。如何防止Maven生成两个包行
我已经尝试过在我的解析器/词法分析器中没有@header语句,它会删除一个包行。但是我不认为这是应该如何处理的包语句之一是由于.g4文件(src/main/antlr/eu/niehus/parser/)的位置
另一个原因是由于.g4文件头部分中的package语句您是否尝试对解析器部分使用
@parser::header{…
而不是@header{…
?
// Generated from eu\niehus\parser\Lexer.g4 by ANTLR 4.7
package eu.niehus.parser;
package eu.niehus.parser;
import org.antlr.v4.runtime.Lexer;
import org.antlr.v4.runtime.CharStream;
import org.antlr.v4.runtime.Token;
import org.antlr.v4.runtime.TokenStream;
import org.antlr.v4.runtime.*;
import org.antlr.v4.runtime.atn.*;
import org.antlr.v4.runtime.dfa.DFA;
import org.antlr.v4.runtime.misc.*;