0

这是 SQL 选择语句的简单语法

grammar SQL;
@rulecatch {
    //We want to stop parsing when SyntaxException is encountered
    //So we re-throw the exception
    catch (SyntaxException e) {
        throw e;
    }
}

eval 
    :  sql_query
    ;

sql_query
    : select_statement from_statement
    | select_statement from_statement where_statement
    ;

select_statement
    : 'select' attribute_list
    ;

from_statement
    : 'from' table_name
    ;

where_statement
    : 'where' attribute_name operator constant
    ;

attribute_list
    : '*'
    | attribute_name
    | attribute_name ',' attribute_list?
    ;

table_name
    : string_literal
    ;

attribute_name
    : string_literal
    ;

operator
    : '=' | '!=' | '>' | '>=' | '<' | '<=' 
    ;

constant
    : INTEGER
    | '"' string_literal '"'
    ;

fragment DIGIT: '0'..'9';
INTEGER: DIGIT+ ; 
fragment LETTER: 'a'..'z'|'A'..'Z';
string_literal: LETTER | LETTER string_literal;
WS : (' ' | '\t' | '\n' | '\r' | '\f')+  {$channel=HIDDEN;};

该工具对sql_query定义和定义唠叨不休attribute_list。老实说,我在我的代码中没有看到任何左递归。

谁能解释发生了什么?

4

1 回答 1

1

不,ANTLR 没有说你的语法是左递归的。它抱怨由于递归规则调用,某些规则具有非 LL(*) 决策。重写以下规则如下,你会没事的:

sql_query
    : select_statement from_statement where_statement?
    ;

attribute_list
    : '*'
    | attribute_name (',' attribute_list?)?
    ;
于 2014-11-26T18:56:36.623 回答