Antlr4导入的组合语法失败 [英] Antlr4 import of combined grammar failing

查看:630
本文介绍了Antlr4导入的组合语法失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我现在正在......

 错误(56):AqlCommentTest.g4:12:4:未定义的引用规则:htmlCommentDeclaration 
错误(56):AqlCommentTest.g4:13:4:对未定义规则的引用:mdCommentDeclaration

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



AqlCommentTest.g4

 语法AqlCommentTest; 
import AqlLexerRules;
import AqlComment;

程序:commentDeclaration +;

commentDeclaration:
htmlCommentDeclaration #Comment_HTML
| mdCommentDeclaration #Comment_MD
;

AqlComment.g4

 语法AqlComment; 
import AqlLexerRules;

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

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

AqlLexerRules.g4

  lexer语法AqlLexerRules; 
STRING:'''[a-z]?''';

可以通过从'AqlCommentTest.g4'文件中删除'import AqlLexerRules;'来停止错误。



为什么这会修复这个问题?



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

解决方案

如果导入词法分析器规则是最后一次:

  import AqlComment; 
import AqlLexerRules;

错误更改为:

 错误(54):AqlCommentTest.g4:4:0:重复语法前缀规范(选项,标记或导入);请合并

因此问题:是否有关于导入的限制?



明确的ANTLR 4参考 15.2语法结构或 doc


每个选项,导入和令牌规范最多只能有一个。


如果我将导入更改为:

  import AqlComment,AqlLexerRules; 

它编译。<​​/ p>

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?

解决方案

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.

这篇关于Antlr4导入的组合语法失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆