[英]Efficient algorithm for expression parser (Java)
I am trying to design an algorithm that can parse an expression in the form of a String.我正在尝试设计一种可以解析字符串形式的表达式的算法。 I want to be able to extract the operands and the operation from the given expression.
我希望能够从给定的表达式中提取操作数和操作。 Also, I want the algorithm to recognize bracket balance.
另外,我希望算法能够识别括号平衡。 No need for precedence of operations, as the input of the algorithm will include brackets if there are more than 1 binary operations.
不需要操作的优先级,因为如果有超过 1 个二进制操作,算法的输入将包含括号。 For unary operations, if a "-" appears before a bracket, it means the entire expression inside the respective brackets is the operand.
对于一元运算,如果括号前出现“-”,则表示相应括号内的整个表达式为操作数。 Examples:
例子:
-parsing "a+b" gives "a" and "b" as operands and "+" as operation.
-parsing "(a/b) - (c*v)" gives "a/b" and "c*v" as operands and "-" as operation.
-parsing "((a/(b))) - (((c)*v))" gives the same result as above
-parsing "-a" gives operand as "a" and operation as "-"
-parsing "a + (-c/v)" gives "a" and "-c/v" as operands and "+" as operation
-parsing "-(c)" gives "c" is operand and "-" as operands
-parsing "(-(c))" gives same result as above
Thanks谢谢
Try this.尝试这个。
record Node(String name, Node left, Node right) {
@Override
public String toString() {
return "Node[" + name
+ (left != null ? ", " + left : "")
+ (right != null ? ", " + right : "") + "]";
}
}
and和
static Node parse(String input) {
return new Object() {
int index = 0;
int ch() { return index < input.length() ? input.charAt(index) : -1; }
boolean eat(char expected) {
while (Character.isWhitespace(ch())) ++index;
if (ch() == expected) {
++index;
return true;
}
return false;
}
Node factor() {
Node node;
boolean minus = eat('-');
if (eat('(')) {
node = expression();
if (!eat(')'))
throw new RuntimeException("')' expected");
} else if (Character.isAlphabetic(ch())) {
node = new Node(Character.toString(ch()), null, null);
++index;
} else
throw new RuntimeException("unknown char '" + (char)ch() + "'");
if (minus) node = new Node("-", node, null);
return node;
}
Node expression() {
Node node = factor();
while (true)
if (eat('*')) node = new Node("*", node, factor());
else if (eat('/')) node = new Node("/", node, factor());
else if (eat('+')) node = new Node("+", node, factor());
else if (eat('-')) node = new Node("-", node, factor());
else break;
return node;
}
}.expression();
}
test:测试:
static void testParse(String input) {
System.out.printf("%-22s -> %s%n", input, parse(input));
}
public static void main(String[] args) {
testParse("a+b");
testParse("(a/b) - (c*v)");
testParse("((a/(b))) - (((c)*v))");
testParse("-a");
testParse("-a + (-c/v)");
testParse("-(c)");
testParse("(-(c))");
}
output: output:
a+b -> Node[+, Node[a], Node[b]]
(a/b) - (c*v) -> Node[-, Node[/, Node[a], Node[b]], Node[*, Node[c], Node[v]]]
((a/(b))) - (((c)*v)) -> Node[-, Node[/, Node[a], Node[b]], Node[*, Node[c], Node[v]]]
-a -> Node[-, Node[a]]
-a + (-c/v) -> Node[+, Node[-, Node[a]], Node[/, Node[-, Node[c]], Node[v]]]
-(c) -> Node[-, Node[c]]
(-(c)) -> Node[-, Node[c]]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.