【问题标题】:The following set of rules are mutually left-recursive以下规则集是相互左递归的
【发布时间】:2015-03-28 00:38:34
【问题描述】:

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

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

以下是导致该错误的语法中的 sn-p:

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
    ;

【问题讨论】:

    标签: antlr4


    【解决方案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,反之亦然,因此创建了相互左递归模式。

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

    【讨论】:

      【解决方案2】:

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2019-02-26
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多