为什么我的语法在用mvn compile
编译后会生成两个包行?
我的文件夹结构如下:
src/main/antlr/eu/niehus/parser/Lexer.g4
src/main/antlr/eu/niehus/parser/Parser.g4
我的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>
在我的Lexer.g4中是以下代码:
lexer grammar Lexer;
@lexer::header {
package eu.niehus.parser;
}
NUMBER: FLOAT | DIGIT;
LETTER: ('a'..'z') | ('A'..'Z');
FLOAT: DIGIT '.' DIGIT+;
DIGIT: [0-9]+ | MINUS [0-9]+;
我的Parser.g4的内容:
parser grammar Parser;
options {
language = Java;
tokenVocab = Lexer;
}
@header {
package eu.niehus.parser;
}
addition: NUMBER ('+' NUMBER)+;
现在如果我使用mvn编译生成它,我会得到以下源代码:
// 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生成两个包线?
我已经尝试在我的Parser / Lexer中没有@header语句,它会删除其中一个包行。但我不认为这是应该如何处理的
答案 0 :(得分:0)
其中一个打包语句是由于您的.g4文件(src / main / antlr / eu / niehus / parser /)的位置所致。
另一个是由于.g4文件标题中的package语句