0

在 Antlr4 中,据说支持直接左递归。我可以用下面给出的语法中的 [expr]-rule 来验证这一点。无论如何,ANTLR4 的语法分析为 [子查询] 规则抛出错误,该规则是该语法中的第二个直接递归规则:

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

再次,子查询规则是递归的。没有间接递归。

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];

不同子查询代码的合法语法输入行将是:

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

等等

递归具有 TERMINALVARIANT 给出退出子句,因此递归是/可以是有限的。为什么我会收到此错误?如何重写语法以避免它?

4

0 回答 0