使用 Java 的递归表达式计算器
我将编写一个仅执行加法和减法的表达式计算器。我有一个简单的算法可以做到这一点;但是,我有一些实施问题。
我将表达式视为(它是一个字符串)
"(" <expression1> <operator> <expression2> ")"
这是我的算法
String evaluate( String expression )
if expression is digit
return expression
else if expression is "(" <expression1> <operator> <expression2> ")"
cut the brackets out of it
expression1 = evaluate( <expression1> )
operator = <operator>
expression2 = evaluate( <expression2> )
if operator is +
expression1 + expression2
else if operator is -
expression1 - expression2
我的问题是解析
、
和
来自表达式。我怎样才能做到这一点?
注意:我不是要求代码。我所需要的只是一个想法来做到这一点。
I am going to write an expression evaluator which only does addition and subtraction. I have a simple algorithm to do that; but, I have some implementation problems.
I considered an expression as (it is a String)
"(" <expression1> <operator> <expression2> ")"
Here is my algorithm
String evaluate( String expression )
if expression is digit
return expression
else if expression is "(" <expression1> <operator> <expression2> ")"
cut the brackets out of it
expression1 = evaluate( <expression1> )
operator = <operator>
expression2 = evaluate( <expression2> )
if operator is +
expression1 + expression2
else if operator is -
expression1 - expression2
My problem is parsing <expression1>
, <operator>
and <expression2>
from the expression. How can I do that?
Note: I'm not asking for a code. All I need is an idea to do that.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
那么不要这样做:) 当您看到左括号时,请对表达式进行递归调用。在表达式的末尾,您要么找到另一个运算符(因此您毕竟不在表达式的末尾),要么找到右括号,在这种情况下,您从评估中返回。
Don't do that, then :) When you see an opening bracket, do your recursive call to expression. At the end of the expresssion, either you find another operator (and so you're not at the end of the expression after all), or a right-bracket, in which case you return from the evaluate.
您可以使用解析器生成器,例如 JavaCUP 或 ANTLR。编写表达式的 BNF 并生成解析器。下面是一个可以帮助您入门的示例语法:
自己执行此操作的“hacky”方法是查找第一个
)
回溯到最接近的(
查看之间的括号自由表达,只需拆分运算符符号并求值即可。Either you use a parser generator such as JavaCUP or ANTLR. Write up a BNF of your expression and generate a parser. Here is a sample grammar that would get you started:
A "hacky" way of doing it yourself would be to look for the first
)
backtrack to the closest(
look at the parenthesis free expression in between, simply split on the operator symbols and evaluate.使用 StringTokenizer 将输入字符串拆分为括号、运算符和数字,然后迭代标记,对每个左括号进行递归调用,并针对每个右括号退出方法。
我知道您没有要求代码,但这适用于有效输入:
它需要更好地处理无效输入,但您明白了...
Use a StringTokenizer to split your input string into parenthesis, operators and numbers, then iterate over your tokens, making a recursive call for every open-parens, and exiting your method for every close parenthesis.
I know you didn't ask for code, but this works for valid input:
It would need better handling of invalid input, but you get the idea...
我建议将中缀输入更改为后缀,然后对其进行评估,而不是逐个减少中缀表达式。已经有明确定义的算法,并且它不存在固有的多重嵌套括号解析问题。
查看 Shunting Yard Algorithm 以转换为 postfix/RPN,然后使用使用 Postfix 操作 的堆栈。这是快速 (O(n)) 且可靠的。
I would recommend changing the infix input into postfix and then evaluating it, rather than reducing the expression infix-wise. There are already well defined algorithms for this and it doesn't come with the inherent multiple-nested-parentheses parsing problems.
Take a look at the Shunting Yard Algorithm to convert to postfix/RPN then evaluate it using a stack using Postfix Operations. This is fast (O(n)) and reliable.
我建议采取一种更类似于this中描述的方法,但(在我看来)有关编译器设计的相关系列文章。我发现使用小函数/方法来解析部分表达式的方法非常有效。
这种方法允许您将解析方法分解为许多子方法,这些子方法的名称和执行顺序紧密遵循 EBNF 您可以用来描述要解析的表达式。
I would suggest taking an approach that more closely resembles the one described in this old but (in my opinion) relevant series of articles on compiler design. I found that the approach of using small functions/methods that parse parts of the expression to be highly effective.
This approach allows you to decompose your parsing method into many sub-methods whose names and order of execution closely follows the EBNF you might use to describe the expressions to be parsed.
也许为表达式和<创建正则表达式 em>operator,然后使用匹配来识别和分解您的内容。
Perhaps create regular expressions for expression and operator and then use matching to identify and break out your content.