Gradle cannot find Antlr marker file

I created the file MyLexer.g4inside myproject/src/main/antlr/com/mypackage, for example:

lexer grammar MyLexer;

DIGIT : '0' .. '9' ;

...

WS  : [ \t\r\n]+ -> skip ;

and then try to write a parser MyParser.g4in the same directory:

grammar MyParser;

options
   { tokenVocab = MyLexer; }

SHORT_YEAR: DIGIT DIGIT;

Unfortunately, when I run the gradle task generateGrammarSource, the following error occurs:

error(160): com\mypackage\MyParser.g4:4:18: cannot find tokens file MYPROJECT\build\generated-src\antlr\main\MyLexer.tokens

those. The file is requested in the wrong place.

The actual file is created inside MYPROJECT\build\generated-src\antlr\main\com\mypackage\MyLexer.tokens

+4
source share
2 answers

When creating a parser in a package using:

@header {package org.acme.my.package;}

and tokenVocab declaration in your parser

options {tokenVocab = MyLanguage;}

The files MyLanguageLexer.g4 and MyLanguageParser.g4 should NOT be in the package directory. because of a mistake.

So that means /src/main/antlr/MyLanguageParser.g4, not /src/main/antlr/com/acme/my/package/MyLanguageParser.g4.

java build/generated-src/antlr, - build/classes/java/main. .tokens , antlr.


, IDE; compileClasspath, .

dependencies {
    testCompile fileTree('build/classes/java/main')
}
+1

, ANTLR src/main/antlr/, .

@header ANTLR. .

build.gradle ( ANTLR):

apply plugin: 'antlr'

dependencies {
    antlr "org.antlr:antlr4:4.7.1"
}

generateGrammarSource {
    arguments += ['-package', 'com.mypackage']
    outputDirectory = new File(buildDir.toString() + "/generated-src/antlr/main/com/mypackage/")
}
0

Source: https://habr.com/ru/post/1662959/


All Articles