布尔和数学表达式解析器

发布于 2024-08-22 06:23:17 字数 627 浏览 2 评论 0原文

我正在编写一个允许用户输入布尔表达式的应用程序。我需要能够在运行时评估输入的布尔表达式,并且正在寻找解析器和表达式验证器。

解析器
解析器需要将布尔表达式作为字符串并返回 true/false。

示例:

string expression = "(1 == 1) && (1 > 0)";
Parser parser = new Parser();
boolean result = parser.parse(expression);  // Result should be True.

除了处理布尔表达式之外,我还需要它来处理数学。

expression = "((1 + 1 * 2) == 1)";
result = parser.parse(expression);  // Result should be False.

验证
这样我就可以告诉用户输入的表达式是否存在问题,我还需要一种验证语法的方法。

我正在使用 .NET Compact Framework 使用 C# 进行工作,但如果您知道用其他语言编写的内容可能会有所帮助。

感谢您提供的任何帮助。 汤姆

I am writing an application that allows a user to enter a boolean expression. I need the ability to evaluate the entered boolean expression at runtime and am looking for both a parser and a expressoin validator.

Parser
The parser needs to take a boolean expression as a string and return true/false.

Example:

string expression = "(1 == 1) && (1 > 0)";
Parser parser = new Parser();
boolean result = parser.parse(expression);  // Result should be True.

In addition to handling boolean expressions I also need it to handle Math.

expression = "((1 + 1 * 2) == 1)";
result = parser.parse(expression);  // Result should be False.

Validate
So that I can tell the user if there is a problem with the expression being entered I also need a way to validate the syntax.

I am working in C# using the .NET Compact Framework, but if you know of something written in another language that may be helpful.

Thanks for any help you can provide.
Tom

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

债姬 2024-08-29 06:23:17

我们的项目正在使用 NCalcANTLR 下面用于词法分析/解析),我们对此非常满意。

NCalc是一个数学表达式
.NET 中的评估器。 NCalc 可以解析任何
表达式并评估结果,
包括静态或动态参数
和自定义函数。

我们的应用程序要求它针对完整框架和紧凑框架进行交叉编译。通过相对简单的调整,我们能够使 NCalc 和 ANTLR 适用于两种框架风格。

Our project is using NCalc (with ANTLR underneath for lexing/parsing) and we're very happy with it.

NCalc is a mathematical expressions
evaluator in .NET. NCalc can parse any
expression and evaluate the result,
including static or dynamic parameters
and custom functions.

Our application requires that it be cross-compiled for both Full and Compact Frameworks. With relatively simple tweaks we were able to make both NCalc and ANTLR work for both framework flavours.

迷乱花海 2024-08-29 06:23:17

http://www.antlr.org

Antlr 语法可以设计为允许解析和评估。

这是一个示例: http://www.antlr.org/wiki/display/ ANTLR3/表达式+评估器

http://www.antlr.org

Antlr grammars can be designed to allow for both parsing and evaluation.

Here's an example: http://www.antlr.org/wiki/display/ANTLR3/Expression+evaluator

守望孤独 2024-08-29 06:23:17

假设您可以稍微更改语法,让嵌入式数据库使用如下 T-SQL 查询为您完成工作:

select case when <Expression> then 1 else 0 end as Result

使用您的示例:

select case when ((1 = 1) and (1 > 0)) then 1 else 0 end as Result
select case when ((1 + 1 * 2) = 1) then 1 else 0 end as Result

Assuming you can change your syntax slightly, let an embedded database do the work for you with a query like this T-SQL:

select case when <Expression> then 1 else 0 end as Result

Using your example:

select case when ((1 = 1) and (1 > 0)) then 1 else 0 end as Result
select case when ((1 + 1 * 2) = 1) then 1 else 0 end as Result
百善笑为先 2024-08-29 06:23:17

我不知道有什么库可以让这变得更容易,但这里确实有两个子问题。您需要构建一个中缀到后缀转换器,然后编写一个用于布尔和数学运算的基本计算器。

一旦构建了布尔树/堆栈,就开始执行操作。如果您有任何不是数字的内容,请通过将字符串/表达式发送到算术计算器来评估它,该算术计算器执行中缀->后缀转换,然后返回一个值。

如果你用谷歌搜索“infix to postfix”和“stack rpn Calculator”,你可能会找到更多资源。

I don't know of any libraries to make this easier, but you really just have two subproblems here. You need to build an infix to postfix converter, then write a basic calculator for the boolean and math operations.

Once you have your boolean tree/stack built, begin performing operations. If you have anything that's not a number, evaluate it by sending the string/expression to the arithmetic calculator which performs infix->postfix conversion and then returns a value.

If you google "infix to postfix" and "stack rpn calculator", you can probably find more resources.

许一世地老天荒 2024-08-29 06:23:17

您可以使用 dotMath 库来执行此操作。

You may be able to use the dotMath library to do this.

我ぃ本無心為│何有愛 2024-08-29 06:23:17

这是 Codeproject 上的一个出色的评估解析器,它使用 eval 方法,并且不使用依赖 CodeDOM 或类似的东西。这是一篇关于使用 Antlr 构建 表达式评估器 的优秀文章,也在同一主题上网站..

希望这有帮助,
此致,
汤姆.

Here's an excellent evaluation parser on Codeproject, that uses the eval method and does not rely on CodeDOM or anything like that. Here's an excellent article on building an expression evaluator using Antlr, also on the same site..

Hope this helps,
Best regards,
Tom.

雨轻弹 2024-08-29 06:23:17

这种东西是 F# 的面包和黄油。你可以尝试一下。对于解析,请使用递归下降,然后您可以遍历生成的树。如果您可以控制输入语言,则可以通过引用操作来完成。

This type of thing is F#'s bread and butter. You might give that a try. For parsing, use recursive descent, then you can run over the tree that results. If you have control of the input language, you can get by with a quote operation.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文