SDT for Concrete to Abstract Syntax Tree
In this class, we discuss SDT for Concrete to Abstract Syntax Tree.
For Complete YouTube Video: Click Here
The reader should have prior knowledge of syntax-directed translation. Click Here.
The example helps the readers for a better understanding of syntax-directed translation.
The below grammar is an expression grammar.
E – E + T
E – T
T – T * F
T – F
F – id
The above grammar will identify expressions.
Example:
The below diagram shows the derivation tree for expression 2 + 5 * 6.
The above derivation tree is called a concrete syntax tree.
We show the program execution in the derivation tree.
The below diagram is the abstract syntax tree for the expression 2 + 5 * 6.
The execution of the expression is understood using an abstract syntax tree.
First, five is multiplied by six, and we add the result with 2.
Syntax Directed Definition:
The below diagram shows the syntax-directed definition for concrete syntax tree to abstract tree.
During the execution of the syntax analysis, we add the code for semantic conditions.
Assume we are using recursive descent parsing.
Each non-terminal will have a function.
F – id will have a function F.
In function F, we need to write the code for the semantic condition defined for F.
We need to create a node to save the lexvalue in the production F.
We place the left and right child values as null values.
We place the address of the node in the attribute F.val.
The production T – F will write the condition, T.val = F.val.
The production E – E + T will create a new node and the nodes from the previous productions as child nodes.
The below diagram shows the final abstract syntax tree.