0
votes

I have the following rules in a grammar:

CCExpression
    : LiteralExpression
    | CCParenthesizedExpression
    | CCSimpleNameExpression
    | CCCastExpression
    | CCOperatorExpression
    | CCConditionalExpression
    ;

CCOperatorExpression
    : CCUnaryOperator CCExpression
    | CCExpression CCBinaryOperator CCExpression
    ;

and I am getting the following error:

The following sets of rules are mutually left-recursive [CCExpression, CCOperatorExpression]


I tried to fold the CCOperatorExpression rule into the CCExpression rule:

CCExpression
    : CCExpression CCBinaryOperator CCExpression
    | CCUnaryOperator CCExpression
    | '(' CCExpression ')'
    | LiteralExpression
    | CCSimpleNameExpression
    | CCCastExpression
    | CCConditionalExpression
    ;

but that didn't seem to help. I still get:

The following sets of rules are mutually left-recursive [CCExpression]

How can I fix this?

1

1 Answers

0
votes

That is because lexer rules can’t be left recursive, only parser rules can.

See: Practical difference between parser rules and lexer rules in ANTLR?