Draw syntax tree and DAG for the statement
x=(a+b)*(a+b+c)*(a+b+c+d)
Answers
Answer:
We convert the given arithmetic expression into a postfix expression as-
( a + b ) * ( c – d ) + ( ( e / f ) * ( a + b ) )
ab+ * ( c – d ) + ( ( e / f ) * ( a + b ) )
ab+ * cd- + ( ( e / f ) * ( a + b ) )
ab+ * cd- + ( ef/ * ( a + b ) )
ab+ * cd- + ( ef/ * ab+ )
ab+ * cd- + ef/ab+*
ab+cd-* + ef/ab+*
ab+cd-*ef/ab+*+
Step-02:
We draw a syntax tree for the above postfix expression.
Steps Involved
Start pushing the symbols of the postfix expression into the stack one by one.
When an operand is encountered,
Push it into the stack.
When an operator is encountered
Push it into the stack.
Pop the operator and the two symbols below it from the stack.
Perform the operation on the two operands using the operator you have in hand.
Push the result back into the stack.
Continue in the similar manner and draw the syntax tree simultaneously.