Antlr4 import of combined grammar failing

寵の児 提交于 2019-12-11 14:43:57

问题


I am presently getting...

error(56): AqlCommentTest.g4:12:4: reference to undefined rule: htmlCommentDeclaration
error(56): AqlCommentTest.g4:13:4: reference to undefined rule: mdCommentDeclaration

The import for the lexer grammar does seem to be loading. The following files present the problem.

AqlCommentTest.g4

grammar AqlCommentTest;
import AqlLexerRules;
import AqlComment;

program: commentDeclaration+;

commentDeclaration:
    htmlCommentDeclaration     #Comment_HTML
  | mdCommentDeclaration       #Comment_MD
;

AqlComment.g4

grammar AqlComment;
import AqlLexerRules;

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

mdCommentDeclaration: 'md' '{' '(*' STRING '*)' '}';

AqlLexerRules.g4

lexer grammar AqlLexerRules;
STRING :  '"' [a-z]? '"' ;

The errors can be stopped by removing the 'import AqlLexerRules;' from the 'AqlCommentTest.g4' file.

Why does this "fix" the problem?

How can I check to see if and how an antlr4 import statement is actually applied?


回答1:


If the import lexer rules comes last :

import AqlComment;
import AqlLexerRules;

the error changes to :

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

Hence the question : is there a constraint about import ?

In the Definitive ANTLR 4 Reference 15.2 Grammar Structure or doc you can find :

There can be at most one each of options, imports, and token specifications.

If I change the import to :

import AqlComment, AqlLexerRules;

it compiles.



来源:https://stackoverflow.com/questions/46479217/antlr4-import-of-combined-grammar-failing

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!