问题描述
我知道这个问题已经被问过很多次了.我正在尝试使用 ANTLR 构建语法.
I do know that this question has been asked a lot of times. I am trying to build a grammar using ANTLR.
Predicate : LOWERCASE | Predicate VarChars ;
VarChars : LOWERCASE | UPPERCASE;
fragment LOWERCASE : [a-z] ;
fragment UPPERCASE : [A-Z] ;
我收到以下错误:以下规则集相互左递归[谓词]"
I am getting the following error :"The following sets of rules are mutually left-recursive [Predicate]"
请告诉我这是如何解决的.如何删除我的 antlr 语法中的相互左递归.
Please show me how this is fixed. How to remove the mutual left recursion in my antlr grammar.
推荐答案
彻底摆脱Predicate"的递归出现.单独的 VarChars 就足以表示小写或大写.使用 + 后缀表示一个或多个实例":
Get rid of the recursive occurrence of "Predicate" altogether. VarChars alone is sufficient to mean lowercase or uppercase. Use the + suffix to indicate "one or more instances":
fragment LOWERCASE : [a-z];
fragment UPPERCASE : [A-Z];
VARCHARS : LOWERCASE | UPPERCASE;
PREDICATE : LOWERCASE VARCHARS+;
应用于您的示例,这将取消p"和PA"作为谓词的资格,并限制pA".
Applied to your examples, this would disqualify "p" and "PA" as predicates, and qualify "pA".
观察 PREDICATE
和 VARCHARS
如何仍然是词法规则(与句法规则相对),因为它们描述了词素是如何形成的.因此它遵循全大写命名约定(Antlr 不在乎,但它提高了可读性).
Observe how PREDICATE
and VARCHARS
are still lexer rules (as opposed to syntactical rules) as they describe how a lexeme is formed. Therefore it follows the all-uppercase naming convention (Antlr does not care, but it improves readability).
这篇关于ANTLR 语法互左递归的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!