 Hello everyone, welcome to the session on 3-adress code generation for Boolean expressions. In this video, we will be seeing that how the translation scheme is provided for the Boolean expression, how the 3-adress codes are generated for the Boolean expression in the compiler. Talking about the learning outcomes of this session, at the end of this session, students will be able to generate this translation scheme for Boolean expressions and you can convert a given Boolean expression into its 3-adress code using the translation scheme. Now, about the Boolean expressions, Boolean expressions can be used by many ways. So, two ways are like, we can use them for computing the logical values like true and false values and another one we can use them in the conditional expressions, either we can use them for if-then-else statements or we can use them for loops also. Now, consider this grammar. What this grammar is? This is the grammar provided for Boolean expressions. The expression is containing the operators or and not, you may have the expression in bracket or the expression may be in the form of id, the relational operator and id. The relational operators may be denoted by less than, less than equal to, greater than, greater than equal to, equal to or not equal to in any form and it may have the values of true and false. So, this is a normal grammar for the relational operator, for the Boolean expressions. Here or and and are having left associative and talking about the precedence of the relation, we know that not is having the highest precedence in the relation and then and operator and then or operator. Now, taken into considering all these one, we will be seeing the next thing that how the Boolean expressions are converted in the three address code. Now let us talk about the methods of translating the Boolean expressions here. Now Boolean expressions, when we are translating them, usually they are in the two forms. The first method is by considering the numerical representation. What is numerical representation? In this one, we are encoding the true and false values in the numerical way like 1 as true and 0 as false value and we are evaluating the Boolean expressions like the arithmetic expressions. The second method is using the flow of control. In this one, we are representing Boolean expression by the position. Like if we are using them, these are usually convenient for if then else statements and loop statements. Here we are using Boolean expressions for moving the control of the statement. Like if the normal flow of the control is there and if we want to change them using the expressions, we are using that. Now consider this numerical representation here. What is there? The example is a or b and not c. So this is the relational expression and see the three address code generated for this one. Now see here that according to the precedence of the operation, not is having the highest precedence, then and is the next precedence and then or. So this expression is executed as first not c is calculated, then and is calculated and then finally or. So this is the result of this one. So this is the three address code which will be generated for this particular example. Like another example here, the simple expression is a less than b. This is taken as if a less than b then take it as true else take it as the value 0 that is a false value in the numerical representation. So this one when we are converting in a three address code for changing the flow of the control, you can see here that this three address code is we are considering the statement which is starting with 100. Now if a is less than b then go to 103 that is what if it is true a less than b is true then it is going to take the value of t as 1 else it is taking the value of t as 0. So once it is taking the value of t as 0 it will it will go to 104 that is it is coming out of this one. So this particular expression is converted in three address code here like this. Now this is the translation scheme for Boolean expressions in the numerical representation form. This is the grammar what we have seen earlier the same grammar is there and these are the semantic actions for that means if this is the matching statement then how the semantic action what action it is taking. Now in this one basically two functions we are using here one is the emit function. What is the emit function? The emit function is used for generating the three address code and new temp that is the function which is generating every time we require the new temporary variable like t1, t2 what earlier we have seen. So for this one we require to generate the temporary variables. So for that now you can see here that if the statement is even or e2 then first we require a temporary variable here. So the attribute e dot place is generating a temporary variable and e dot place value is nothing but a temporary variable whatever it has created then emit function is used. What this emit function is doing? It is generating the three address code and it is keeping it at it is there. So in this e dot place value whatever the place value is there that equal to equal to anything in a single code is taken as it is for the three address code here. So this assignment statement is taken as it is then even dot place or is taken as operator and then e2 is place. Similarly for and also that is a temporary variable is created then even dot place assigns to as it is and and operator is taken for not also and for bracket it is only exchanging the place value. Relational operator in this we require a temporary variable then we require if this it is generating the code if that particular id then the relational operator then id is place then go to now how the it is emitting the spaces now that is go to we have seen earlier 103 104 according to that it is calculating we will see this by the example later and the normal true and false value. If the normal true and false value are there how the code is generating is place value if it is for true then it is 1 and for false it is 0 for the temporary variables. Now as I told earlier that emit function is used for generating the three address code here new temp function is used for generating the temporary variable and the relational operator it is containing next state and it is giving the index of the next three address statements in the given output sequence. Let us see how it is there. Take an example earlier same example if a less than b then 1 l 0. Now this we have seen that the three address code is generated for this. Now according to this translation scheme how this code is generated let us see that one by one. So first statement is what E dot place new temp. So here what it is doing we are considering the next state as 100. So 100 is taken as it is then place value is a new temporary variable here. So it has generated next is what the emit statement. So in this emit statement now see here what it is emitting if as it is then id 1 dot place so that is a relational operator is less than id 2 dot place that is b. Now go to as it is it is using and next state next state is what it is 100 and we are taking 100 plus 3 so that is 1 0 3 it is writing here. So like this this statement is generated by this emit statement. Next the next statement is generated by this emit E dot place assigns to now assigns to 0. So what is E dot place we have taken E dot place earlier as temporary variable t. So t assigns to 0. The next statement is generated by this emit statement. So go to as it is it is writing then the currently what is the current statement next state value that is 1 0 true now 1 0 2 plus 2 that is 1 0 4 so it is writing go to 1 0 4 here. Then this statement is created now in this one what is there it is emitting E dot place it is for true value. So E dot place we have taken as t and t is assigning the value as 1. So like this one it is generating the 3 address code by this one we have seen that how this is generated by this 1 by 1. So totally first the temporary variable is created then this statement is emitted by this then this statement is emitted by this. This statement is generated by this emit statement this statement is generated by this given statement like this a complete 3 address code is generated. Now pause the video and try to convert the given Boolean expression into 3 address code using the above translation scheme. The relation operator the expression what we are using here is p greater than q and r less than s or u greater than v. Now see here we have to consider the precedence of relations also how the precedence of operations are working. So here and is calculating first and then r is calculating. So according to that let us see the answer for that how the 3 address code is generated by this. Now in this one how the 3 address code is generated you can see that first it is generating the 3 address code for p greater than q. So this is the 3 address code for p greater than q. Now this is for r less than s by the AND operator and then this is for u greater than v and based on that these are taken. So first p greater than q is calculated here then r less than s is calculated here the next one and u greater than v is calculated and finally first AND operation is taken place so that is t1 and t2 and t4 or t3. So this is by these one. So like this the 3 address code is generated by this one. Take a look of this. These are my references. Thank you.