AntlrGrammar.g4::: The following sets of rules are mutually left-recursive [subquery]

152 Views Asked by At

In Antlr4 direct left recursion is said to be supported. I can verify this with [expr]-rule in the grammar given below. Anyhow ANTLR4's grammar analysis throws the error for the [subquery]-rule, the second direct recursive rule in this grammar:

AntlrGrammar.g4::: The following sets of rules are mutually left-recursive [subquery]

Again the subquery-rule is left recursive. There is no indirect recursion.

grammar GrammarSubspace;

DIGIT: [0-9];
INT:  DIGIT ( DIGIT )*;
LETTER : [a-zA-Z_] ;
ID :  LETTER (LETTER|[0-9]|'_');

expr: expr '*' expr // precedence 4
| expr '+' expr // precedence 3
| INT // primary (precedence 2)
| ID // primary (precedence 1)
;

rowsetoperator: ( UNION (ALL)? | INTERSECT | MINUS );

subquery:
( 
    TERMINALVARIANT  
  | (subquery rowsetoperator subquery)+
  |  '('  subquery  ')'
) 
;

    TERMINALVARIANT: T E R M I N A L V A R I A N T; // Placeholder for another UNTERMINAL that resolves into all terminals without indirect recurrence to subquery

fragment A: [aA];    fragment B: [bB];    fragment C: [cC];    fragment D: [dD];
fragment E: [eE];    fragment F: [fF];    fragment G: [gG];    fragment H: [hH];
fragment I: [iI];    fragment J: [jJ];    fragment K: [kK];    fragment L: [lL];
fragment M: [mM];    fragment N: [nN];    fragment O: [oO];    fragment P: [pP];
fragment Q: [qQ];    fragment R: [rR];    fragment S: [sS];    fragment T: [tT];
fragment U: [uU];    fragment V: [vV];    fragment W: [wW];    fragment X: [xX];
fragment Y: [yY];    fragment Z: [zZ];

Legal grammar inputlines for diffrent subquery-code would be:

TERMINALVARIANT 
(TERMINALVARIANT) 
TERMINALVARIANT INTERSECT TERMINALVARIANT 
TERMINALVARIANT UNION ALL TERMINALVARIANT 
TERMINALVARIANT UNION TERMINALVARIANT 
TERMINALVARIANT MINUS TERMINALVARIANT 
(TERMINALVARIANT INTERSECT TERMINALVARIANT) 
(((((TERMINALVARIANT INTERSECT TERMINALVARIANT)))))

etc.

The recursion has TERMINALVARIANT gives an exit-clause, thus the recursion is/can be finite. Why do I get this error? How can I rewrite the grammar to avoid it?

0

There are 0 best solutions below