左递归ANTLR语法

时间:2013-02-22 07:26:55

标签: antlr antlr3 antlrworks left-recursion

我写了一个语法,但得到了一个左递归错误。

grammar Lang;

options
{
    output  = AST;
    language    = C;
    ASTLabelType= pANTLR3_BASE_TREE;
    backtrack   = true;
}

start   : primary_expression+
    ;

primary_expression
                : '{' expression '}'
                | expression ',' expression
                ;

expression
                : logical_or_expression
                | logical_or_expression '?' expression ':' expression
                | logical_or_expression '?' ':' expression
                | logical_or_expression '?' expression
                ;

logical_or_expression
                : logical_and_expression
                | logical_and_expression '|' logical_or_expression
                ;

logical_and_expression
                : primary_expression
                | primary_expression '&' logical_and_expression
                ;

我收到以下错误:

  

[12:41:35]错误(210):以下几组规则是相互左递归的[primary_expression,logical_and_expression,logical_or_expression,expression]
  [12:41:35]中止,因为以下规则是相互左递归的:       [[Lang.primary_expression,index = 2,line = 19],[Lang.logical_and_exression,index = 5,line = 36],[Lang.logical_or_expression,index = 4,line = 31],[Lang.expression,index = 3,线= 24]]


更正语法

grammar Lang;
options 
{
    // Note that in the C implementation, all implementations of trees and
    // adaptors pass around pANTLR3_BASE_TREE, which contains a super pointer
    // to your own implementation of a tree node and tree and so on. Hence
    // the node type is ALWAYS pANTLR3_BASE_TREE and there is no need to define
    // the type (the definition is silently ignored if you leave it there)
    //
    //output    = AST;
    language    = C;
    //ASTLabelType= pANTLR3_BASE_TREE;
    backtrack   = true;
}

start   : primary_expression+
    ;

primary_expression
                : '{' expression '}'
                | expression ',' expression
                ;

expression
                : logical_or_expression
                | logical_or_expression '?' expression ':' expression
                | logical_or_expression '?' ':' expression
                | logical_or_expression '?' expression
                ;

logical_or_expression
                : logical_and_expression
                | logical_and_expression '|' logical_or_expression
                ;

logical_and_expression
                : STRING
                | STRING '&' logical_and_expression
                ;

/* We're going to ignore all white space characters */
WS  
    :   (' ' | '\t' | '\r'| '\n') {$channel=HIDDEN;}
    ;

STRING
    :   ('a'..'z' | 'A'..'Z' | '0'..'9' | '_')+
    ;

1 个答案:

答案 0 :(得分:2)

Antlr 4可以处理直接左递归,但无法处理间接左递归。在第一种情况下," primary_expression"和" logical_and_expression:primary_expression ..."形成间接左递归。但是现在antlr4无法生成' c'码。

grammar test3;

options
{
    language    = Java;
}

start   : 
    expression+
    ;

expression : 
    primary_expression
    | expression '&' expression
    | expression '|' expression
    | expression '?' expression ':' expression
    | expression '?' expression 
    | expression '?' ':' expression 
    | expression ',' expression
    | '{' expression '}'
    ;

primary_expression : // variable or constant definition, such as
    VARIABLE 
    | NUMBER
    ;
VARIABLE :
    ('A'..'Z')+
    ;

NUMBER :
    ('0'..'9') +
    ;