在 Perl 中解析逻辑表达式并将其转换为树
我有复杂的逻辑表达式,如下所示:
((((!((cond1) || (cond2) || (cond3)) && (cond4))) && (cond5)) <= (((cond6) || (cond7) || (cond8)) || (cond9)))
每行有几十个表达式。允许的逻辑符号是 ||
、&&
、!
和 <=
。 <=
表示引导,如 a <= b
表示 b 引导到 a。
我需要仔细检查这些声明并检查条件,因为其中一些不再有效。我希望能够将其解析为一棵树,然后检查每个叶子(其中每个叶子都是一个条件),删除不需要的叶子并重新构建完整且正确的表达式。
我知道树的每个节点都是由一对第一个括号和关闭它的括号定义的,但我不知道如何识别这些对以及如何识别它们之间的逻辑符号。
除 !
之外的所有符号都位于两个表达式之间。
I have complex logical expression that look like this:
((((!((cond1) || (cond2) || (cond3)) && (cond4))) && (cond5)) <= (((cond6) || (cond7) || (cond8)) || (cond9)))
Each line has several dozens of expressions. The allowed logical signs are ||
, &&
, !
and <=
. <=
means leads, as in a <= b
means that b leads to a.
I need to go over those statements and check the conditions, since some of them are no longer valid. I want to be able to parse it to a tree, then check each of it leafs (where each leaf is a condition), delete the unwanted leafs and build the full and correct expressions back.
I know that each node of the tree is defined by a pair of the first bracket and the bracket that closes it, but I don't know how to identify such pairs and how to identify the logical sign between them.
All signs except for !
come between two expressions.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
听起来像是 Parse::RecDescent 的情况:
语法,英语:
整体事物是一种表达。表达式是一个操作数,后跟零个或多个二元运算符及其操作数。每个操作数都是带括号的表达式“!”后跟一个操作数或一个单词(例如
cond1
)。生成的树中的每个节点都采用以下形式之一:
cond1
- 条件{ 'op' =>; '!', '值' => '节点' }
- !应用于另一个节点{ 'lvalue' =>; '节点', '操作' => [ 一项或多项:{ 'op' => 'binop', '右值' => 'node' } ] }
- 代表节点 binop 节点 binop 节点的一系列一个或多个操作...我没有破坏一系列二进制操作(例如
((cond1) || (cond2) | | (cond3))
) 到二叉树中,因为您没有提供有关优先级或关联性的信息。您的示例的输出是:
Sounds like a case for Parse::RecDescent:
The grammar, in English:
The whole thing is an expression. An expression is an operand followed by zero or more binary operators and their operands. Each operand is either a parenthesized expression, '!' followed by an operand, or a word (e.g.
cond1
).Every node in the produced tree is in one of the following forms:
cond1
- a condition{ 'op' => '!', 'value' => 'node' }
- ! applied to another node{ 'lvalue' => 'node', 'operations' => [ one or more of: { 'op' => 'binop', 'rvalue' => 'node' } ] }
- series of one or more operations representing node binop node binop node ...I didn't break series of binary operations (e.g.
((cond1) || (cond2) || (cond3))
) into a binary tree because you provided no information about precedence or associativity.Output for your example is: