逻辑表达式解析器 [英] Logic expression parser

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

问题描述

我正在尝试为以下表达式创建一个逻辑表达式解析器:
((VariableA - > VariableB)AND NOT VariableC)
解析器应​​该能够返回,无论结果为真还是对于给定的变量值为false。



基本上,表达式只包含变量,逻辑运算符(或,和,暗示,等价,否定和括号)。



我想问一下实现这种解析器的最好方法是什么(使用AST树或反向波兰符号)?或者也许已经有一些可以做这个工作的开源解析器?

解决方案

你定位什么语言?

如果您想创建一个解析器, ANTLR 将会你的诀窍它最初是基于java的,但它有各种语言的生成器(我用它来生成一个C#解析器),并不难太难。
它有一个漂亮的编辑器(ANTLRWorks),允许测试语法,这是一个很好的加分。


I'm trying to create a logic expression parser for expressions like: ((VariableA -> VariableB) AND NOT VariableC) The parser should be able to return, whether the result is true or false for given values of variables.

Basically, the expressions will only contain variables, logical operators (or, and, implication, equivalence, negation and parentheses).

I would like to ask what is the best way to implement this kind of parser (using AST tree, or Reverse Polish Notation)? Or maybe there already exist some open source parsers that can do the job?

解决方案

What language are you targeting?

If you want to create a parser, maybe ANTLR will do the trick for you. It is originally java-based but it has generators for a variety of languages (I use it for generating a C# parser for example) and is no too difficult to pick-up. It has a nice editor (ANTLRWorks) that allows testing the grammar, which is a nice plus.

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

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