是否可以使Antlr4从基本语法词法分析器而不是通用词法分析器生成词法分析器? [英] Is it possible to make Antlr4 generate lexer from base grammar lexer instead of gener Lexer?
问题描述
我有一个名为BasicTokens
的lexer grammar
,这是我语言的基本标记集,具有诸如null
,true
,false
等的标记.
现在,当我创建解析器语法时,说导入的BasicGrammar
是指BasicTokens
,而另一个名为InheritedGrammar
的语法是指导入BasicGrammar
.
当Antlr4为InheritedGrammar
生成解析器时,它包括BasicGrammar
中已经定义的所有规则.
有没有办法让Antlr仅描述在InheritedGrammar
中生成的规则而不在BasicGrammar
中描述,并且还继承BasicGrammarParser
而不是Parser
?
由于ANTLR 4实现导入的方式,因此无法实现.
如果语法x
导入语法y
,则该操作的行为如下:
- 加载语法
y
(及其所有规则). - 将语法
x
中的规则添加到规则集合中.如果发生任何名称冲突,请将y
中找到的规则替换为x
中找到的规则.
当您到达代码生成器时,规则层次结构已完全变平.
I have lexer grammar
called BasicTokens
which is set of basic tokens for my language, having tokens like null
, true
, false
etc.
Now when I create parser grammar say BasicGrammar
which imports refers BasicTokens
and another grammar called InheritedGrammar
which imports BasicGrammar
.
When Antlr4 generates the parser for InheritedGrammar
it included all the rules already defined in BasicGrammar
.
Is there a way to make Antlr describe only the rules generated in InheritedGrammar
and not in BasicGrammar
and also inherit BasicGrammarParser
instead of Parser
?
This is not possible due to the way ANTLR 4 implements imports.
If grammar x
imports grammar y
, the operation behaves as follows:
- Load grammar
y
(and all its rules). - Add the rules from grammar
x
to the collection of rules. If any name collision occurs, replace the rule found iny
with the one found inx
.
By the time you reach the code generator, the rules hierarchy is completely flattened.
这篇关于是否可以使Antlr4从基本语法词法分析器而不是通用词法分析器生成词法分析器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!