简体   繁体   English

ANTLR4 中关于立即左递归的错误

[英]Error about Immediate left recursion in ANTLR4

I write follow grammar in a.g4 file:我在 a.g4 文件中写了以下语法:

expr
    :function=expr'('((arg',')*arg)?')'                             #callFun
    |object=expr'.'SYMBOL                                           #objCall
    |coll=expr'['arg']'                                             #collage
    |varId=SYMBOL                                                   #varValue
    |(expr)                                                         #brackets
    |expr ADD ADD                                                   #lastInc
    |expr SUB SUB                                                   #lastDec
    //2
    |ADD ADD <assoc=right> expr                                     #postInc
    |SUB SUB <assoc=right> expr                                     #postDec
    |NOT <assoc=right> expr                                         #not
    |SUB <assoc=right> expr                                         #negative
    |'new' expr                                                     #createObj
    //3
    |expr MUL expr                                                  #mul
    |expr DIV expr                                                  #div
    |expr MOD expr                                                  #mod
    //4
    |expr ADD  expr                                                 #add
    |expr SUB  expr                                                 #sub
    //5
    |expr LMOVE expr                                                #lMove
    |expr RMOVE expr                                                #rMove
    //6
    |expr LESS expr                                                 #less
    |expr LESS EQUAL expr                                           #lessEqual
    |expr GREATER expr                                              #greater
    |expr GREATER EQUAL expr                                        #greaterEqual
    //7
    |expr EQUAL EQUAL expr                                          #equal
    |expr NOT EQUAL expr                                            #notEqual
    //8
    |expr AND ADD expr                                              #and
    //9
    |expr OR OR expr                                                #or
    //10
    |expr '?' <assoc=right> exprA = expr':'exprB = expr             #trueAfalseB
    //11
    |expr EQUAL <assoc=right> expr                                  #putIn
    |expr ADD EQUAL <assoc=right> expr                              #addPutIn
    |expr SUB EQUAL <assoc=right> expr                              #subPutIn
    |expr MUL EQUAL <assoc=right> expr                              #mulPutIn
    |expr DIV EQUAL <assoc=right> expr                              #divPutIn
    |expr MOD EQUAL <assoc=right> expr                              #modPutIn
    |expr LMOVE EQUAL <assoc=right> expr                            #lMovePutIn
    |expr RMOVE EQUAL <assoc=right> expr                            #rMovePutIn
    //12
    |THROW <assoc=right> expr                                       #throw
    //13
    |expr','expr                                                    #comma
    ;

If I'm not mistaken, there's only immediate left recursion in this grammar.But there is a error message:"The following sets of rules are mutually left-recursive [expr]".why?如果我没记错的话,这个语法中只有立即左递归。但是有一个错误消息:“以下规则集是相互左递归的[expr]”。为什么?

As mentioned by Kaby76 in the comments, this alternative:正如 Kaby76 在评论中提到的那样,这个替代方案:

expr
 : ...
 | (expr)
 | ...
 ;

is the cause.是原因。 You probably meant to do this:您可能打算这样做:

expr
 : ...
 | ‘(‘ expr ‘)’
 | ...
 ;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM