问题描述:
由于编译器中将使用到not、 not in、not exists、not like、is not null等包含英文not的单词,而ANTLR在进行词法分析时,无法根据空格进行解析。而且原来用("not"^) search_condition对整个WHERE条件的逻辑非构造语法树,也与其他地方的not产生冲突,导致无法识别英文的not。
处理过程:
1、对整个WHERE条件的英文逻辑非(not),增加一个SEARCH_NOT_CONDITION TOKEN,用来分辨此为整个WHERE条件,而中文的非仍热为整个WHERE条件的根节点,语法定义如下:
1 search_condition
2 : bool_exp
3 | ( "not"
4 {#search_condition = #([SEARCH_NOT_CONDITION, "search_not_condition"], search_condition);}
5 | "非"^
6 ) search_condition
7 ;
2、增加LOGICAL_NOT_LIKE、LOGICAL_NULL、LOGICAL_NOT_NULL、LOGICAL_IN、LOGICAL_NOT_IN TOKEN,用来针对这些英文的关键字进行分析,语法定义如下:
1 equation
2 : expression (
3
4 //关系运算符(+ - * /) 表达式
5 ("=" | compare_op) expression
6 {#equation=#([COMPARE_OP, "comp_op"], #equation);}
7
8 //关系运算符NOT LIKE 表达式
9 | ("not" "like") expression
10 {#equation=#([LOGICAL_NOT_LIKE, "logic_not_like"], #equation);}
11
12 //关系运算符IS NULL/IS NOT NULL
13 | ( "is" "null"
14 {#equation = #([LOGICAL_NULL, "logic_null"], #equation);}
15 | "is" "not" "null"
16 {#equation = #([LOGICAL_NOT_NULL, "logic_not_null"], #equation);}
17 | "为空"^ | "非空"^
18 )
19
20 //关系运算符BETWEEN AND
21 | ("between"^ | "范围"^) expression ("and"!)? expression
22
23 //关系运算符IN/NOT IN
24 | ( "in"
25 {#equation = #([LOGICAL_IN, "logic_in"], #equation);}
26 | "not" "in"
27 {#equation = #([LOGICAL_NOT_IN, "logic_not_in"], #equation);}
28 | "在于"^ | "不在于"^
29 ) exp_set
30 )
31 ;
语法树遍历代码如下:
1 equation returns [EquationModel model]
2 {
3 ExpressionModel e1, e2, e3;
4 EquationModel equation;
5 model=new EquationModel();
6 String nullStr = "";
7 }
8 : #(COMPARE_OP e1=expression op:compare_op e2=expression)
9 {model.addExpression(e1); model.addOperator(op.getText()); model.addExpression(e2);}
10
11 | #(LOGICAL_NOT_LIKE e1=expression "not" "like" e2=expression)
12 {model.addExpression(e1); model.addOperator("not like"); model.addExpression(e2);}
13
14 | #(LOGICAL_NULL e1=expression "is" "null")
15 {model.addExpression(e1); model.addOperator("is null");}
16 | #(n:"为空" e1=expression)
17 {model.addExpression(e1); model.addOperator(n.getText());}
18 | #(LOGICAL_NOT_NULL e1=expression "is" "not" "null")
19 {model.addExpression(e1); model.addOperator("is not null");}
20 | #(nn:"非空" e1=expression)
21 {model.addExpression(e1); model.addOperator(nn.getText());}
22 | #("between" e1=expression e2=expression e3=expression)
23 {model.addExpression(e1); model.addOperator("between");
24 model.addExpression(e2); model.addExpression(e3);}
25 | #(btw:"范围" e1=expression e2=expression e3=expression)
26 {model.addExpression(e1); model.addOperator(btw.getText());
27 model.addExpression(e2); model.addExpression(e3);
28 }
29
30 | #(LOGICAL_IN e1=expression "in" e2=exp_set)
31 {model.addExpression(e1); model.addOperator("in"); model.addExpression(e2);}
32 | #(ct1:"在于" e1=expression e2=exp_set)
33 {model.addExpression(e1); model.addOperator(ct1.getText()); model.addExpression(e2);}
34 | #(LOGICAL_NOT_IN e1=expression "not" "in" e2=exp_set)
35 {model.addExpression(e1); model.addOperator("not in"); model.addExpression(e2);}
36 | #(ct2:"不在于" e1=expression e2=exp_set)
37 {model.addExpression(e1); model.addOperator(ct2.getText()); model.addExpression(e2);}
38 ;
LORD
jiandeh@sina.com
2007-05-22
posted on 2007-05-22 16:44
LORD BLOG 阅读(228)
评论(0) 编辑 收藏 所属分类:
工作日志