Antlr4 导入组合语法失败

phr*_*eed 0 import antlr4

我目前正在...

error(56): AqlCommentTest.g4:12:4: reference to undefined rule: htmlCommentDeclaration
error(56): AqlCommentTest.g4:13:4: reference to undefined rule: mdCommentDeclaration
Run Code Online (Sandbox Code Playgroud)

词法分析器语法的导入似乎正在加载。以下文件存在问题。

AqlCommentTest.g4

grammar AqlCommentTest;
import AqlLexerRules;
import AqlComment;

program: commentDeclaration+;

commentDeclaration:
    htmlCommentDeclaration     #Comment_HTML
  | mdCommentDeclaration       #Comment_MD
;
Run Code Online (Sandbox Code Playgroud)

AqlComment.g4

grammar AqlComment;
import AqlLexerRules;

htmlCommentDeclaration: 'html' '{' '(*' STRING '*)' '}';

mdCommentDeclaration: 'md' '{' '(*' STRING '*)' '}';
Run Code Online (Sandbox Code Playgroud)

AqlLexerRules.g4

lexer grammar AqlLexerRules;
STRING :  '"' [a-z]? '"' ;
Run Code Online (Sandbox Code Playgroud)

可以通过删除“import AqlLexerRules;”来停止错误 来自“AqlCommentTest.g4”文件。

为什么这可以“解决”问题?

如何检查是否实际应用了 antlr4 导入语句以及如何应用?

Ber*_*rdK 5

如果导入词法分析器规则排在最后:

import AqlComment;
import AqlLexerRules;
Run Code Online (Sandbox Code Playgroud)

错误更改为:

error(54): AqlCommentTest.g4:4:0: repeated grammar prequel spec (options, tokens, or import); please merge
Run Code Online (Sandbox Code Playgroud)

因此,问题是:导入是否存在限制?

Definitive ANTLR 4 Reference 15.2 Grammar Structure 或doc 中,您可以找到:

选项、导入和令牌规范中最多可以有一个。

如果我将导入更改为:

import AqlComment, AqlLexerRules;
Run Code Online (Sandbox Code Playgroud)

它编译。