我如何将 NSString 方程解析为树? Objective-C
我正在尝试解析当前 NSString 形式的布尔方程。
我想将它解析成一棵树,以便我可以操纵表达式并将其简化为最简单的形式。
就像 Wolfram Alpha 能够做到的那样。
简化输入:
(A and (A or B) or (B and A) or not(A)) and (B or not (A))
to
Not(a) or B
我的问题是将方程解析为树对象,其中每个树节点有3个属性:
1.TreeNode *parent
2.NSMutableArray *children
3.NSString *data
谢谢
I am trying to parse a Boolean equation that is currently in the form of an NSString.
I want to parse it into a tree so that I can manipulate and simplify the expression into its simplest form.
Like Wolfram Alpha is able to do.
Simplifies the input:
(A and (A or B) or (B and A) or not(A)) and (B or not (A))
to
Not(a) or B
My problem is parsing the equation into a tree object where each tree node has 3 properties:
1.TreeNode *parent
2.NSMutableArray *children
3.NSString *data
thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
要将字符串解析为树 (AST),您需要两个组件:一个词法分析器,它将字符串拆分为单独的“标记”(在您的情况下为大括号、运算符、标识符);以及一个解析器,它逐一消耗来自字符串的标记。词法分析器并构建树。对于词法分析器,您可能会使用 NSScanner,您的语法的解析器很容易手动编写(请参见例如 http://en.wikipedia.org/wiki/Recursive_descent_parser),或者您可以使用 yacc 或 Lemon 等工具。
To parse a string into a tree (AST), you need two components: a lexer, which splits the string into individual "tokens" - braces, operators, identifiers in your case, and a parser, that consumes tokens one by one from the lexer and builds the tree. For the lexer you're probably going to use NSScanner, the parser for your grammar is easy to write by hand (see for example http://en.wikipedia.org/wiki/Recursive_descent_parser), or you can use a tool like yacc or Lemon.
我的数学解析器(DDMathParser)应该能够通过一些修改来处理这个问题:
至于简化表达式... DDMathParser 进行基本的表达式重写,这完全是DDMathParser 存储库上的此 wiki 页面中进行了解释。我不确定逻辑表达式是否有任何重写规则(应用德摩根定律等),但添加这些并不难。
关于您的要求:
DDExpression
节点都有一个parentExpression
只读属性arguments 访问
属性DDExpression
节点的子表达式A
和B
实际上将被解析为A()
和B()
(即不带参数的函数)。如果您希望它们被解析为“变量”表达式,则需要在前面添加$
:$A
等。这仅意味着您可以访问名称通过使用function
属性(而不是variable
属性)来定义事物。My math parser (DDMathParser) should be able to handle this with a little modification:
As for simplifying the expression... DDMathParser does rudimentary expression rewriting, which is fully explained in this wiki page on the DDMathParser repository. I'm not sure if there are any rewrite rules for logical expressions (applying DeMorgan's law, etc), but those wouldn't be hard to add.
Regarding your requirements:
DDExpression
node has aparentExpression
readonly propertyDDExpression
node via thearguments
propertyA
andB
will actually be parsed asA()
andB()
(i.e., functions that take no parameters). If you want them to be parsed as "variable" expressions, they'd need a$
in front:$A
, etc. This just means that you can access the name of the thing by using thefunction
property, as opposed to thevariable
property.好的,感谢您的帮助,这是我编写的用于将布尔表达式解析为树的最终 Objective-C 代码:
它需要一个表达式,例如
以下形式:
它与括号优先解析一起使用:
其中 treeNode 对象具有属性和方法:
方法按照名称所暗示的方式执行操作。
希望这可以帮助其他人将来寻找专门用于布尔代数的解析器或作为不同解析器的基础。
Ok so thanks for the help this is the final Objective- C code i wrote to parse a Boolean expression into a tree:
it takes an expression such as
in the form:
It works with brackets parsing with precedence:
Where the treeNode object has properties and methods:
The methods do as implied by there names.
Hope this can help anyone else in future looking for a parser either specifically for Boolean algebra or maybe as a basis for a different parser.