SDT for Infix to Postfix Expression
In this class, We discuss SDT for Infix to Postfix Expression.
For Complete YouTube Video: Click Here
The reader should have a prior understanding of syntax-directed translation. Click Here.
First, we understand the need to convert infix expression to postfix expression.
Example:
5 + 6 * 7 * 8
The above expression starts evaluating 6 * 7.
The result of 6 * 7 is multiplied by 8.
The evaluation decision is difficult to consider.
We evaluate the expression According to the priority of the previous and next operator.
The postfix expression evaluation is easy.
567*8*+ is the postfix expression for the infix expression 5 + 6 * 7 * 8.
We move on to postfix expression. The previous two numbers are applied to the operator when we find the operator.
The evaluation is easy in postfix expression.
The below diagram shows the Syntax directed definition for the expression grammar.
E – E + T { print(‘+’)
E – T
T – T * F { print(‘*’)
T – F
F – id { print(id.lexval}
After completing the production E – E + T, we display the operator ‘+’.
Similarly, we write the remaining productions.
The grammar executes in a top-down approach.
The below diagram shows the derivation tree for the expression 5 + 6 * 7 * 8.