带有 antlr3 的简单条件表达式解析器 [英] simple criteria expression parser with antlr3

查看:43
本文介绍了带有 antlr3 的简单条件表达式解析器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用 antlr3 创建一个简单的标准表达式解析器

I want to create a simple criteria expression parser with antlr3

更新:单独的 AND OR 表达式规则以支持 AND/OR 不同的层次结构,但遇到了另一个问题:如果表达式类似于:a = 1 and b = 2 and c = 3根据当前的实现,树应该如下:

Updated: separate AND OR expression rules to support AND/OR different hierarchy, but got another problems: if the expression is something like: a = 1 and b = 2 and c = 3 The tree should be as following according to current implement:

       =      =
 (a = 1)(b = 2)(c = 3)
But I want to generate it as follows:
          =       =
    (a = 1)(b = 2)
               (c = 3)
First "and" should be higher priority than another, because I want to parse all the expression as left exp and right exp.

我想我需要在subcond"中重新编写规则使 a = 1 and b = 2 and c = 3 -> (a = 1 and b = 2) and c = 3

I think I need to re-write the rule in the "subcond" To make a = 1 and b = 2 and c = 3 -> (a = 1 and b = 2) and c = 3

但是尝试了很多次都没有运气.有没有人知道如何实现它?谢谢.

but tried many times with no luck. Has anybody got an idea how to achieve it? Thanks.

我的目标是解析某种 SQL where 子句样式的句子,并构建一个 AST 来遍历.

My goal is to parse some kind of SQL where clause style sentence, and build a AST to walk through.

例如:

    a = 1 and (b = 2 or c = 3)            //This one can parse correctly.
    a = 1 and ((b = 2 or c = 3) or d = 4) //This one cannot parse correctly, missing last d = 4 in the tree. 
                                          //Tree is not correct.

我当前的语法文件无法解析上述复杂条件.因为我是 antlr 的新手,不确定如何修改我的语法以更正确地实现上述方法.有人可以帮忙吗?!任何建议或意见表示赞赏.

My current grammar file cannot parse above complex condition. For I'm newbie for antlr, not sure how to modify my grammar to achieve above approach more correctly. Can someone help on this? !Any suggestions or comments are appreciate.

和我的语法如下(根据评论更新.警告问题已解决.):

and my grammar as follows (Updated according to the comments. Warning issue resolved.):

grammar CriteriaExpression;

options {
  output       = AST;
  ASTLabelType = CommonTree;
  language     = Java;
}

tokens {
  AND    = 'and';
  OR     = 'or';
  LPAREN = '(';
  RPAREN = ')';
}

@lexer::header {
package com.antlr;
}

@parser::header {
package com.antlr;
}

eval
:
expression
;

表达式: andExp (OR^ andExp)*;

expression : andExp (OR^ andExp)* ;

andExp: subcond (AND^ subcond)*;

andExp : subcond (AND^ subcond)* ;

子条件: LPAREN 表达式 RPAREN|原子;

subcond : LPAREN expression RPAREN |atom ;

atom
  :
  EXPR OPERATOR EXPR
  ;

OPERATOR
  :
  '='| '<>'| '!='| '<='| '!>'| '<'| '>='| '!<'| '>'| 'like'
  ;

EXPR
  :
  ('a'..'z'| 'A'..'Z'| '0'..'9')+
  ;

 WILDCARD
  :
  '%'
  ;

WS
  :
  ('\t'| ' '| '\r'| '\n'| '\u000C')*
   {$channel = HIDDEN;}
  ;

((a=1))

a = 1 and ((b = 2 or c = 3) or d = 4)

a = 1 and ((b = 2 or c = 3) or d = 4)

推荐答案

语法中的一个缺陷是规则

One flaw in your grammar is the rule

expression
  :
  LPAREN* subcond RPAREN* (( AND | OR )^ LPAREN* subcond RPAREN*)
  ;

由于您可以拥有任意数量的 LPAREN 或 RPAREN,因此无法保证它们匹配.我建议使用类似的东西

Since you can have any number of LPAREN or RPAREN, there is no guarantee they are matched. I suggest using somehting like

expression
  : subcond (( AND | OR ) subcond)?
  | subcond
  ;

subcond

subcond
  : atom (( AND | OR )^ atom)*
  | LPAREN expression RPAREN
  ;

理想情况下,您还应该为 ANDOR 表达式制定单独的规则,以便在解析树中拥有正确的优先级.

Ideally, you should also have separate rules for AND and OR expressions to have the correct precedence in your parse tree.

更新:在您更新的语法中,您再次使用 LPAREN*RPAREN* ,它们不会为您提供适当平衡的树.您需要使用递归对多个括号进行建模,例如 ((a = 1)) ,就像我在上面的示例中描述的那样.这会给一棵树像

Update: In your updated grammar, again you are using LPAREN* and RPAREN* which won't give you properly balanced trees. You need to model multiple parens like ((a = 1)) with recursion, like I described in my example above. This would give a tree like

((a = 1))
  ^---^--- ATOM
 ^-----^-- Subcond -> Expression
^-------^- Subcond -> Expression

所以树应该是这样的:

Expression "((a = 1))"
^
Subcond "(a = 1)"
^
Expression "(a = 1)"
^
Subcond "a = 1"
^
ATOM "a = 1"

这篇关于带有 antlr3 的简单条件表达式解析器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆