extended operands——遞歸數據結構的一種處理方式

AST中有關extended operands

最近遇到Java語言的AST中,eclipse對於中綴表達式InfixExpression的AST一個解析後的表示方式:
下面是org.eclipse.jdt.core.dom.InfixExpression#extendedOperands方法的註釋:

/**
* Returns the live list of extended operands.
* The extended operands is the preferred way of representing deeply nested expressions of 
* the form L op R op R2 op R3... where the same operator appears between all the operands 
* (the most common case being lengthy string concatenation expressions). Using the extended 
* operands keeps the trees from getting too deep; this decreases the risk is running out of 
* thread stack space at runtime when traversing such trees. ((a + b) + c) + d would be 
* translated to: leftOperand: a rightOperand: b extendedOperands: {c, d} operator: +
* 
* Returns:
* the live list of extended operands (element type: Expression)
* 
*/
public List extendedOperands();

意思大概如下:

返回live list of extended operands(活躍的擴展操作數列表?)
The extended operands是一種更好的表示形如這種深層嵌套的表達式L op R op R2 op R3...,這裏所有一樣的
操作符將這些操作數連接起來(最常見的例子就是字符串拼接"str1" + "str2" + "str3" + ... + "strn")。
使用extended operands能夠避免這顆中綴表達式的語法樹太深,這樣就能夠使我們在遍歷這樣一顆樹時,降低運行時 線程棧空間被耗盡的風險;例如((a + b) + c) + d將會被解析成:


leftOperand: a
rightOperand: b
extendedOperands: { c, d }
operator: +

即如下所示的AST結構:↙↘

                 a + b + c + d
                     ↓  // 被解析成如下的抽象語法樹(AST)結構+  
                   ↙ ↓ ↘
                 ↙   ↓   ↘
                a    b  extendedOperands
                               ↙  ↘
                              c    d

這樣的處理方式有什麼用?
能夠爲我們設計遞歸的數據結構時,提供一種思路。
像這種比較簡單的字符串連接,很容易理解,沒必要弄成一個很大的且無意義的樹

"str1" + "str2" + "str3" + ... + "strn"
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章