日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

antlr4-maven-plugin简单学习

發(fā)布時間:2023/12/29 编程问答 35 豆豆
生活随笔 收集整理的這篇文章主要介紹了 antlr4-maven-plugin简单学习 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

1. 序言

  • antlr4-maven-plugin的官方介紹為:

    The ANTLR 4 plugin for Maven can generate parsers for any number of grammars in your project.
  • 博客《 mac上的Antlr4環(huán)境搭建》,有介紹如何通過antlr4-maven-plugin實現(xiàn).g4文件的編譯

  • 這里將介紹antlr4-maven-plugin在開源組件中的使用,以及額外的使用注意事項

2. antlr4-maven-plugin在開源組件中的使用

2.1 Presto中 antlr4-maven-plugin的配置

  • 以Presto為例,presto-parser模塊的pom.xml文件中,對antlr4-maven-plugin的配置非常簡單

    <build><plugins><plugin><groupId>org.antlr</groupId><artifactId>antlr4-maven-plugin</artifactId></plugin></plugins> </build>
  • 這是因為這里的配置,繼承了presto-root(項目的父模塊)對antlr4-maven-plugin的配置

    <dep.antlr.version>4.7.1</dep.antlr.version><!-- 指定了插件的版本、goal以及是否生成vistor代碼 --> <pluginManagement><plugins><plugin><groupId>org.antlr</groupId><artifactId>antlr4-maven-plugin</artifactId><version>${dep.antlr.version}</version> <executions><execution><goals><goal>antlr4</goal></goals></execution></executions><configuration><visitor>true</visitor></configuration></plugin> </pluginManagement>
  • 對標(biāo)在4.1中的配置,這樣的配置顯得十分精簡,例如,缺少sourceDirectory、outputDirectory、listener這三個關(guān)鍵配置

2.2 antlr4-maven-plugin的配置項

  • 點擊某個配置項,將進入plugin.xml中

  • 在plugin.xml中可以發(fā)現(xiàn)更多信息,里面有配置的name、type、required、description、默認值等

    <!-- 定義和描述 --> <parameter><name>sourceDirectory</name><type>java.io.File</type><required>false</required><editable>true</editable><description>The directory where the ANTLR grammar files ({@code *.g4}) are located.</description> </parameter><!-- 默認值 --> <sourceDirectory implementation="java.io.File" default-value="${basedir}/src/main/antlr4"/> <outputDirectory implementation="java.io.File" default-value="${project.build.directory}/generated-sources/antlr4"/>
  • 從sourceDirectory和outputDirectory的默認值可以看出,.g4文件的location以及編譯生成的Java代碼的路徑,都與4.1中的差異很大。例如,甚至將生成的Java代碼放在了target/generated-sources/antrl4目錄下,而非我們理解的/src/main/java目錄下

2.3 Presto中的.g4文件以及生成的Java代碼

  • presto-parser模塊,將SqlBase.g4文件放在src/main/antlr4/com/facebook/presto/sql/parser目錄下
  • 執(zhí)行mvn clean install -DskipTests完成本地編譯后,將在target/generated-sources/antlr4/com/facebook/presto/sql/parser目錄下生成相關(guān)的Java代碼
  • 同時,在src/main/antlr4下的多層目錄,還變成了Java代碼中的package信息
  • 這些在target/generated-sources/antlr4目錄下的Java代碼,最終將使用同一版本的antlr-runtime進行編譯和運行
  • 與普通的Java代碼一樣,這些Java代碼編譯生成的.class文件,也將放在target/classes/目錄下
  • 這也是為什么在本地運行Presto時,需要先進行一次全局的編譯,不然直接運行,將提示各種類找不到

3. antlr4-maven-plugin對JDK的版本要求

  • 將antlr版本定義為4.10.1(antlr4-maven-plugin和antlr-runtime的版本都是4.10.1),然后執(zhí)行mvn clean install命令,使用antlr4-maven-plugin編譯.g4文件,卻發(fā)現(xiàn)報錯信息如下:

    java.lang.UnsupportedClassVersionError: org/antlr/v4/Tool has been compiled by a more recent version of the Java Runtime (class file version 55.0), this version of the Java Runtime only recognizes class file versions up to 52.0at java.lang.ClassLoader.defineClass1 (Native Method)at java.lang.ClassLoader.defineClass (ClassLoader.java:763)at java.security.SecureClassLoader.defineClass (SecureClassLoader.java:142)at java.net.URLClassLoader.defineClass (URLClassLoader.java:468)at java.net.URLClassLoader.access$100 (URLClassLoader.java:74)
  • 與之前安裝使用高版本的antlr-4.x-complete.jar,遇到的問題一樣

  • 總結(jié): 使用antlr4-maven-plugin時,要注意不要超過Antlr對應(yīng)的JDK版本。

  • 其中,version與JDK的對應(yīng)關(guān)系,可以參考《Class has been compiled by a more recent version of the Java Environment》

總結(jié)

以上是生活随笔為你收集整理的antlr4-maven-plugin简单学习的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。