 Welcome to the session on syntax restricted translation and implementation of three address code statements which is in compiler design. Talking about the learning outcomes at the end of this session you will be able to generate the syntax restricted definition for a given program statement and differentiate various types of implementations of three address code statements. There are various implementations of three address code so you will be able to differentiate them at the end of this session. Now talking about syntax restricted definition we know that we are writing a grammar for every language. So for a programming language also we are implementing that using a grammar. So syntax restricted definition it is a generalization of a context free grammar. Consider this particular production that is X producing A so this is associated with a function S the attribute of the production rule is saying that the attribute S value is calculated by a function with various attributes again. So S is the attribute here. You can say an example here that E producing E1 plus T so this is a production so semantic rule for this one says that the value of E is calculated by value of E1 plus value of T. So the function F is nothing but addition of these two which is returning the value as SCF. Now for the syntax restricted translation when we are writing the syntax restricted translation for three address code few things we have to consider. Now for the attributes you can say here that E is the production. So this production is having two attributes one is place another one is code. Place attribute represents the value of that E at runtime and code attribute represents the three address code for that particular E. Similarly for non-terminal ID it is having only a place value so that is lexab here. Now for the syntax restricted translation we are using one function which is called as a new temp. What this function is doing it is always returning the new temporary name for our three address code statement generating of generation of the three address code statement. Consider this but productions these are the two types of productions one is a simple statement producing ID assigns to E and a while loop production another one is a simple arithmetic expression production. Now for this one let us talk about the synthesized attribute here. Now see the attributes for S that is a statement S it may be here or it may be here it is having the three attributes. The code attribute represents the actual three address code for that S and the two attributes begin and after these are the levels which we are using for a loop here. So begin is a level at the beginning of the particular loop and after is created at the end of this. We will see later on the example where these levels are created. Similarly for E the code that is E's code is representing the three address code for E and place representing the actual place value of E. You can see here that the generate function is generating the three address code. How it is generating? E1's place value assigns to as it is operator and whatever the E1's place value is there the temporary variable E1's place value again temporary variable plus as it is as it is written in quote single quote and E2's place value. Now this is the syntax accepted definition for an assignment statement. The assignment statement is with the arithmetic statement also the arithmetic statements are these one. Now ID assigns to E and E is producing various things here. You can consider here that for a particular production S is producing ID assigns to E. How the three address code is generated? The three address code of S is nothing but three address code of E first then it is generating a code. What is the code it is generating? The place value of ID assigns to place value of E. It may be temporary it may be identifier whatever it is according to that it is generating. Consider another statement here another production here E producing E1 plus E2. This E is having now two attributes one is place another one is code. The place attribute of E is nothing but it now we know that here we require a temporary variable. So for this it is creating a new temporary variable. Now another attribute that is code so the three address code is generated how it is generating? First it has to calculate the three address code of E1 then it has to calculate the three address code of E2 and then it has to generate the three address code for E1 plus E2. So that is given here you can see here that E1's three address code that is E1 code which is appended with E2's code is appended this R is nothing but here it is used as an append and finally it is generating a code. How it is generating a code? It is generating a code by calling a generate function. So E's place assigns to E1's place plus E2's place. So it may be temporary it may be identifier whatever accordingly it is taking. Similarly for multiplication for the subtraction here also we require a temporary variable this is for the bracket and finally this is for simple id. Now consider another example here the here it is provided for syntax vector definition for a file statement the production is like this S is producing while E do S1. Now the semantic rule for this one is in this way before seeing this one let us talk about this. Now see this production statement is producing this three address code. What is the three address code here? You can see that it is generating the three address code of E first and then it is checking the condition that is while E so this E condition it is written as if E's place equal to 0 that is false then it has to go out of the loop. So go after S dot after so it is coming out of the loop if this condition is false else what it has to do it has to execute this S code and then it has to go to the begin that is what again it is executing the loop here okay. Now see here that this code is generated by this particular semantic rule. Now how it has generated you can see how many levels we require we require two more levels that is one is begin another one is after so that is what S dot begin is equal to new level S dot after is equal to new level so two levels are generated. Next S1's code now the three address code for this one how we are generating this complete code we have to generate. So generate S dot begin so this one and then E's code is appended so this is appended then generate if E dot place now see here that everything which is written in quotation single code that is written as it is so if as it is written E dot place so whatever the place value it is taking so that it is writing here equal to is in single code zero is in single code so as it is it is writing and again then S dot after then S's code is appended then go to S dot begin this is appended and then this is appended so based on this semantic rule this code is generated. So we call this as a syntax recreate definition for a while statement. Now pause this video and try to convert it into three address code now see the statement given is I assigns to two multiplied by n plus k and while I do I assigns to I minus k see how it is generated you can see that the first four things are nothing but three address code for this two so it is assigning to value two into temporary variable two multiplied by n so that T1 multiplied by n this is storing in T2 and then T2 plus k it is doing and storing in T3 and finally T3's value is storing so this statement is generating four three address code statements similarly while I do now this is earlier we have seen according to that it is generating so level one is created and level two are creating as now that new level function is creating these levels so you can see that this is the three address code generated by this one. Now talking about the implementation of three address statements now the three address statements are implemented by three types one is the quadruple the second one is a triple and the third one is the indirect triple. Let us talk about quadruple the name itself is giving here the information that quadruple is having four things what are those one the first thing is operator the second one is argument one the third one is argument two and finally it is showing the result so it is having four things therefore we call it as a quadruple you can see that this assignments this statement a assigns to b star minus c plus b star minus c is created the quadruple representation of this one is like this now see here that this minus c as it is having a unary operation it is not having an argument two so it is not it is blank and it is stored in T1 similarly the next operation that is b star minus c so star is an operator b and then T1 the T1 is nothing but this minus c so it is taken here so like this this quadruple is representing you can see how many temporaries are required five so that becomes a disadvantage of this what we are saying lot of memories are required for this quadruple but the advantage of this one is what is this easy to rearrange why it is easy to rearrange the code because we know that the temporaries are stored like this so any we can rearrange this thing this is not there in the triples let us talk about the triples here what is triple again it is having only three things what are those one one is operator and the remaining two are the operands so operator operand one and operand two represented as argument one and argument two so here the temporary that is a result is not taken as an as an extra field here so that is represented by these only so implicitly the temporaries are given here so the same thing we have implicitly represented by this so that will become the advantage advantage is what temporaries are implicit but what is the disadvantage here it is difficult to rearrange why because we are not storing the result here the third format is the indirect triples what is there in indirect triples it is the addition of these two that is triple and quadruple combination what you can say it is having two things one is a program another one is a triple container so triple container is having the indirect addressing here and these addresses are stored in a program so whatever the sequencing is there if the sequence we are changing or the statement we are changing accordingly it is changing so what will happen temporaries are also implicit and it is easy to rearrange the code so the same statement the same implementation we have seen that we have done by triples quadruples and indirect triples this is all about the syntactic definition and the implementation of three address code statements these are my references thank you