 Hello everyone, welcome to the session on syntax regeneration and tree address code. These are the part of the compiler generation things which are used for intermediate code generation. In the earlier video, we have seen the types of intermediate code generation. So out of that, we will see now syntax regeneration and the tree address code. The learning outcomes of this session are you will be able to construct the syntax tree using a given data structure will be going to see that how the data structure is implemented and all. Then you will be able to write the tree address code statements for a given statement using the various types of tree address code. Consider the syntax tree. This statement is given here A assigns to B multiplied by minus C plus B multiplied by minus C and the syntax tree generated for this one. Now in this one, usually for a syntax tree always the leaf nodes are representing the operands and the internal nodes, intermediate nodes are representing the operation. The operation with one or two operands. If it is having a binary operation, two operands then the left and the right nodes are representing the operand 1 and operand 2 respectively or else if it is a unary operation then a single node is representing its operand. You can see here unary minus is representing only one operand and the multiplication operator is representing the two operands here. Now what are the rules for syntax tree generation? Usually the productions the grammar rules are given like this here. If a particular statement or a production is providing that S start symbol is providing ID or ID assigns to E then it is implemented by the semantic rule like S point. Now the pointer which is equal to make node it is calling a function here make node which is creating the node of it. And what kind of node it is creating? It is creating a node of assign where the left side is the leaf node and right side is again a pointer. So you can see here that this is the statement ID assigns to E, so for ID it is generating the leaf node and for E it is just writing the pointer of it. Similarly E assigns to E1 plus E2 for this one so the left side is a pointer. That pointer is equal to it is generating the nodes of it and now there are two nodes one is the operator is plus operator and left node is E1 and right node is E2. So accordingly the pointers are assigned. Similarly for multiplication now for unary minus unary 1 minus E1 it is creating the node for it and that node it is having the U minus. So the last one that is E producing ID now in this case it is just generating the identifier so that is what identifier is nothing but the leaf node so it is generating the leaf node for this one. Let us see properly. So consider the first statement here the production rule and the semantic rule that S producing ID assigns to E in this case now it is calling a make node function where assign is the node so here assignment is the node which is generated at the left side it has generated it has called make leaf so for ID it has created the leaf node and E as it is pointer so it is again a node it is not a leaf node. Left node in this case is created by this make leaf. Similarly for this statement E1 plus E2 now here no leaf nodes are there these are already the internal nodes so the nodes are already created like E1 and E2 only the node plus is created and the pointer of E1 and pointer of E2 are assigned at left and right nodes that is provided by this particular function. Similarly for multiplication so a node is created for multiplication and the pointer of left node E1 is assigned here and the right node E2 is assigned here which is already created it may be a leaf node it may be an internal node. Now talking about the 3 address code what do you mean by 3 address code usually all of us are knowing that 3 address code is one of the form of the intermediate code as it is one of the form of the intermediate code it is near to the you can say a system or a machine so therefore it is close to the assembly language and it is used for generating the machine code so it makes easier for the code generator to generate the machine code if we are using the 3 address code statements. So the format of 3 address code statement is X assigns to Y operator and another operand so here now this is the assignment statement you can say where whatever the result is stored and the two operands are there and the operator is specified in the middle that is the 3 address code. Let us see here some example the example statement is provided like X plus Y multiplied by Z so in this case the 3 address code generated for this is according to the precedence of operations here the first operation it is executing is Y multiplied by Z so for this one it is taking a temporary value here so temporary 1 temporary variable 1 is equal to assigns to Y multiplied by Z that is the first operation which it is evaluating similarly the next operation that is the temporary 2 is created for a complete whole expression where X plus the whatever this expression result is there which is stored in T1 that is used here so this is the 3 address code for the statement X plus Y multiplied by Z it is using the two temporaries T1 and T2 now see here for whenever we are taking the syntax tree here how it is associating the temporary variables here you can see the syntax tree that is X plus this is multiplied by Y and Z the same statement X plus Y multiplied by Z the syntax tree for this is generated and how the temporary values are taken temporary variables are taken usually the temporary variables are created by every internal node of a tree so this is the internal node here so that is for multiplication operation temporary 1 is created and for plus operation temporary 2 is created in this one you can see that this syntax tree is converted with this statement so you may say that this tree part of the tree node is created as one 3 address code and another part is created and another 3 address code. Let us see here few examples that is A assigns to minus C one of the statement here which is provided as minus C multiplied by B plus minus C multiplied by D so in this case what will happen is first it will take the unary operation then the multiplication of it with this one and then it goes on so according to the precedence of operation according to the tree generation you can say this is the 3 address code statement which it is generating and you can see here that it is using 5 temporary variables for this particular statement so finally you can see that the 6 3 address code statements are generated for this particular statement. Now in another example here we are providing with the bracket implementations X plus Y multiplied by Y plus Z plus X plus Y plus Z now in this case again the 5 temporary variables are used so X plus Y Y plus Z now this one is converted so usually what will happen is in the 3 address coding sample generation the statement is converted into the postfix notation and that postfix notation is used here for the rest of the things. So this is the 3 address code generation you can see here properly now similarly consider a particular programming statement that is for i equal to 1 i less than 10 that is a general problem statement programming statement this one so how this is converted as a 3 address code example here it is given you can just see here because later on when we will see the types of 3 address code statements you will understand better what it is doing in this one in this case it is usually assigning the i equal to 1 the initial 1 and then it is taking this whatever this particular operation is there so i equal to 1 after that this one as X multiplied by Y so that is taken as a temporary one and again that address code so this final thing this whatever this X multiplied by 5 address of A and size of this is there this is totally the calculation of A of i so that one it has taken and then again it moves on for further statements. So this is the 3 address code statement which is generated for this particular for statement. Now pause the video and write the 3 address code for the following expression the expression is a simple arithmetic expression you can pause the video here and you can write by your own the 3 address code statements let us see how many 3 address code statements are generated for this now see in this one this is the solution for this and this many total 7 temporary variables are used for this one so this is a solution for that particular statement. Now let us talk about the types of 3 address statements how many types are there we will see one by one so the first type is assignment type one where it is a binary operation X assigns to Y operator Z now in this case XY and Z are the addresses assignment operator 2 now in this case it is a unary operation so only one operator and operand is used this is a copy instruction simple X assigns to Y next comes unconditional statement go to L it is just a simple unconditional statement so go to and the label is used here. If it is there then if is the keyword used then X relational operator Y and go to the label is and what is the relational operator the relational operator may be less than equal to greater than equal to or whatever the relational operators we are using but finally this is the 3 address code statement for it. For parameter calling we will see that parameter X and call p, n what is p these are the parameters and n is the number of the parameters so if this is the case it is for the function call in this case it is converted like this all the parameters are declared here and then it is calling. Next comes index assignment in this one it is the X assigns to Y of I or X of I assigns to Y so this is a simple indexing and address and pointer assignments these are the 3 address code statements simply address operator, pointer operator and value assigning the value to it. These are the references thank you.