Antlr 解析器和/或逻辑 - 如何获取逻辑运算符之间的表达式? [英] Antlr parser for and/or logic - how to get expressions between logic operators?
问题描述
我正在使用 ANTLR 创建和/或解析器+评估器.表达式的格式如下:
I am using ANTLR to create an and/or parser+evaluator. Expressions will have the format like:
x eq 1 &&y eq 10
(x lt 10 && x gt 1) OR x eq -1
我正在阅读这篇关于 ANTLR 中的逻辑表达式的帖子 寻求建议项目上.解析逻辑表达式,我发现这里发布的语法是一个好的开始:
I was reading this post on logic expressions in ANTLR Looking for advice on project. Parsing logical expression and I found the grammar posted there a good start:
grammar Logic;
parse
: expression EOF
;
expression
: implication
;
implication
: or ('->' or)*
;
or
: and ('&&' and)*
;
and
: not ('||' not)*
;
not
: '~' atom
| atom
;
atom
: ID
| '(' expression ')'
;
ID : ('a'..'z' | 'A'..'Z')+;
Space : (' ' | '\t' | '\r' | '\n')+ {$channel=HIDDEN;};
然而,虽然从解析器获取一棵树适用于变量只有一个字符的表达式(即,"(A || B) AND C"
,但我很难适应这对我来说(在示例中 "x eq 1 && y eq 10"
我希望有一个 "AND"
父母和两个孩子,"x eq 1"
和 "y eq 10"
,见下面的测试用例).
However, while getting a tree from the parser works for expressions where the variables are just one character (ie, "(A || B) AND C"
, I am having a hard time adapting this to my case (in the example "x eq 1 && y eq 10"
I'd expect one "AND"
parent and two children, "x eq 1"
and "y eq 10"
, see the test case below).
@Test
public void simpleAndEvaluation() throws RecognitionException{
String src = "1 eq 1 && B";
LogicLexer lexer = new LogicLexer(new ANTLRStringStream(src));
LogicParser parser = new LogicParser(new CommonTokenStream(lexer));
CommonTree tree = (CommonTree)parser.parse().getTree();
assertEquals("&&",tree.getText());
assertEquals("1 eq 1",tree.getChild(0).getText());
assertEquals("a neq a",tree.getChild(1).getText());
}
我相信这与ID"
有关.正确的语法是什么?
I believe this is related with the "ID"
. What would the correct syntax be?
推荐答案
对于那些有兴趣的人,我对我的语法文件做了一些改进(见下文)
For those interested, I made some improvements in my grammar file (see bellow)
当前限制:
仅适用于 &&/||,不适用于 AND/OR(问题不大)
only works with &&/||, not AND/OR (not very problematic)
括号和 &&/|| 之间不能有空格(我通过在提供词法分析器之前将源字符串中的(")替换为)"并将)"替换为)"来解决这个问题)
you can't have spaces between the parenthesis and the &&/|| (I solve that by replacing " (" with ")" and ") " with ")" in the source String before feeding the lexer)
语法逻辑;
options {
output = AST;
}
tokens {
AND = '&&';
OR = '||';
NOT = '~';
}
// parser/production rules start with a lower case letter
parse
: expression EOF! // omit the EOF token
;
expression
: or
;
or
: and (OR^ and)* // make `||` the root
;
and
: not (AND^ not)* // make `&&` the root
;
not
: NOT^ atom // make `~` the root
| atom
;
atom
: ID
| '('! expression ')'! // omit both `(` and `)`
;
// lexer/terminal rules start with an upper case letter
ID
:
(
'a'..'z'
| 'A'..'Z'
| '0'..'9' | ' '
| SYMBOL
)+
;
SYMBOL
:
('+'|'-'|'*'|'/'|'_')
;
这篇关于Antlr 解析器和/或逻辑 - 如何获取逻辑运算符之间的表达式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!