【问题标题】:ANTLR failure due to left recursion. How to solve?由于左递归,ANTLR 失败。怎么解决?
【发布时间】:2014-11-26 17:25:16
【问题描述】:

这是一个简单的 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 定义。老实说,我一开始在我的代码中看不到任何左递归。

谁能解释一下是怎么回事?

【问题讨论】:

  • 发布确切的错误信息总是好的。

标签: recursion antlr ll left-recursion


【解决方案1】:

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

sql_query
    : select_statement from_statement where_statement?
    ;

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多