Theory of Computation & Computer Graphics β Introduction to Context Free Language, NEC licence examination syllabus (Nepal Engineering Council).
Derivation Trees
The tree that records the structure of a derivation β and discards the order in which the steps were taken.
π Where this lives: The derivation tree is what a compiler actually builds. When you write a + b * c, the parser produces a tree whose shape determines the order of evaluation, and the code generator walks that tree emitting instructions. If the tree groups the multiplication under the addition, the machine multiplies first β so the tree's shape is the meaning of your program, and a parser that builds the wrong tree produces a program that computes the wrong answer while containing no syntax error at all. Search "abstract syntax tree compiler code generation traversal".
Definition and properties
A DERIVATION TREE (PARSE TREE, SYNTAX TREE) IS A TREE
REPRESENTING THE STRUCTURE OF A DERIVATION IN A CONTEXT-FREE
GRAMMAR.
THE DEFINING CONDITIONS β a tree is a derivation tree for
G = (V, T, P, S) if:
1. THE ROOT IS LABELLED S, the start symbol.
2. EVERY INTERNAL NODE IS LABELLED WITH A VARIABLE from V.
3. EVERY LEAF IS LABELLED WITH A TERMINAL, or with Ξ΅.
4. IF an internal node is labelled A and its children, LEFT TO
RIGHT, are labelled Xβ Xβ β¦ Xβ, THEN
A β XβXββ¦Xβ MUST BE A PRODUCTION OF G.
5. A node labelled Ξ΅ must be the ONLY child of its parent
(corresponding to a production A β Ξ΅).
ββ THE YIELD βββββββββββββββββββββββββββββββββββββββββββββββ
THE YIELD of a derivation tree is the string obtained by
reading its LEAVES LEFT TO RIGHT.
L(G) = { yield of T : T is a derivation tree of G with root
S and all leaves terminal }
A tree whose leaves include variables is a PARTIAL derivation
tree, corresponding to a sentential form rather than a
sentence.
ββ THE CENTRAL PROPERTY ββββββββββββββββββββββββββββββββββββ
A DERIVATION TREE RECORDS **WHICH** PRODUCTIONS WERE USED
AND **WHERE**, BUT NOT THE **ORDER** IN WHICH THEY WERE
APPLIED.
That is precisely what makes the tree useful. Many different
derivations correspond to the same tree, because the choice of
which variable to expand next is an arbitrary scheduling
decision that carries no structural information.
THE PRECISE CORRESPONDENCE, which is a standard exam
statement:
Β· Every derivation tree corresponds to EXACTLY ONE LEFTMOST
derivation.
Β· Every derivation tree corresponds to EXACTLY ONE RIGHTMOST
derivation.
Β· A tree may correspond to MANY derivations in general.
SO LEFTMOST DERIVATIONS AND DERIVATION TREES ARE IN
ONE-TO-ONE CORRESPONDENCE, which is why ambiguity can be
defined equivalently in terms of either.
ββ A WORKED EXAMPLE ββββββββββββββββββββββββββββββββββββββββ
Grammar: E β E + T | T, T β T * F | F, F β ( E ) | id
The derivation tree for id + id * id :
E
/ | \
E + T
| /|\
T T * F
| | |
F F id
| |
id id
YIELD, reading leaves left to right: id + id * id β
THE LEFTMOST DERIVATION recorded by this tree:
E β E + T
β T + T
β F + T
β id + T
β id + T * F
β id + F * F
β id + id * F
β id + id * id
THE RIGHTMOST DERIVATION recorded by the SAME tree:
E β E + T
β E + T * F
β E + T * id
β E + F * id
β E + id * id
β T + id * id
β F + id * id
β id + id * id
EIGHT STEPS EACH, THE SAME EIGHT PRODUCTIONS, APPLIED IN
DIFFERENT ORDERS, PRODUCING THE SAME TREE. That is the
central property made concrete.
AND NOTE THE STRUCTURE: the multiplication sits BELOW the
addition, in a subtree of its own, so any evaluation that
walks the tree bottom-up computes id * id first. THE
GRAMMAR'S LAYERING HAS BECOME THE TREE'S SHAPE.
ββ SUBTREES AND THE RECURSIVE STRUCTURE ββββββββββββββββββββ
A SUBTREE rooted at a node labelled A is itself a derivation
tree for A, and its yield is derivable from A.
THIS IS WHAT MAKES INDUCTION ON DERIVATION TREES WORK, and
it is the standard technique for proving that a grammar
generates the language it is claimed to generate: prove the
claim for the base productions, then assume it for subtrees
and prove it for a node combining them.
IT IS ALSO WHAT THE PUMPING LEMMA FOR CONTEXT-FREE
LANGUAGES exploits β a sufficiently deep tree must repeat a
variable on some root-to-leaf path, and the subtree between
the two occurrences can be duplicated or removed.
From tree to meaning, and the practical use
ββ PARSE TREE VERSUS ABSTRACT SYNTAX TREE ββββββββββββββββββ
A distinction worth knowing, since compilers use both:
PARSE TREE (concrete syntax tree)
Contains EVERY grammar symbol, including punctuation,
parentheses and every intermediate variable. It records the
derivation faithfully and is therefore verbose: parsing
id + id * id produces a tree with E, T and F nodes even
where they add no information.
ABSTRACT SYNTAX TREE (AST)
Keeps only the SEMANTICALLY MEANINGFUL structure. The same
expression becomes:
+
/ \
id *
/ \
id id
The chain E β T β F β id collapses to a single leaf, and
the parentheses vanish because the SHAPE now carries what
they specified.
COMPILERS BUILD A PARSE TREE CONCEPTUALLY AND AN AST
ACTUALLY, because every later phase β type checking,
optimisation, code generation β operates on the AST.
ββ TRAVERSALS AND EVALUATION βββββββββββββββββββββββββββββββ
Walking the tree in different orders serves different
purposes:
INORDER (left, root, right) on an expression tree
reproduces the INFIX notation, and needs parentheses to
be unambiguous.
PREORDER (root, left, right) gives PREFIX / POLISH
notation: + id * id id
POSTORDER (left, right, root) gives POSTFIX / REVERSE
POLISH: id id id * +
POSTFIX NEEDS NO PARENTHESES AND NO PRECEDENCE RULES,
because the tree structure has been linearised β which is
why stack machines and calculators use it, and why
compilers emit code in essentially postorder.
EVALUATION is a postorder traversal: evaluate both children,
then apply the operator.
FOR THE TREE ABOVE, POSTORDER GIVES id id id * + β the
multiplication's operands appear together and are combined
before the addition, which is exactly the required order of
computation. THE TREE HAS DETERMINED THE SEMANTICS.
ββ WHY THE TREE MATTERS MORE THAN THE DERIVATION βββββββββββ
The derivation is a HISTORY of how the string was produced;
the tree is a STRUCTURE describing what the string means.
A COMPILER DOES NOT CARE WHICH ORDER THE PARSER EXPANDED
VARIABLES IN. It cares only about the resulting shape,
because the shape determines evaluation order, scope
nesting, and which operands belong to which operator.
THIS IS WHY AMBIGUITY IS DEFINED IN TERMS OF TREES rather
than derivations: two derivations of the same string are
harmless if they yield the same tree, and are a genuine
problem only when the trees differ β because then the string
has two possible MEANINGS.
ββ CONSTRUCTING A TREE FROM A DERIVATION βββββββββββββββββββ
THE MECHANICAL PROCEDURE, which is the exam task:
1. Start with the root, labelled S.
2. Take the derivation steps in order. Each step expands one
variable A using A β Xββ¦Xβ.
3. Find the node for that occurrence of A and give it
children Xβ β¦ Xβ, left to right.
4. Continue until no variables remain.
5. CHECK: read the leaves left to right and confirm they
give the target string.
THE REVERSE TASK β reading a derivation off a tree β is
equally standard: for the LEFTMOST derivation, repeatedly
expand the leftmost unexpanded variable node; for the
RIGHTMOST, the rightmost.
ββ A COMMON ERROR ββββββββββββββββββββββββββββββββββββββββββ
DRAWING THE CHILDREN IN THE WRONG ORDER. The children of a
node must appear left to right EXACTLY as they do in the
production. A β BC and A β CB are different productions and
give different trees with different yields. Since the yield is
read left to right, reordering the children silently changes
the string the tree generates.
A derivation tree records which productions were used and where, but never the order in which they were applied. That is exactly why compilers care about trees rather than derivations β the order of expansion is arbitrary scheduling, while the shape determines evaluation order and therefore meaning.
π Go further: Postorder traversal is why reverse Polish notation needs no parentheses and no precedence rules. Once a tree is linearised in postorder, the operands of every operator are already adjacent and already evaluated, so a machine needs only a stack: push operands, and on each operator pop two and push the result. This is why HP calculators used RPN, why the Java Virtual Machine and .NET CLR are stack machines, and why "compile an expression" reduces to "traverse the tree in postorder and emit an instruction per node". The parenthesis-free property of postfix and the parenthesis-carrying property of the tree are the same information in two forms. Search "reverse Polish notation stack machine bytecode expression evaluation".
π‘ Exam angle: give the five defining conditions of a derivation tree and define the yield. State the central property β the tree records which productions and where, not the order β and the correspondence that each tree has exactly one leftmost and one rightmost derivation. The standard question is to draw the tree for a given string and then read off both derivations; practise this with the expression grammar. Distinguish the parse tree from the AST. Know the three traversals and that postorder gives postfix, which is also the evaluation order.
Syllabus points
Bottom-up vs top-down approach
Leftmost & rightmost derivation
Language of a grammar
Create a free account to tick topics off, take notes as you read, watch the video lessons and get a day-by-day study plan built around your exam date.
Related topics in Introduction to Context Free Language