是否可以让 Antlr4 从基本语法词法分析器而不是一般词法分析器生成词法分析器? [英] Is it possible to make Antlr4 generate lexer from base grammar lexer instead of gener Lexer?

查看:48
本文介绍了是否可以让 Antlr4 从基本语法词法分析器而不是一般词法分析器生成词法分析器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有名为 BasicTokenslexer 语法,它是我的语言的一组基本标记,具有像 nulltrue<这样的标记/code>、false

I have lexer grammar called BasicTokens which is set of basic tokens for my language, having tokens like null, true, false etc.

现在,当我创建解析器语法时说 BasicGrammar 导入引用 BasicTokens 和另一个名为 InheritedGrammar 的语法导入 BasicGrammar.

Now when I create parser grammar say BasicGrammar which imports refers BasicTokens and another grammar called InheritedGrammar which imports BasicGrammar.

当 Antlr4 为 InheritedGrammar 生成解析器时,它包含了 BasicGrammar 中已经定义的所有规则.

When Antlr4 generates the parser for InheritedGrammar it included all the rules already defined in BasicGrammar.

有没有办法让 Antlr 只描述 InheritedGrammar 中生成的规则,而不是 BasicGrammar 中生成的规则,并且还继承 BasicGrammarParser 而不是 >解析器?

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?

推荐答案

由于 ANTLR 4 实现导入的方式,这是不可能的.

This is not possible due to the way ANTLR 4 implements imports.

如果语法x导入语法y,操作如下:

If grammar x imports grammar y, the operation behaves as follows:

  1. 加载语法 y(及其所有规则).
  2. 将语法x中的规则添加到规则集合中.如果发生任何名称冲突,请将 y 中的规则替换为 x 中的规则.
  1. Load grammar y (and all its rules).
  2. Add the rules from grammar x to the collection of rules. If any name collision occurs, replace the rule found in y with the one found in x.

当您到达代码生成器时,规则层次结构已完全扁平化.

By the time you reach the code generator, the rules hierarchy is completely flattened.

这篇关于是否可以让 Antlr4 从基本语法词法分析器而不是一般词法分析器生成词法分析器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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