0

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?

Zev Spitz
  • 13,950
  • 6
  • 64
  • 136

1 Answers1

0

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?

Bart Kiers
  • 166,582
  • 36
  • 299
  • 288
  • How do I define a rule as a lexer rule or a parser rule? – Zev Spitz Mar 11 '18 at 13:11
  • I've been trying (and failing) for some hours to understand the problem and solution. Could you elaborate? – Zev Spitz Mar 11 '18 at 14:07
  • Apparently you found an existing (E)BNF grammar for a language and are trying to port it to an ANTLR grammar (without any knowledge of ANTLR or parsers/parser-generators). If you don't understand the difference between a lexer and parser, you should stop what you're doing and learn some fundamentals first. Google on parser/lexer/tokeniser or something like that. Also checkout: https://stackoverflow.com/questions/2842809/lexers-vs-parsers – Bart Kiers Mar 11 '18 at 16:34
  • I see you've found that grammar on Microsoft's site (VB language specification). Although they mention ANTLR, in no way is the syntax of those grammar rules compatible with ANTLR (not without heavily modifying them). Without any knowledge of ANTLR, you won't be able to get something working. – Bart Kiers Mar 11 '18 at 16:40