ANTLR语法相互左递归 [英] ANTLR Grammar Mutually Left Recursive

查看:87
本文介绍了ANTLR语法相互左递归的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道这个问题已经问过很多次了.我正在尝试使用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]"

请告诉我如何解决此问题.如何删除我的反语法中的相互左递归.

Please show me how this is fixed. How to remove the mutual left recursion in my antlr grammar.

推荐答案

完全摆脱谓词"的递归出现.仅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".

观察PREDICATEVARCHARS如何仍然是词法规则(与语法规则相对),因为它们描述了词素的形成方式.因此,它遵循全部大写的命名约定(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语法相互左递归的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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