What is a Parse Tree? 什麼是解析樹
What is a Parse Tree?
Author's name: Carel-Jan Engel Author's Email: cjpengel.dbalert@xs4all.nl |
Date written: Mar 24, 2005 Oracle version(s): N/A |
In documentation about tuning SQL, I see references to parse trees. What is a parse tree ? |
A parse-tree is an internal structure, created by the compiler or interpreter while parsing some language construction. Parsing is also known as 'syntax analysis'.
An example (slightly adapted version of the example found at page 6 of the famous 'Dragon Book', Compilers: principles, techniques and tools, by Alfred V. Aho, Ravi Sethi and Jeffrey D. Ullman, Published by Addison Wesley. My copy is from 1986) will illustrate a parse tree. Rather than dealing with the complexities of a SQL statement, let's take a rather simple language construction: The assignment of the result of an expression to a variable:
result := salary + bonus * 1.10
When the compiler analyzes this statement the resulting parse-tree will look like this :
assignment
________ statement ____
/ | \
/ := \
identifier ___ expression _______
| / | \
result / + \
expression __ expression ___
| / | \
identifier / * \
| expression expression
salary | |
identifier number
| |
bonus 1.10
The picture is an upside-down representation of a tree. The language elements in this small simple assignment are: identifiers (result, salary, bonus), operators (:=, +, *), and a number (1.10). 'Identifier' is the language element that names a variable, function or procedure. 'Operator' is the language element that represents some action to be taken, upon the operands at either end of the operator. Number is a constant, 1.10 in this statement. The syntax rules (' grammar') will specify which 'sentences' are valid.
After successfully decomposing the statement into its internal representation, the compiler or interpreter can 'walk the tree' to create the executable code for the construction. An interpreter will not generate code for the execution, but will invoke built-in executing functions by itself. Let's take the interpreter for the rest of the explanation, execution of the steps is easier to explain than the code-generation of a compiler. For the example I assume the bonus to be 100, and the salary to be 1000. The tree-walk will start at the root of the tree, the assignment statement. The rule for the assignment will tell the interpreter that the right hand has to be evaluated first. This evaluation is also known as 'reduction'. The right hand side of the assignment needs to be reduced to a value, the result of the expression, before it can be assigned to the variable at the left hand side of the statement.
The first node at the right-hand side of the statement contains an expression with a '+' operator. The right hand side of the '+' operator needs to be assigned to the left hand side. So the walk goes on to the next node at the right hand side. There the interpreter will detect the expression with the '*' operator. The left hand side of this operator needs to be multiplied with the right hand side. The interpreter goes on to the right hand side, and detects an expression that consists of a single number: 1.10. This side is fully reduced, the result can be stored and the interpreter walks the tree back up to the '*' operator, and starts evaluating its left hand side. This is an expression that consists of one single identifier, representing a variable, 'bonus'., The memory location represented by this variable is read and it's contents (100) will be multiplied by the right hand side result, 1.10. This expression has been fully reduced to the result 110 now. The interpreter walks up, to the '+' operator, and starts evaluating its left hand side. There it will again detect an identifier, 'salary'. Its location is read (1000) and the expression is reduced to a number, 1000. The right and left hand side will be added, resulting in 1,110. Now the expression at the right hand side of the assignment is fully reduced, and the interpreter walks up the tree, finds the assignment operator ':='. This instructs the interpreter to copy the result of the expression to the left hand side. The left hand side contains an identifier, 'result'. The memory location represented by 'result' will be filled with the result of the expression, 1,110.
It is just a simplified explanation of how an interpreter or compiler uses a parse tree. It's out of scope of this answer to create a complete introduction to compiler building practices. However, it might be clear that creating a parse-tree consumes some resources. Before the language elements can be recognized they must be read character by character, type checking and possible conversion needs to be done, identifiers (tables, columns etc.) need to be identified and checked in the data dictionary, and so on. After this 'hard parse' the parse tree is composed, and is a far cheaper form. to use to execute a statement than doing all this analysis over and over again. Therefore, storing the parse-tree in the SQL-area for future use can save quite some time during the processing of SQL-statements that have come across before.
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/35489/viewspace-676754/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- 【翻譯】 What is class diagram(什麼是類圖)?
- soft parse(軟解析),hard parse(硬解析)
- What is Data Mining 什麼是資料探勘
- 什麼是DNS解析?DNS解析的過程是什麼樣的?DNS
- 圖解:什麼是AVL樹?圖解
- 域名解析是什麼?
- 樹莓派是什麼 樹莓派能做什麼 樹莓派的功能用途樹莓派
- 【翻譯】What is State Machine Diagram(什麼是狀態機圖)?Mac
- 「譯」什麼是抽象語法樹抽象語法樹
- 什麼是DNS雲解析?雲解析和普通解析有什麼區別?DNS
- 什麼是域名解析TTL?
- 什麼是DNS域名解析DNS
- 小議解析parse
- 什麼是DNS解析?如何提升DNS解析安全?DNS
- 什麼是目標框架別名(What are the Target Framework Monikers (TFMs))?框架Framework
- Lesson8 What's your job? 你是做什麼工作的?
- 面試官:什麼是二叉樹面試二叉樹
- 什麼是雲解析?雲解析有哪些特點?
- 5W1H聊開源之What——開源是什麼?
- flutter_parse_sdk解析Flutter
- OB有問必答 | LSM Tree的技術原理是什麼?OceanBase的儲存引擎為什麼基於LSM Tree?儲存引擎
- 一文詳解:什麼是B樹?
- 漫畫演算法:什麼是 B 樹?演算法
- Python機器學習:決策樹001什麼是決策樹Python機器學習
- 雲解析DNS是什麼意思?雲解析DNS有什麼用?(中科三方)DNS
- DNS解析監控是什麼?解析監控有什麼用?(中科三方)DNS
- 什麼是域名解析?如何設定域名解析?
- 什麼是DNS雲解析?雲解析有哪些特點?DNS
- 域名解析中“TTL”是什麼意思?
- PHP carbon :日期解析 Carbon::parsePHP
- Vue原始碼解析之parseVue原始碼
- AWR Execute to Parse引數解析
- [譯]什麼是蒙特卡洛樹搜尋
- 什麼是智慧DNS雲解析?雲解析如何實現智慧解析效果?DNS
- 遞迴解析和權威解析的區別是什麼?遞迴
- 什麼是DNS雲解析?-中科三方DNS
- 什麼是遞迴解析伺服器?遞迴解析伺服器的作用是什麼?遞迴伺服器
- Lesson6 What make is it? 它是什麼牌子的?