What is minimal sample Gradle project for ANTLR4 (with antlr plugin)?

后端 未结 7 1435
陌清茗
陌清茗 2021-02-14 03:47

I have created new Gradle project, added

apply plugin: \'antlr\'

and

dependencies {
    antlr \"org.antlr:antlr4:4.5.3\"
         


        
7条回答
  •  星月不相逢
    2021-02-14 04:45

    What helped me is two things:

    • Add header:@header{ package com.example.something.antlrparser; } to the top of the grammar file.
    • Place the grammar file in corresponding folder, i.e. src/main/antlr/com/example/something/antlrparser/grammar.g4

    Now when I run the generateGrammarSource gradle task, .java files are generated in /build/generated-src/antlr/main/com/example/something/antlrparser/*.java and they are automatically picked up by IntelliJ as well as compilable by gradle.

    The build.gradle file is just:

    group 'com.example.something'
    version '1.0-SNAPSHOT'
    
    apply plugin: 'java'
    apply plugin: 'antlr'
    apply plugin: 'idea'
    
    sourceCompatibility = 1.8
    
    repositories {
        mavenCentral()
    }
    
    dependencies {
        testCompile group: 'junit', name: 'junit', version: '4.12'
        antlr "org.antlr:antlr4:4.5" // use ANTLR version 4
    }
    

提交回复
热议问题