问题描述
我正在使用ANTLR构建MySQL语法验证器.我从MySQL源代码的sql_yacc.yy
开始,但是在转换以下语法时遇到一些困难.我尝试了很多次,但是没有用.谁能帮我吗?
I am constructing a MySQL grammar validator with ANTLR. I started with the sql_yacc.yy
from the MySQL source code, but I have some difficulties converting the following grammar. I tried many times, but it doesn't work. Can anyone help me?
expr
: expr or expr
| expr XOR expr
| expr and expr
| NOT_SYM expr
| bool_pri IS TRUE_SYM
| bool_pri IS not TRUE_SYM
| bool_pri IS FALSE_SYM
| bool_pri IS not FALSE_SYM
| bool_pri IS UNKNOWN_SYM
| bool_pri IS not UNKNOWN_SYM
| bool_pri
;
bool_pri
: bool_pri IS NULL_SYM
| bool_pri IS not NULL_SYM
| bool_pri EQUAL_SYM predicate
| bool_pri comp_op predicate
| bool_pri comp_op all_or_any '(' subselect ')'
| predicate
;
predicate
: bit_expr IN_SYM '(' subselect ')'
| bit_expr not IN_SYM '(' subselect ')'
| bit_expr IN_SYM '(' expr ')'
| bit_expr IN_SYM '(' expr ',' expr_list ')'
| bit_expr not IN_SYM '(' expr ')'
| bit_expr not IN_SYM '(' expr ',' expr_list ')'
| bit_expr BETWEEN_SYM bit_expr AND_SYM predicate
| bit_expr not BETWEEN_SYM bit_expr AND_SYM predicate
| bit_expr SOUNDS_SYM LIKE bit_expr
| bit_expr LIKE simple_expr opt_escape
| bit_expr not LIKE simple_expr opt_escape
| bit_expr REGEXP bit_expr
| bit_expr not REGEXP bit_expr
| bit_expr
;
bit_expr
: bit_expr '|' bit_expr
| bit_expr '&' bit_expr
| bit_expr SHIFT_LEFT bit_expr
| bit_expr SHIFT_RIGHT bit_expr
| bit_expr '+' bit_expr
| bit_expr '-' bit_expr
| bit_expr '+' INTERVAL_SYM expr interval
| bit_expr '-' INTERVAL_SYM expr interval
| bit_expr '*' bit_expr
| bit_expr '/' bit_expr
| bit_expr '%' bit_expr
| bit_expr DIV_SYM bit_expr
| bit_expr MOD_SYM bit_expr
| bit_expr '^' bit_expr
| simple_expr
;
推荐答案
ANTLR无法应对左递归,因此没有简单的方法可以将sql_yacc.yy
转换为ANTLR.您可能想看看ANTLR Wiki上的以下资源:
ANTLR cannot cope with left-recursion, so there's no trivial way to convert sql_yacc.yy
into the ANTLR equivalent. You might want to have a look at the following resources from the ANTLR Wiki:
- Left-Recursion Removal
- SQL grammar for MySQL dialect (ANTLR v3)
请注意,MySQL语法是不完整的,但可能会为您提供一个起点.
Note that the MySQL grammar is incomplete, but might give you a starting point.
这篇关于如何将MySQL yacc语法转换为antlr LL(1)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!