Is it possible to make Antlr4 generate lexer from base grammar lexer instead of gener Lexer?

帅比萌擦擦* 提交于 2019-12-25 03:26:45

问题


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?


回答1:


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

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

  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.



来源:https://stackoverflow.com/questions/25539575/is-it-possible-to-make-antlr4-generate-lexer-from-base-grammar-lexer-instead-of

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