AntlrGrammar.g4 :::以下几组规则是相互左递归的[子查询]

时间:2017-07-23 12:54:26

标签: parsing recursion antlr4 context-free-grammar top-down

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

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给出一个exit子句,因此递归是/可以是有限的。 为什么我会收到此错误?如何重写语法以避免它?

0 个答案:

没有答案