Hamzawey
Hamzawey

Reputation: 416

The following set of rules are mutually left-recursive

I'm working with ANTLR4 i get the following error:

The following sets of rules are mutually left-recursive [primary_expression, primary_no_array_creation_expression].

Below is the snippet from the grammar causing that error:

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
    ;

Upvotes: 1

Views: 849

Answers (2)

Hamzawey
Hamzawey

Reputation: 416

I came across the same exact problem and it is discussed here in more details: https://theantlrguy.atlassian.net/wiki/display/ANTLR3/2.+Example

Upvotes: 1

GeorgDangl
GeorgDangl

Reputation: 2192

In your sample grammar, primary_expression can be primary_no_array_creation_expression.

Then, primary_no_array_creation_expression can be primary_expression ++.

Therefore, primary_no_array_creation_expression can also be primary_no_array_creation_expression ++.

This is allowed when there are "one way" dependencies to other rules, for example

term   : Digit
       | term Operator term
       ;

product : term Operator term ;

would be valid, because even though a term is self-referencing (left-recursive), it is only so in its own definition.

The following would be invalid:

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

product : term Operator term ;

Here, a product references a term and vice versa, therefore creating a mutually left recursive pattern.

You should break your grammar probably up into different rules.

Upvotes: 1

Related Questions