對於語法項目。文件夾結構應是這樣的:
ls -R grammar/
grammar/:
pom.xml src
grammar/src:
main test
grammar/src/main:
resources
grammar/src/main/resources:
org
grammar/src/main/resources/org:
boazglean
grammar/src/main/resources/org/boazglean:
dabar
grammar/src/main/resources/org/boazglean/dabar:
grammar
grammar/src/main/resources/org/boazglean/dabar/grammar:
DabarLexer.g4 DabarParser.g4
現在,當你建立這個項目,你就必須構建一個這樣的罐子:
jar -tf grammar/target/grammar-1.0-SNAPSHOT.jar
META-INF/
META-INF/MANIFEST.MF
org/
org/boazglean/
org/boazglean/dabar/
org/boazglean/dabar/grammar/
org/boazglean/dabar/grammar/DabarParser.g4
org/boazglean/dabar/grammar/DabarLexer.g4
META-INF/maven/
META-INF/maven/org.boazglean.dabar/
META-INF/maven/org.boazglean.dabar/grammar/
META-INF/maven/org.boazglean.dabar/grammar/pom.xml
META-INF/maven/org.boazglean.dabar/grammar/pom.properties
現在的解析器項目。它將使用org.boazglean.dabar.grammar中的語法並在org.boazglean.dabar.parser中生成解析器
對於解析器項目。文件夾結構應是這樣的:
ls -R parser/
parser/:
pom.xml src
parser/src:
test
parser/src/test:
java
parser/src/test/java:
org
parser/src/test/java/org:
boazglean
parser/src/test/java/org/boazglean:
dabar
parser/src/test/java/org/boazglean/dabar:
parser
parser/src/test/java/org/boazglean/dabar/parser:
DabarLexerTest.java
現在繁重的工作來在pom.xml 供以後使用:
<properties>
<antlr.grammar.dir>${project.build.directory}/grammar/</antlr.grammar.dir>
<antlr.parser.dir>${project.build.directory}/generated-sources/antlr/</antlr.parser.dir>
</properties>
首先,我們將需要提取的語法,從語法罐。這允許antlr反對它。
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<id>extract-grammar</id>
<phase>initialize</phase>
<goals>
<goal>unpack-dependencies</goal>
</goals>
<configuration>
<includeArtifactIds>grammar</includeArtifactIds>
<outputDirectory>${antlr.grammar.dir}</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
既然下載了語法,我們可以生成解析器。爲了做到這一點,我們需要指出antlr不在包的根目錄($ {antlr.grammar.dir}),而是位於包$ {antlr.grammar.dir}/org/boazglean/dabar/grammar的頂部/同樣,我們必須生成不在包$ {project.build.directory}/generated-sources/antlr /的根目錄下的java文件,而是在包$ {project.build。目錄} /生成來源/ ANTLR /組織/ boazglean /達巴爾/解析器。爲了使這與項目的groupId和artifactId保持一致,我們將使用build helper插件通過替換所有的'。'來生成一些新的屬性。在groupId中用'/'來形成路徑。
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<executions>
<execution>
<id>antlr.grammar.package.dir</id>
<goals>
<goal>regex-property</goal>
</goals>
<configuration>
<name>antlr.grammar.package.dir</name>
<regex>\.</regex>
<value>${antlr.grammar.dir}/${project.groupId}/grammar</value>
<replacement>/</replacement>
</configuration>
</execution>
<execution>
<id>antlr.parser.package.dir</id>
<goals>
<goal>regex-property</goal>
</goals>
<configuration>
<name>antlr.parser.package.dir</name>
<regex>\.</regex>
<value>${antlr.parser.dir}/${project.groupId}/${project.artifactId}</value>
<replacement>/</replacement>
</configuration>
</execution>
</executions>
</plugin>
現在我們有兩個新的屬性; antlr.grammar.package.dir,antlr.parser.package.dir給我們antlr所需的正確路徑。現在我們可以稱爲antlr
<plugin>
<groupId>org.antlr</groupId>
<artifactId>antlr4-maven-plugin</artifactId>
<executions>
<execution>
<id>generate-antlr-sources</id>
<goals>
<goal>antlr4</goal>
</goals>
<configuration>
<sourceDirectory>${antlr.grammar.package.dir}</sourceDirectory>
<listener>true</listener>
<visitor>true</visitor>
<outputDirectory>${antlr.parser.package.dir}</outputDirectory>
<arguments>
<argument>-package</argument>
<argument>${project.groupId}.${project.artifactId}</argument>
</arguments>
</configuration>
</execution>
</executions>
</plugin>
最後一件事,antlr生成的token文件非常有用。但在這一點上,他們不會進入你的parser.jar。所以我們將其添加爲資源。我們不希望增加這是坐在旁邊的標記文件的Java文件,所以我們將添加一個排除的資源:
<resource>
<directory>${antlr.parser.dir}</directory>
<includes>
<include>**/*.tokens</include>
</includes>
</resource>
現在可以構建,測試將能夠使用ANTLR生成的解析器和解析器jar將如下所示:
jar -tf parser/target/parser-1.0-SNAPSHOT.jar
META-INF/
META-INF/MANIFEST.MF
org/
org/boazglean/
org/boazglean/dabar/
org/boazglean/dabar/parser/
org/boazglean/dabar/parser/DabarLexer.tokens
org/boazglean/dabar/parser/DabarParser.tokens
org/boazglean/dabar/parser/DabarLexer.class
org/boazglean/dabar/parser/DabarParserListener.class
org/boazglean/dabar/parser/DabarParser$ProgramContext.class
org/boazglean/dabar/parser/DabarParser$PhraseContext.class
org/boazglean/dabar/parser/DabarParser$CallContext.class
org/boazglean/dabar/parser/DabarParser$PassContext.class
org/boazglean/dabar/parser/DabarParser$ReferenceContext.class
org/boazglean/dabar/parser/DabarParser$SentenceContext.class
org/boazglean/dabar/parser/DabarParser$CompoundContext.class
org/boazglean/dabar/parser/DabarParser$BlockContext.class
org/boazglean/dabar/parser/DabarParser.class
org/boazglean/dabar/parser/DabarParserBaseVisitor.class
org/boazglean/dabar/parser/DabarParserVisitor.class
org/boazglean/dabar/parser/DabarParserBaseListener.class
META-INF/maven/
META-INF/maven/org.boazglean.dabar/
META-INF/maven/org.boazglean.dabar/parser/
META-INF/maven/org.boazglean.dabar/parser/pom.xml
META-INF/maven/org.boazglean.dabar/parser/pom.properties
成功。