布尔和数学防爆pression分析器分析器、布尔、数学、pression

2023-09-02 10:30:30 作者:醉卧江南烟雨中

我写一个应用程序,允许用户输入一个布尔EX pression。我需要评估所输入的布尔EX pression在运行时的能力,并正在寻找既解析器和EX pressoin验证。

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.

解析器 解析器需要采取一个布尔EX pression为一个字符串,返回真/假。

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

例:


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

在除了处理布尔EX pressions我也需要它来处理数学运算。

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.

验证 所以,我可以告诉用户,如果有被输入我还需要一种方法来验证语法与前pression一个问题。

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.

我的工作中使用.NET Compact Framework的C#,但如果你知道的东西写在另一种语言可能会有所帮助。

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

推荐答案

http://www.antlr.org

ANTLR的语法可以被设计为允许既分析和评价。

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

下面是一个例子: HTTP://www.antlr。组织/维基/显示/ ANTLR3 / EX pression +评估