布尔和数学表达式解析器 [英] Boolean and Math Expression Parser

查看:195
本文介绍了布尔和数学表达式解析器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



解析器

解析器需要使用一个布尔表达式作为字符串,并返回true / false。



示例:_

 
string expression =(1 == 1)&&(1> 0);
解析器解析器=新的Parser();
boolean result = parser.parse(expression); //结果应为True。

除了处理布尔表达式,我还需要它来处理Math。

 
expression =((1 + 1 * 2)== 1);
result = parser.parse(expression); //结果应该是False。

验证

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



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



感谢您提供的任何帮助。
Tom

解决方案

http://www.antlr.org



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



以下是一个示例: http:// www。 antlr.org/wiki/display/ANTLR3/Expression+evaluator


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

解决方案

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

这篇关于布尔和数学表达式解析器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆