Chapter 40. The ANTLR Plugin

The ANTLR plugin extends the Java plugin to add support for generating parsers using ANTLR.

The ANTLR plugin only supports ANTLR version 2.

40.1. Usage

To use the ANTLR plugin, include the following in your build script:

Example 40.1. Using the ANTLR plugin

build.gradle

apply plugin: 'antlr'

40.2. Tasks

The ANTLR plugin adds a number of tasks to your project, as shown below.

Table 40.1. ANTLR plugin - tasks

Task name Depends on Type Description
generateGrammarSource - AntlrTask Generates the source files for all production ANTLR grammars.
generateTestGrammarSource - AntlrTask Generates the source files for all test ANTLR grammars.
generateSourceSetGrammarSource - AntlrTask Generates the source files for all ANTLR grammars for the given source set.

The ANTLR plugin adds the following dependencies to tasks added by the Java plugin.

Table 40.2. ANTLR plugin - additional task dependencies

Task nameDepends on
compileJava generateGrammarSource
compileTestJava generateTestGrammarSource
compileSourceSetJava generateSourceSetGrammarSource

40.3. Project layout

Table 40.3. ANTLR plugin - project layout

Directory Meaning
src/main/antlr Production ANTLR grammar files.
src/test/antlr Test ANTLR grammar files.
src/sourceSet/antlr ANTLR grammar files for the given source set.

40.4. Dependency management

The ANTLR plugin adds an antlr dependency configuration. You use this to declare the ANTLR dependency that you wish to use.

Example 40.2. Declare ANTLR version

build.gradle

repositories {
    mavenCentral()
}

dependencies {
    antlr 'antlr:antlr:2.7.7'
}

40.5. Convention properties

The ANTLR plugin does not add any convention properties.

40.6. Source set properties

The ANTLR plugin adds the following properties to each source set in the project.

Table 40.4. ANTLR plugin - source set properties

Property name Type Default value Description
antlr SourceDirectorySet (read-only) Not null The ANTLR grammar files of this source set. Contains all .g files found in the ANTLR source directories, and excludes all other types of files.
antlr.srcDirs Set<File>. Can set using anything described in Section 16.5, “Specifying a set of input files”. [projectDir/src/name/antlr] The source directories containing the ANTLR grammar files of this source set.