ANTLR 接受特殊字符,例如 .标识符或表达式中的(点)和,(逗号)

问题描述 投票:0回答:1

我正在尝试使用 ANTLR 并使用 stackoverflow 帖子中 @Bart Kiers 的答案来执行输入数据(地图)的动态表达式我能够做到这一点。

我尝试在语法中添加

IN
STARTSWITH
ENDSWITH 
条件,并尝试从 java 端添加它的逻辑。对于
IN 
函数的情况,我尝试使用特殊字符(最好是
 , (comma)
)分割一组字符串。但是当我将它们添加到我的表达式中时,我收到如下错误

line 1:64 token recognition error at: ','

让我知道如何在表达式中允许使用特殊字符。

我使用的语法文件

grammar SimpleBoolean;

parse
 : expression EOF
 ;

expression
 : LPAREN expression RPAREN                       #parenExpression
 | NOT expression                                 #notExpression
 | left=expression op=comparator right=expression #comparatorExpression
 | left=expression op=binary right=expression     #binaryExpression
 | bool                                           #boolExpression
 | IDENTIFIER                                     #identifierExpression
 | DECIMAL                                        #decimalExpression
 ;

comparator
 : GT | GE | LT | LE | EQ | NE | IN | NOTIN | STARTSWITH | ENDSWITH | NULL | NOTNULL
 ;

binary
 : AND | OR
 ;

bool
 : TRUE | FALSE
 ;

AND        : 'AND' ;
OR         : 'OR' ;
NOT        : 'NOT';
TRUE       : 'TRUE' ;
FALSE      : 'FALSE' ;
GT         : '>' ;
GE         : '>=' ;
LT         : '<' ;
LE         : '<=' ;
EQ         : '=' ;
NE         : '!=' ;
IN         : 'IN' ;
NOTIN      : 'NOTIN' ;
STARTSWITH : 'STARTSWITH' ;
ENDSWITH   : 'ENDSWITH' ;
NULL       : 'NULL' ;
NOTNULL    : 'NOTNULL' ;
LPAREN     : '(' ;
RPAREN     : ')' ;
DECIMAL    : '-'? [0-9]+ ( '.' [0-9]+ )? ;
IDENTIFIER : [a-zA-Z_] [a-zA-Z_0-9]* ;
WS         : [ \r\t\u000C\n]+ -> skip;

修改了EvalVisitor.class

添加了以下几行

if (ctx.op.EQ() != null) {
          return this.visit(ctx.left).equals(this.visit(ctx.right));
        }
        else if (ctx.op.IN() != null) {
            String checkVal[] = this.visit(ctx.right).toString().split(",");
            boolean valuePresent = false;
            
            for(String value : checkVal) {
                if(value.equals(this.visit(ctx.left).toString()))
                    valuePresent = true;
            }
            return valuePresent;
        }

我通过了表达

9355560 = 9355560 AND NOT ( comments = comments AND system IN system,admin )
java parsing antlr logical-operators conditional-operator
1个回答
0
投票

在ANTLR中,您可以指定识别和处理“.”等特殊字符的规则。 (点)和“,”(逗号)在你的语法中。要在表达式中允许使用这些特殊字符,您需要为它们定义词法分析器规则并确保正确识别它们。以下是如何修改语法以包含这些特殊字符的示例:

假设您有表达式的语法规则,您可以修改它以包含“.”和 ',' 作为有效字符。这是一个简化的示例:

grammar YourGrammar;

// Lexer rules for special characters
DOT: '.';     // Define DOT as a lexer rule
COMMA: ',';   // Define COMMA as a lexer rule

// Other lexer rules for tokens you might have
// ...

// Your expression rule
expression: term (DOT term)*; // Allow dot-separated terms

// Other grammar rules and definitions
term: ID | NUMBER | STRING; // Define other possible terms

// Define lexer rule for identifiers (assuming you want to allow special characters in identifiers)
ID: [a-zA-Z_][a-zA-Z0-9_]*; // Modify this rule as needed to include special characters

NUMBER: [0-9]+;
STRING: '"' .*? '"';

在上面的示例中,我们添加了“.”的词法分析器规则和 ',' 作为标记。这些标记可以在表达式语法中使用。我们还修改了 ID 规则以允许标识符中包含特殊字符,但您可以根据您的要求调整此规则。

确保根据您的特定语法和要求调整此示例。关键是为特殊字符定义词法分析器规则,并调整任何相关的语法规则,以允许它们根据需要出现在表达式或标识符中。

© www.soinside.com 2019 - 2024. All rights reserved.