The Derivation Tree
Chapter Forty-Two
Syllabus topic Module 1, "Context Free Languages: Derivation Tree"
Pages 208 to 212 of 438
In one line
A derivation tree records which production was applied to which symbol, and forgets the order in which the applications were made.
In the wording a student can write in an examination: a derivation tree, also called a parse tree, for a context free grammar G is an ordered tree in which the root is labelled with the start symbol, every interior vertex is labelled with a variable, every leaf is labelled with a terminal or with epsilon, and if an interior vertex labelled A has children labelled X1 to Xk from left to right then A to X1 ... Xk is a production of G. The yield of the tree is the string of leaf labels read left to right.
Why a tree as well as a derivation
Because a derivation records too much.
Chapter 23 showed that one string can have many derivations differing only in the order the variables were taken. For the grammar S to AB with A to a and B to b, the string ab has two derivations: expand A first, or expand B first. They are different sequences and they say exactly the same thing about the structure of ab.
The tree throws away the order and keeps the structure. So two derivations that differ only in order give the same tree, and two derivations that differ in which production was applied to which symbol give different trees. That is precisely the distinction ambiguity is about, and it is why chapter 44 is defined on trees and not on derivations.
The tree is also what a compiler actually builds. A parser's output is not a sequence of rewriting steps; it is a tree, and the next stage of the compiler walks it.
The four conditions
Each is a condition on the labels, and an answer that drops one has not defined the tree.
The root is the start symbol.
Every interior vertex is labelled with a variable. A terminal can never have children, because no production rewrites a terminal.
Every leaf is labelled with a terminal or with epsilon. A leaf labelled epsilon is the child of a variable that was rewritten by a null production, and it is the only child of that vertex.
The children of a vertex spell a production's right hand side, in order. If A has children X1 to Xk left to right, then A to X1 ... Xk must be a production. The order matters, which is why the tree is called ordered.
A tree satisfying all four is a derivation tree. A tree whose root is some other variable A rather than S is called a subtree or an A-tree, and it is what the induction in chapter 50's proof works with.
The Derivation Tree
The yield
The yield of a tree is the string obtained by reading its leaves from left to right, with epsilon leaves contributing nothing.
That definition is what connects the tree to the language: the strings of L(G) are exactly the yields of the derivation trees of G. A tree whose yield is w is a proof that w is in the language, and finding such a tree is what parsing means.
Worked: one tree, read three ways
Take the grammar for a to the n followed by b to the n.
S->aSb | εAccepts: ε, ab, aabb, aaabbb
Rejects: a, b, ba, aab, abab
The derivation of aabb, from chapter 23:
S
aSb
aaSbb
aabbThe tree, written with indentation, one vertex per line, children indented under their parent:
S
a
S
a
S
ε
b
bRead it against the four conditions. The root is S. The interior vertices are the three S. The leaves are a, a, epsilon, b, b, all terminals or epsilon. And each S's children spell a right hand side: the outer two spell a, S, b, which is the first production, and the innermost spells epsilon, which is the second.
The yield is read off the leaves left to right: a, a, epsilon, b, b, and the epsilon contributes nothing, so the yield is aabb. Which is the string the derivation produced, as it must be.
Worked: building the tree from a derivation
The procedure, and it is mechanical.
- Write the start symbol as the root.
- Take the derivation's first step. It rewrote some occurrence of some variable; find the vertex for that
occurrence and give it one child per symbol of the right hand side, in order.
- Repeat for each step, always attaching to the vertex for the occurrence that step rewrote.
- When the derivation ends, every leaf is a terminal or epsilon.
Take a grammar with two variables so the occurrences are worth tracking.
S->AB
A->aA | a
B->bB | bAccepts: ab, aab, abb, aabb, aaabbb
Rejects: ε, a, b, ba, bab
A derivation of aabb:
S
AB
aAB
aaB
aabB
aabbThe tree it builds:
S
A
a
A
a
B
b
B
bStep by step: the first step gives S the children A and B. The second rewrites A as aA, so A gets children a and A. The third rewrites the inner A as a, so it gets one child a. The fourth rewrites B as bB. The fifth rewrites the inner B as b.
The yield is a, a, b, b, which is aabb.
The Derivation Tree
Notice what the tree shows and the string does not: that the two a belong to A and the two b belong to B. That structure is the whole reason a compiler wants a tree.
Worked: reading a tree back into a derivation
The reverse, which MU's papers also set.
Take this tree, for the grammar V2 above:
S
A
a
B
b
B
bThe yield is a, b, b, which is abb.
A derivation, obtained by choosing an order in which to expand the interior vertices. Expanding leftmost first:
S
AB
aB
abB
abbAnd there are other derivations of the same tree, differing only in the order. Chapter 43 counts them.
The tree and the expression grammar
The tree is what makes operator precedence visible, so here is the arithmetic grammar of chapter 41 with a tree.
E->E+T | T
T->T*F | F
F->(E) | iAccepts: i, i+i, ii, i+ii, (i+i)*i
Rejects: ε, +, i+, +i, i**i, (i
The tree for i+i*i:
yield i+i*i
E
E
T
F
i
+
T
T
F
i
*
F
iRead the shape rather than the labels. The top of the tree is a plus, with i on its left and the whole of i*i on its right. So the tree says: add i to the product of i and i. It does not say: multiply the sum of i and i by i.
That is precedence, and it is a fact about the shape of the tree, not about the string. The three levels E, T, F are what force it: a plus can only appear at an E vertex, a times only at a T vertex, and a T is below an E, so a plus is always higher in the tree than a times. Chapter 44 shows what happens to a grammar that tries to do without the levels.
Distinctions
| A derivation | A derivation tree | |
|---|---|---|
| Records | the order of the steps | which production was applied where |
| Two of them differ when | the order differs, or the productions do | the productions differ |
| Number for one string | many | one, if the grammar is unambiguous |
| What a parser produces | no | yes |
| Ambiguity is defined on | no | yes |
| An interior vertex | A leaf | |
|---|---|---|
| Label | a variable | a terminal, or epsilon |
| Has children | yes, spelling a production's right hand side | no |
| Contributes to the yield | no | yes, unless it is epsilon |
What it does NOT mean
A tree is not a derivation. It is what many derivations have in common, and the order is gone.
Two derivations of one string do not make a grammar ambiguous. Two trees do. V2 above has several derivations of abb and exactly one tree, and it is unambiguous.
The Derivation Tree
The yield is not the label of the root. It is the leaves, read left to right.
An epsilon leaf is not nothing. It is a leaf, it is the only child of its parent, and it records that a null production was used. Omitting it makes the tree violate the fourth condition, because its parent would then have no children at all.
A terminal never has children. Interior vertices are variables, always, because only a variable can be rewritten.
Quick revision
- A derivation tree has the start symbol at its root, variables at every interior vertex, terminals or epsilon at
every leaf, and each vertex's children spelling a production's right hand side in order.
- The yield is the leaves read left to right, epsilon leaves contributing nothing. The strings of L(G) are exactly
the yields of the derivation trees.
- The tree records which production went where and forgets the order, which is why ambiguity is defined on trees.
- Building the tree from a derivation: attach children to the vertex for the occurrence each step rewrote.
- Reading a derivation from the tree: choose an order for expanding the interior vertices.
- Precedence is a fact about the SHAPE of the tree, forced by the level structure of the grammar.
- A subtree rooted at a variable A is an A-tree, and it is what the induction in the CFG pumping lemma works with.
Test yourself
1. Give the four conditions defining a derivation tree. The root is labelled with the start symbol; every interior vertex is labelled with a variable; every leaf is labelled with a terminal or epsilon; and if a vertex labelled A has children X1 to Xk left to right then A to X1 ... Xk is a production.
2. What is the yield, and what does it have to do with the language? The string of leaf labels read left to right, epsilon leaves contributing nothing. L(G) is exactly the set of yields of derivation trees of G, so a tree with yield w proves w is in the language.
3. For S->aSb | ε, draw the tree for ab. S at the root with three children: a, then S, then b. The inner S has one child, epsilon. The yield is a, epsilon, b, which is ab.
4. Why is ambiguity defined on trees rather than on derivations? Because one tree corresponds to many derivations, which differ only in the order the variables were taken. Counting derivations would make almost every grammar ambiguous; counting trees counts genuinely different structures.
5. Can an interior vertex be labelled with a terminal? Can a leaf be labelled with a variable? No to both. Only a variable can be rewritten, so only a variable can have children; and a finished tree has no variable left to rewrite, so no leaf carries one.
The Derivation Tree
6. In the arithmetic grammar, what makes a plus sit higher in the tree than a times? The level structure. A plus can only appear at an E vertex and a times only at a T vertex, and every T vertex is below some E vertex, so any plus is nearer the root than any times in the same expression. That is what encodes precedence.
The rest of this subject
These notes are cut from the University's printed syllabus. Open the syllabus itself, or the past papers, for the same subject.