Import Antlr4 Combined Grammar

I am currently getting ...

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

Import for lexer grammar seems to be loading. The following files are a 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 : '"' [az]? '"' ; 

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

Why does this β€œfix” the problem?

How to check if antlr4 import statement is really applied?

0
source share
1 answer

If lexer import rules last 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 

Therefore, the question arises: is there a restriction on imports?

In the Final ANTLR 4 Reference 15.2 Grammar structure or doc you can find:

In each version of the options, import and token there can be no more than one.

If I changed the import to:

 import AqlComment, AqlLexerRules; 

it compiles.

+1
source

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


All Articles