gpt4 book ai didi

java - Antlr4 ÅÄÖ 字符无限循环

转载 作者:行者123 更新时间:2023-11-30 07:41:55 27 4
gpt4 key购买 nike

我有一个 Antlr4 语法,在尝试解析表达式时以无限循环结束。

运行 Antlr 4.7 版Java 1.8

表达式看起来像这样:

monkey=Å

但如果正确的变量是一个字符串,它会起作用:

monkey="Å"

或者如果它看起来像这样:

monkey=A

Antlr 在卡住之前打印的最后一条消息是:

line 1:5 mismatched input '' expecting {NUMBER, STRING, BOOLEAN, 'EMPTY', 'NULL'}

遗憾的是,我不是 Antlr 的专家,我试图阅读它,但无法弄清楚这一点。

这是我的语法文件:

grammar MyObjectFilter;


/*
* Lexer rules
*/

fragment DIGIT : [0-9] ;

NUMBER : DIGIT+ ([.,] DIGIT+)?;
// Non-greedy String expression that also removes the quotes from the string
STRING : '"' ( '\\"' | . )*? '"' {setText(getText().substring(1, getText().length()-1));} ;
BOOLEAN : 'true' | 'false';
EMPTY : 'EMPTY';
NULL : 'NULL';

// Remove the $ sign from the start of the identifier
IDENTIFIER : [a-zA-Z][a-zA-Z0-9._-]* ;
VALUE : [0-9]*;

AND : '&&' ;
OR : '||' ;
NOT : '!' ;
NEQ : '!=' ;
GT : '>' ;
GE : '>=' ;
LT : '<' ;
LE : '<=' ;
EQ : '=' ;
LPAREN : '(' ;
RPAREN : ')' ;

WS : [ \r\t\u000C\n]+ -> skip;

/*
* Parser rules
*/
parse
: expression EOF
;

expression
: LPAREN expression RPAREN #parenExpression
| NOT expression #notExpression
| left=identifier op=comparator right=value #comparatorExpression
| left=expression op=binary right=expression #binaryExpression
;

identifier
: IDENTIFIER
;

value
: STRING | NUMBER | BOOLEAN | EMPTY | NULL
;

comparator
: GT | GE | LT | LE | EQ | NEQ
;

binary
: AND | OR
;

初始化:

InputStream stream = new ByteArrayInputStream(definition.getBytes(StandardCharsets.UTF_8));
MyObjectFilterLexer lexer = new MyObjectFilterLexer(CharStreams.fromStream(stream, StandardCharsets.UTF_8));
MyObjectFilterParser parser = new WTObjectFilterParser(new CommonTokenStream(lexer));

//This is where it get stuck.
ExpressionContext expr = parser.expression();

我最好的猜测是它无法确定表达式的 EOF。

最佳答案

有一个匹配零宽度标记(其中有无限数量)的词法分析器规则:

VALUE      : [0-9]*;

将其更改为:

VALUE      : [0-9]+;

关于java - Antlr4 ÅÄÖ 字符无限循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55413161/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com