Zev Spitz
Zev Spitz

Reputation: 15375

Mutually left-recursive rules

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?

Upvotes: 0

Views: 62

Answers (1)

Bart Kiers
Bart Kiers

Reputation: 170298

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?

Upvotes: 0

Related Questions