Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Parse Tree

Download as pdf or txt
Download as pdf or txt
You are on page 1of 10

3/6/2009

1
Software Engineering II Software Engineering II
Algorithms and Data Structures Algorithms and Data Structures
Parse Trees Parse Trees
Dr. Christos Bouganis
EE2/ISE1 Algorithms & Data Structures
g
Imperial College London
1
Problem setting Problem setting
Evaluate the following expression:
X + 2*Y 7+8*Z*(X+2+Z) 4*X*Y 2*Y + K
What if K is the only variable that changes?
EE2/ISE1 Algorithms & Data Structures
3/6/2009
2
Objectives Objectives
Turn an expression into a parse tree
X + 2*Y 7

+
*
2 Y
X
7
EE2/ISE1 Algorithms & Data Structures
2 Y
The tree can be used to guide and optimize the expression
evaluation
Link to the previous lecture: The previous lecture looked
Overview Overview
at evaluating an expression while parsing it. This lecture
looks at turning an expression or sentence of a formal
language into a parse tree.
This is what most compilers do as an intermediate step
towards compiling a program.
The parse tree can then be processed or optimized. Well
see how to evaluate an expression from its parse tree.
3/6/2009
3
A parse tree is a tree structure representing sentences or
expressions of a formal language that mirrors the
grammar of that language, as defined by its BNF.
Parse Trees Parse Trees
For example, the parse tree for the expression
X + 2*Y 7
would be the following:

+
*
2 Y
X
7
<expression> ::= <term> |
<expression> + <term> |
<expression> <term>
<term> ::= <factor> |
<term> * <factor> |
<term> / <factor>
<factor> ::= <number> | ( <expression> )
<expression> ::= <term> |
<expression> + <term> |
<expression> <term>
<term> ::= <factor> |
X + 2*Y 7
Rule 1
How to construct a parse tree How to construct a parse tree
|
<term> * <factor> |
<term> / <factor>
<factor> ::= <number> | ( <expression> )
X + 2*Y 7
Rule 2
Rule 3

Rule 1

Rule 1

Rule 2
X +2*Y 7
+
X
7
2*Y
+
*
2 Y
X
7
3/6/2009
4
Declare a Parse Tree in C++ Declare a Parse Tree in C++
Lets declare the node structure of the
tree tree.

+
7
EE2/ISE1 Algorithms & Data Structures
*
2 Y
X
Heres the type declaration for a parse tree for arithmetic
expressions.
The leaves of the tree (nodes with no sub-trees) are always
numbers Other nodes comprise an operator and two
Parse Trees in C++ Parse Trees in C++
numbers. Other nodes comprise an operator and two
operands, where the operands are themselves trees.

struct TreeNode {
bool isLeaf; // true for leaf node
int number; // filled for leaf node
TreeNode* leftTree; // Pointer to left Tree
TreeNode* rightTree; // Pointer to right Tree
+
*
2 Y
X
7
g ; g
char op; // +,-,*, or /
};
typedef TreeNode* TreePtr;
3/6/2009
5
We assume that we have the access routines to handle
the expression string as in the last lecture.
We also assume the same routines to access the parse
Access Routines for Parse Trees I Access Routines for Parse Trees I
We also assume the same routines to access the parse
tree:
In addition, we have the following two access
procedures for building the parse tree and its leaves.
Access Routines for Parse Trees II Access Routines for Parse Trees II
3/6/2009
6
and
Access Routines for Parse Trees III Access Routines for Parse Trees III
sso far... o far...
Importanc
e of
parsing
trees
How to
create a
parse
Access
routines
for tree
creation
C code to
parse an
expression
C code
to parse
a term
C code
to parse
a factor
Example
p
tree
p
3/6/2009
7
The functions that turn an arithmetic expression into a
parse tree are much like the procedures in the previous
lecture for evaluating an expression.
X+Y+Z
From Expressions to Trees From Expressions to Trees
To generate the parse tree T1 for an expression E, this is
what you do.
Parse the next term in E, and let T1 be the resulting
tree.
While the next character in E is an operator (+ or ),
Read past the operator.
T1 T2
+
T2 T1
T2
+
Y X
+
Z
p p
Parse the next term in E, giving tree T2.
Let T1 be either or , depending on the
operator.
+
T1 T2

T1 T2
Heres the C++ code.
Parsing an expression Parsing an expression
3/6/2009
8
Heres the C++ code for parsing a term. No surprises here.
Parsing a Term Parsing a Term
Heres the code for parsing a factor. Again, no surprises.
Parsing a Factor Parsing a Factor
3/6/2009
9
100*(3+4) 10*(8/2)
Building a Parse Tree: An example Building a Parse Tree: An example
<expression> ::= <term> |
<expression> + <term> |
<expression> <term>
<term> ::= <factor> |
100*(3+4) - 10*(8/2)
<term> ::= <factor> |
<term> * <factor> |
<term> / <factor>
<factor> ::= <number> | ( <expression> )
So what? How can we evaluate the expression from that tree?
Here is the routine for evaluating an arithmetic expression from its
parse tree. Just traverse the tree and apply the operators in the nodes
Evaluating a Parse Tree Evaluating a Parse Tree
3/6/2009
10
Here is the routine to print the tree in console window.
void printTree(TreePtr expTree) {
if (isLeafNode(expTree))
cout << leafValue(expTree) << endl;
Printing a Tree Printing a Tree
cout leafValue(expTree) endl;
else {
printTree(leftOf(expTree));
cout << nodeOp(expTree) << endl;
printTree(rightOf(expTree));
}
}
Input: 100*(3+4)-10*(8/2)
Result: 100*3+4-10*8/2
What change should I make in order to
print the round brackets as well?
In the last two lectures we learnt to
parse an arithmetic expression
Summary Summary
evaluate an arithmetic expression (C++ code)
create a parsing tree of an arithmetic expression
evaluate the expression using a parse tree.

You might also like