以下规则集是相互左递归的

时间:2015-03-28 00:38:35

标签: antlr4

我正在使用ANTLR4,我收到以下错误:

  

以下规则集是相互左递归的[primary_expression,primary_no_array_creation_expression]。

以下是导致该错误的语法片段:

primary_expression
    :   primary_no_array_creation_expression
    |   array_creation_expression
    ;

primary_no_array_creation_expression
    :   literal
    |   simple_name
    |   parenthesized_expression
    |   member_access
    |   primary_expression '.' identifier type_argument_list?
    |   primary_expression '(' argument_list? ')'
    |   primary_no_array_creation_expression '[' argument_list ']'
    |   this_access
    |   base_access
    |   primary_expression '++'
    |   primary_expression '--'
    |   object_creation_expression
    |   delegate_creation_expression
    |   anonymous_object_creation_expression
    |   typeof_expression
    |   checked_expression
    |   unchecked_expression
    |   default_value_expression
    |   anonymous_method_expression
    |   primary_expression '->' identifier
    |   primary_no_array_creation_expression '[' expression ']'
    |   sizeof_expression
    ;

2 个答案:

答案 0 :(得分:1)

在您的示例语法中,primary_expression可以是primary_no_array_creation_expression

然后,primary_no_array_creation_expression可以是primary_expression ++

因此,primary_no_array_creation_expression也可以是primary_no_array_creation_expression ++

当有单向"时,这是允许的。依赖于其他规则,例如

term   : Digit
       | term Operator term
       ;

product : term Operator term ;

是有效的,因为即使一个术语是自引用(左递归),它只是在它自己的定义中。

以下内容无效:

term   : Digit
       | term Operator term
       | product Operator product
       ;

product : term Operator term ;

此处,product引用term,反之亦然,因此创建了相互左移的模式。

你应该把语法分解成不同的规则。

答案 1 :(得分:1)

我遇到了同样的问题,这里将详细讨论: https://theantlrguy.atlassian.net/wiki/display/ANTLR3/2.+Example

相关问题