A directed
acyclic graph (DAG) for an expression identifies the common sub
expressions (sub expressions that occur more than once) of the expression.
DAG's can be constructed by using the same techniques that construct syntax
trees.
A DAG has leaves
corresponding to atomic operands and interior nodes corresponding to operators.
A node N in a DAG has more than one parent if N represents a common sub expression,
so a DAG represents expressions concisely. It gives clues to compiler about the
generating efficient code to evaluate expressions.
Example 1: Given
the grammar below, for the input string id + id * id , the parse tree, syntax
tree and the DAG are as shown.
Example 2: DAG for the expression
a + a * (b - c) + ( b - c ) * d is shown below.
0 comments