简体   繁体   English

Antlr - 表达式解析器和评估器

[英]Antlr - Expression parser and evaluator

I am required to write a boolean expression parser/evaluator.我需要编写一个布尔表达式解析器/评估器。 The expressions would be of the form and would be enclosed in parenthesis :表达式将采用以下形式并括在括号中:

exp1 : (A = B)
exp2 : ((A = B) AND (C = D))
exp3 : ((A = B) AND ((C = D) OR (E = F)))
exp4: (((A = B) AND (C = D)) OR (E = F))

and it goes on.它继续。 The rule may contain 'n' number of expressions with proper grouping 2 in each group.该规则可能包含“n”个表达式,每组中具有适当的分组 2。 My grammar file looks like this:我的语法文件如下所示:

/*
Expression grammar
 */

grammar Exparser;

options

{
    language = Java;
}
cond    :   
tc EOF;

tc:
      exp
  | binary_exp
| leftparen* exp ( binaryop leftparen* exp rightparen+)*
| leftparen* exp ( binaryop leftparen* exp rightparen*)*

;

binary_exp:
 '(' exp BINARYOP exp ')'
;

binaryop:
            BINARYOP
        ;
leftparen:
             LEFTPARN
         ;

rightparen:
              RIGHTPARN
          ;

exp:
LEFTPARN VARIABLE COMPOP VARIABLE RIGHTPARN

;

variable:
            VARIABLE;


BINARYOP: AND | OR;
COMPOP: EQUAL | LT | GT | LTE | GTE | NE;
VARIABLE: (CHAR)+;
LEFTPARN: '(';
RIGHTPARN: ')';
EQUAL: '=' | 'EQ';
LT: '<' | 'LT';

GT:'>' | 'GT';
LTE: '<=';
GTE: '>=';
NE  :   '!=' | 'NE';
AND: 'AND' | '&' | 'and';

OR: 'OR' | 'or';

CHAR  :   'a'..'z'|'A'..'Z'|'_' |'0'..'9'|'-' | '.'
   ;

this grammar works fine, but I am not able to achieve the depth in the AST.这个语法工作正常,但我无法达到 AST 的深度。 for example exp3 is parsed as three exp and not as one exp and one binary_exp .例如 exp3 被解析为三个exp而不是一个exp和一个binary_exp Also how do I evaluate a boolean expression using my parser?另外,如何使用解析器评估布尔表达式? How does my grammar enforce balancing of parenthesis?我的语法如何强制平衡括号? Though Nested Boolean Expression Parser using ANTLR give some idea for evaluating an expression, I am not able to apply in my case尽管使用 ANTLR 的嵌套布尔表达式解析器提供了一些评估表达式的想法,但我无法在我的情况下应用

The parser generated from the following grammar parses all your example input:从以下语法生成的解析器解析所有示例输入:

grammar Exparser;

parse
 : expr EOF
 ;

expr
 : expr binop expr
 | VARIABLE
 | '(' expr ')'
 ;

binop
 : AND | OR | EQUAL | LT | GT | LTE | GTE | NE
 ;

EQUAL     : '=' | 'EQ';
LT        : '<' | 'LT';
GT        : '>' | 'GT';
LTE       : '<=';
GTE       : '>=';
NE        : '!=' | 'NE';
AND       : 'AND' | '&' | 'and';
OR        : 'OR' | 'or';
VARIABLE  : [a-zA-Z0-9_.-]+;
SPACE     : [ \t\r\n] -> skip;

Evaluating these expressions should be the same as the Q&A you linked to in your question .评估这些表达式应该与您在问题中链接的问答相同。

Caveat: I don't know antlr.警告:我不知道 antlr。 However, I think you need to make your grammar more explicit.但是,我认为您需要使您的语法更加明确。 You are overloading exp too much.你让exp过载太多了。 Try something like this pseudocode:试试这样的伪代码:

tc <- binary_exp ;
binary_exp <- comparison_exp
            | LEFTPARN binary_exp RIGHTPARN
            | LEFTPARN binary_exp BINARYOP binary_exp RIGHTPARN ;
comparison_exp <- LEFTPARN VARIABLE COMPOP VARIABLE RIGHTPARN ;
  • Every tc is a binary expression binary_exp .每个tc都是一个二进制表达式binary_exp
  • A binary expression is one of:二进制表达式是以下之一:
    • a comparison expression comparison_exp ,一个比较表达式comparison_exp
    • a binary expression surrounded by parentheses ( LEFTPARN and RIGHTPARN ),由括号( LEFTPARNRIGHTPARN )包围的二进制表达式,
    • or a sequence of left-parenthesis LEFTPARN , a binary_exp , a binary operator BINARYOP , a binary_exp , and a right-parenthesis RIGHTPARN .或由左括号LEFTPARNbinary_exp 、二元运算符BINARYOPbinary_exp和右括号RIGHTPARN
  • a comparison expression is a sequence of left-parenthesis LEFTPARN , a variable VARIABLE , a comparison operator COMPOP , a VARIABLE , and a right-parenthesis RIGHTPARN .比较表达式是由左括号LEFTPARN 、变量VARIABLE 、比较运算符COMPOPVARIABLE和右括号RIGHTPARN

This grammar would allow binary expressions to be nested inside extra parentheses or nested inside one another, but comparison expressions cannot have other expressions nested inside them.这种语法允许二进制表达式嵌套在额外的括号内或嵌套在另一个括号内,但比较表达式不能嵌套其他表达式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM