我如何推断括号的使用翻译的前pression树是什么时候? [英] How do I infer the usage of parentheses when translating an expression tree?
问题描述
我正在翻译的前pression树类似于缀表示法的格式;我不评价树或执行其业务。该树包含逻辑和关系操作,我想在翻译过程中发出的括号以智能的方式。
要说明这一点,考虑以下做作前pression:
A< X'放大器; (A< Y |一==三)及;一!= D
如果我走这个EX pression按顺序产生的EX pression棵树,那么我就会打印出以下EX pression,这是不正确。
A< X'放大器; A< Y |一== C和;一!= D
//等效于(一个&其中X - 安培;一个&所述; Y)| (一== C和;!A = D)
另外,我可以再次执行中序遍历但在此之前和处理的二进制EX pression后发出的括号。这将产生以下正确的EX pression,但有几个多余的括号。
(((A< X)及((A< Y)|(A == C)))及(A = D)!)
有没有产生最佳,括号中的前pressions一个前pression树的遍历算法?
作为参考,在这里是的<一个片段href="http://msdn.microsoft.com/en-us/library/system.linq.ex$p$pssions.ex$p$pssionvisitor.aspx"><$c$c>Ex$p$pssionVisitor$c$c>我使用的检查树。
类MyVisitor:防爆pressionVisitor
{
保护覆盖防爆pression VisitBinary(BinaryEx pression节点)
{
Console.Write(();
访问(node.Left);
Console.WriteLine(node.NodeType.ToString());
访问(node.Right);
Console.Write());
返回节点;
}
// VisitConstant,VisitMember,和VisitParameter为简洁起见省略。
}
尝试这样的事情,假设 node.NodeType
的类型是的NodeType
,并且函数 precedes
存在,如果第一个参数precedes第二返回true。
保护覆盖防爆pression访问(BinaryEx pression节点的NodeType ParentType的)
{
布尔useParenthesis = precedes(ParentType的,node.NodeType);
如果(useParenthesis)
Console.Write(();
访问(node.Left,node.NodeType);
Console.WriteLine(node.NodeType.ToString());
访问(node.Right,node.NodeType);
如果(useParenthesis)
Console.Write());
返回节点;
}
I am working on translating an expression tree to a format that resembles infix notation; I am not evaluating the tree or executing its operations. The tree contains both logical and relational operations, and I would like to emit parentheses in an intelligent manner during the translation.
To illustrate, consider the following contrived expression:
a < x & (a < y | a == c) & a != d
If I walk the expression tree produced by this expression in-order, then I will print out the following expression, which is incorrect.
a < x & a < y | a == c & a != d
// equivalent to (a < x & a < y) | (a == c & a != d)
Alternatively, I can again perform an in-order traversal but emit parentheses before and after processing a binary expression. This will produce the following correct expression, but with several redundant parentheses.
(((a < x) & ((a < y) | (a == c))) & (a != d))
Is there an expression tree traversal algorithm that produces optimally-parenthesized expressions?
For reference, here is a snippet of the ExpressionVisitor
I am using to inspect the tree.
class MyVisitor : ExpressionVisitor
{
protected override Expression VisitBinary(BinaryExpression node)
{
Console.Write("(");
Visit(node.Left);
Console.WriteLine(node.NodeType.ToString());
Visit(node.Right);
Console.Write(")");
return node;
}
// VisitConstant, VisitMember, and VisitParameter omitted for brevity.
}
Try something like this, assuming that node.NodeType
is of type NodeType
, and that function Precedes
exists and returns true if first parameter precedes the second.
protected override Expression Visit(BinaryExpression node, NodeType parentType)
{
bool useParenthesis = Precedes(parentType, node.NodeType);
if (useParenthesis)
Console.Write("(");
Visit(node.Left, node.NodeType);
Console.WriteLine(node.NodeType.ToString());
Visit(node.Right, node.NodeType);
if (useParenthesis)
Console.Write(")");
return node;
}
这篇关于我如何推断括号的使用翻译的前pression树是什么时候?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!