{"id":187,"date":"2018-07-20T06:20:38","date_gmt":"2018-07-20T06:20:38","guid":{"rendered":"http:\/\/csp10.epgpbooks.inflibnet.ac.in\/?post_type=chapter&#038;p=187"},"modified":"2018-07-20T06:22:08","modified_gmt":"2018-07-20T06:22:08","slug":"types-of-three-address-code-representations-and-declarations","status":"publish","type":"chapter","link":"https:\/\/ebooks.inflibnet.ac.in\/csp10\/chapter\/types-of-three-address-code-representations-and-declarations\/","title":{"rendered":"Types of Three-address code, Representations and Declarations"},"content":{"raw":"<div>\r\n\r\nIn this module we would discuss the types of three-address code for the various programming constructs. We would also discuss how to store this three-address code information for later retrieval for code generation. As a first step, we would learn to write semantic rules for generating three-address code for declarations\r\n\r\n&nbsp;\r\n\r\n<strong>22.1Types of Three-address code<\/strong>\r\n\r\n&nbsp;\r\n\r\nIn a three-address code there is at most one operator at the right side of an expression. The LHS of an expression will be a temporary variable or a permanent variable. Figure 22.1 are some examples of three-address code for expressions.\r\n\r\n&nbsp;\r\n\r\n\u2022\u00a0\u00a0\u00a0\u00a0\u00a0 t1 = b \u2013 c\r\n\r\n\u2022\u00a0\u00a0\u00a0\u00a0\u00a0 t2 = a * t1\r\n\r\n\u2022\u00a0\u00a0\u00a0\u00a0\u00a0 t3 = a + t2\r\n\r\n\u2022\u00a0\u00a0\u00a0\u00a0\u00a0 t4 = t1 * d\r\n\r\n\u2022\u00a0\u00a0\u00a0\u00a0\u00a0 t5 = t3 + t4\r\n\r\nFigure 22.1 Example of expressions\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">Figure 22.1 has shown examples of three-address code involving expressions and we would like to know how the three-address code would look like for other programming constructs. In order to know this, the following are the various types of three-address code and all programming constructs need to use one or a combination of more than one to generate three-address code.<\/p>\r\n<p style=\"text-align: justify\">\u2022\u00a0\u00a0\u00a0\u00a0\u00a0 x = y op z \u2013 This corresponds to expressions involving binary operators, where x, y, z are variables indicating addresses and \u2018op\u2019, \u2018=\u2019 are the two operators<\/p>\r\n<p style=\"text-align: justify\">\u2022\u00a0\u00a0\u00a0\u00a0\u00a0 x = op y \u2013 This is similar to the previous one. It is also for expressions involving unary operators where x, y corresponds to variables and \u2018op\u2019 represents the unary operator along with the assignment operator.<\/p>\r\n<p style=\"text-align: justify\">\u2022\u00a0\u00a0\u00a0\u00a0\u00a0 x = y \u2013 This is simply an assignment statement involving two variables x, y.<\/p>\r\n<p style=\"text-align: justify\">\u2022\u00a0\u00a0\u00a0\u00a0\u00a0 goto L \u2013 This is an unconditional jump. L is the address of the location to jump.<\/p>\r\n<p style=\"text-align: justify\">\u2022\u00a0\u00a0\u00a0\u00a0\u00a0 if x goto L and if (false x) goto L1 \u2013 This is a conditional jump. The variable \u2018x\u2019 is one address and the location of the conditional jump L is another address. If \u2018x\u2019 is true the sequence jumps to L and if false would jump to another address L1.<\/p>\r\n<p style=\"text-align: justify\">\u2022\u00a0\u00a0\u00a0\u00a0\u00a0 if x relop y goto L \u2013 This is conditional jump involving two addresses, where \u2018x\u2019 is checked to jump to location L if true.<\/p>\r\n<p style=\"text-align: justify\">\u2022\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 Procedure calls using: - Procedure calls are done in sequence by evaluating the parameters and then using the values to call the necessary function which is at an address.<\/p>\r\n\r\n<\/div>\r\n<ul>\r\n \t<li style=\"text-align: justify\">param x<\/li>\r\n \t<li style=\"text-align: justify\">call p,n<\/li>\r\n \t<li style=\"text-align: justify\">y = call p,n<\/li>\r\n \t<li style=\"text-align: justify\">x = y[i] and x[i] = y \u2013 Array assignments involve two addresses and an offset<\/li>\r\n \t<li style=\"text-align: justify\">x = &amp;y \u2013 Address of a variable \u2018y\u2019 is an address, and is assigned to another address location \u2018x\u2019.<\/li>\r\n \t<li style=\"text-align: justify\">x = *y and *x =y \u2013 Assigning address of \u2018y\u2019 to a variable \u2018x\u2019 or assigning the value of \u2018y\u2019 to the dereferenced variable \u2018x\u2019<\/li>\r\n<\/ul>\r\nConsider an example code and let us discuss the possible three-address code.\r\n<ul>\r\n \t<li>do i = i+1; while (a[i] &lt; v);<\/li>\r\n<\/ul>\r\n<p style=\"text-align: justify\">The above statement is a do-while loop. The value of \u2018i' is incremented by one in the body of the loop. The value of an array variable is checked to continue or exit from the loop. The three-address code could be generated either using symbolic labels or using position line numbers and both are given in Table 22.1<\/p>\r\n&nbsp;\r\n\r\n<img class=\"size-full wp-image-188 aligncenter\" src=\"http:\/\/csp10.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/50\/2018\/07\/Untitled-106.png\" alt=\"\" width=\"689\" height=\"438\" \/>\r\n<p style=\"text-align: justify\">As can be seen from table 22.1, all the instructions obey one of the types of three-address codes already mentioned. Now, the later sections of this module and the next module would discuss the work involved in generating this code from the input high level language. After generating the code, it has to be stored. The next section would discuss means of storing the generated 3-address code.<\/p>\r\n&nbsp;\r\n\r\n<strong>22.2Three-address code representation<\/strong>\r\n\r\n&nbsp;\r\n\r\nThree-address code could be represented using quadruples, triples and indirect triples. All the representations use tables to store the information. The following subsection discusses these representations.\r\n\r\n&nbsp;\r\n\r\n22.2.1 Quadruples\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">This has four fields in the table to represent op, arg1, arg2 and result where \u2018op\u2019 refers to the operator, \u2018arg1\u2019 and \u2018arg2\u2019 refers to the operands and the \u2018result\u2019 field stores the result of computing \u2018arg1\u2019 and \u2018arg2\u2019 on the operator \u2018op\u2019. Consider the following example :<\/p>\r\n&nbsp;\r\n\r\na := b * - c + b * - c\r\n\r\n&nbsp;\r\n\r\nThe three-address code corresponding to the above example is given below:\r\n\r\n&nbsp;\r\n\r\nt1 : = -c\r\n\r\nt2 := b * t1\r\n\r\nt3 : = - c\r\n\r\nt4 := b * t3\r\n\r\nt5 := t2 + t4\r\n\r\na\u00a0 := t5\r\n\r\n&nbsp;\r\n\r\nThe generated three-address is stored as a quadruple in Table 22.2\r\n\r\n&nbsp;\r\n\r\nTable 22.2 Quadruple example\r\n\r\n<img class=\"size-full wp-image-189 aligncenter\" src=\"http:\/\/csp10.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/50\/2018\/07\/Untitled-107.png\" alt=\"\" width=\"670\" height=\"702\" \/>\r\n<div>\r\n\r\nThis uses a triple representation but the triples are remembered in another table for quick reference and reuse. We use a list of pointers to triples to remember this. For the example of Table 22.3, the list of pointers is given in Table 22.5.\r\n\r\n&nbsp;\r\n\r\nTable 22.5 Indirect triple to maintain list of Triple pointers\r\n\r\n&nbsp;\r\n<table class=\"aligncenter\" border=\"1\">\r\n<tbody>\r\n<tr>\r\n<td style=\"width: 146.063px\"><\/td>\r\n<td style=\"width: 514.063px\">Triple reference<\/td>\r\n<\/tr>\r\n<tr>\r\n<td style=\"width: 146.063px\">(10)<\/td>\r\n<td style=\"width: 514.063px\">(0)<\/td>\r\n<\/tr>\r\n<tr>\r\n<td style=\"width: 146.063px\">(11)<\/td>\r\n<td style=\"width: 514.063px\">(1)<\/td>\r\n<\/tr>\r\n<tr>\r\n<td style=\"width: 146.063px\">(12)<\/td>\r\n<td style=\"width: 514.063px\">(2)<\/td>\r\n<\/tr>\r\n<tr>\r\n<td style=\"width: 146.063px\">(13)<\/td>\r\n<td style=\"width: 514.063px\">(3)<\/td>\r\n<\/tr>\r\n<tr>\r\n<td style=\"width: 146.063px\">(14)<\/td>\r\n<td style=\"width: 514.063px\">(4)<\/td>\r\n<\/tr>\r\n<tr>\r\n<td style=\"width: 146.063px\">(15)<\/td>\r\n<td style=\"width: 514.063px\">(5)<\/td>\r\n<\/tr>\r\n<\/tbody>\r\n<\/table>\r\n<\/div>\r\n&nbsp;\r\n\r\n<strong>22.3 SDT to Three-address code<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">After discussing how to store a three-address code representation, we would now discuss how to generate three-address code. Three-address code is constructed based on the grammar that is defined for each programming construct. The typical attributes based on which the three-address code is generated are code, place, and value. The code attribute corresponds to the actual code which would reflect in the final assembly code to perform some computation. The place attribute refers to the address to store a variable. The value refers to the result of the computation or the value of the variable or the constant.<\/p>\r\n&nbsp;\r\n\r\nTable 22.6 gives the semantic rules for the Expression grammar to generate three-address code.\r\n\r\n&nbsp;\r\n\r\nTable 22.6 Example semantic rules to generate three-address code for expressions\r\n\r\n&nbsp;\r\n\r\n<img class=\"size-full wp-image-190 aligncenter\" src=\"http:\/\/csp10.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/50\/2018\/07\/Untitled-108.png\" alt=\"\" width=\"384\" height=\"536\" \/>\r\n\r\n&nbsp;\r\n\r\n<img class=\"size-full wp-image-191 aligncenter\" src=\"http:\/\/csp10.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/50\/2018\/07\/Untitled-109.png\" alt=\"\" width=\"997\" height=\"760\" \/>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">which is the address of \u2018c\u2019 and\u2018d\u2019. Using the given semantic rule, we need to create a new temporary variable for E2 and let us call that as t1. The code corresponding to the \u2018*\u201d node is generated using \u2018gen\u2019 as E2.addr \u2018=\u2019 E3.addr \u2018*\u2019 E4.addr and is given below<\/p>\r\n&nbsp;\r\n\r\nt1 = c * d\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">Now, t1 corresponds to the address of E2 and afte r generating code corresponding to E2, we need to generate code corresponding to E1. E1 is a single variable and hence address of E1 is \u2018a\u2019. Using the \u2018gen\u2019 we create a new address corresponding to E as t2 which is the address of the \u2018+\u2019 node. Thus the following code is generated for the \u2018+\u2019 node.<\/p>\r\n&nbsp;\r\n\r\nt2 = b + t1\r\n\r\n&nbsp;\r\n\r\nAfter this we have the code corresponding to E in address t2. Thus we generate a new address for the LHS variable as t3 and we generate the code corresponding to it as\r\n\r\n&nbsp;\r\n\r\nt3 = t2\r\n\r\n&nbsp;\r\n\r\nFinally, for the root node, the code will be as\r\n\r\na : = t3\r\n\r\n&nbsp;\r\n\r\n<strong>22.4 Declarations<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">Declarations can be in a procedure or part of the main program. Declarations of variable, constants are done in any procedure so that the variables could be used as part of the code. The main objective of generating three-address code for variables is to keep track of scope information, in addition to allocating a width to store the variable which is based on the data type. The scope information of a variable is maintained in the symbol table along with ot her information like data type, address, width, value, etc. Computing the address of variables is done by semantic rules related to three-address code. Type, width and offset are three attributes associated with generating three-address code for declarations. Table 22.7 gives the semantic rules corresponding to three-address code generation of declarations.<\/p>\r\n&nbsp;\r\n\r\nTable 22.7 Three-address code for declarations\r\n\r\n<img class=\"size-full wp-image-192 aligncenter\" src=\"http:\/\/csp10.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/50\/2018\/07\/Untitled-110.png\" alt=\"\" width=\"699\" height=\"750\" \/>\r\n<p style=\"text-align: justify\">The initial production is P \u00e0 D\u00a0 For each procedure a new symbol table is created and a beginning of a procedure is indicated using the \u2018proc\u2019 keyword. The production for declarations looks like<\/p>\r\n&nbsp;\r\n\r\nD \u00e0 D ; D | id = T | proc id ; D ; S\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">A new symbol table is created when proc id; D; S is encountered. The procedures in which all symbol tables are linked and kept track of are discussed in the next module along with their semantic rules.<\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify\"><strong>Summary<\/strong>: In this module we discussed the various ways of representing Intermediate code representations like Quadruples, Triples, and Indirect Triples. Their procedure and advantages were discussed. We also discussed an example of semantic rules set to generate 3-address code for expressions and declarations in Pascal language. The subseq uent module would discuss the semantic rules for generating three- address code for other programming constructs.<\/p>","rendered":"<div>\n<p>In this module we would discuss the types of three-address code for the various programming constructs. We would also discuss how to store this three-address code information for later retrieval for code generation. As a first step, we would learn to write semantic rules for generating three-address code for declarations<\/p>\n<p>&nbsp;<\/p>\n<p><strong>22.1Types of Three-address code<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p>In a three-address code there is at most one operator at the right side of an expression. The LHS of an expression will be a temporary variable or a permanent variable. Figure 22.1 are some examples of three-address code for expressions.<\/p>\n<p>&nbsp;<\/p>\n<p>\u2022\u00a0\u00a0\u00a0\u00a0\u00a0 t1 = b \u2013 c<\/p>\n<p>\u2022\u00a0\u00a0\u00a0\u00a0\u00a0 t2 = a * t1<\/p>\n<p>\u2022\u00a0\u00a0\u00a0\u00a0\u00a0 t3 = a + t2<\/p>\n<p>\u2022\u00a0\u00a0\u00a0\u00a0\u00a0 t4 = t1 * d<\/p>\n<p>\u2022\u00a0\u00a0\u00a0\u00a0\u00a0 t5 = t3 + t4<\/p>\n<p>Figure 22.1 Example of expressions<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">Figure 22.1 has shown examples of three-address code involving expressions and we would like to know how the three-address code would look like for other programming constructs. In order to know this, the following are the various types of three-address code and all programming constructs need to use one or a combination of more than one to generate three-address code.<\/p>\n<p style=\"text-align: justify\">\u2022\u00a0\u00a0\u00a0\u00a0\u00a0 x = y op z \u2013 This corresponds to expressions involving binary operators, where x, y, z are variables indicating addresses and \u2018op\u2019, \u2018=\u2019 are the two operators<\/p>\n<p style=\"text-align: justify\">\u2022\u00a0\u00a0\u00a0\u00a0\u00a0 x = op y \u2013 This is similar to the previous one. It is also for expressions involving unary operators where x, y corresponds to variables and \u2018op\u2019 represents the unary operator along with the assignment operator.<\/p>\n<p style=\"text-align: justify\">\u2022\u00a0\u00a0\u00a0\u00a0\u00a0 x = y \u2013 This is simply an assignment statement involving two variables x, y.<\/p>\n<p style=\"text-align: justify\">\u2022\u00a0\u00a0\u00a0\u00a0\u00a0 goto L \u2013 This is an unconditional jump. L is the address of the location to jump.<\/p>\n<p style=\"text-align: justify\">\u2022\u00a0\u00a0\u00a0\u00a0\u00a0 if x goto L and if (false x) goto L1 \u2013 This is a conditional jump. The variable \u2018x\u2019 is one address and the location of the conditional jump L is another address. If \u2018x\u2019 is true the sequence jumps to L and if false would jump to another address L1.<\/p>\n<p style=\"text-align: justify\">\u2022\u00a0\u00a0\u00a0\u00a0\u00a0 if x relop y goto L \u2013 This is conditional jump involving two addresses, where \u2018x\u2019 is checked to jump to location L if true.<\/p>\n<p style=\"text-align: justify\">\u2022\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 Procedure calls using: &#8211; Procedure calls are done in sequence by evaluating the parameters and then using the values to call the necessary function which is at an address.<\/p>\n<\/div>\n<ul>\n<li style=\"text-align: justify\">param x<\/li>\n<li style=\"text-align: justify\">call p,n<\/li>\n<li style=\"text-align: justify\">y = call p,n<\/li>\n<li style=\"text-align: justify\">x = y[i] and x[i] = y \u2013 Array assignments involve two addresses and an offset<\/li>\n<li style=\"text-align: justify\">x = &amp;y \u2013 Address of a variable \u2018y\u2019 is an address, and is assigned to another address location \u2018x\u2019.<\/li>\n<li style=\"text-align: justify\">x = *y and *x =y \u2013 Assigning address of \u2018y\u2019 to a variable \u2018x\u2019 or assigning the value of \u2018y\u2019 to the dereferenced variable \u2018x\u2019<\/li>\n<\/ul>\n<p>Consider an example code and let us discuss the possible three-address code.<\/p>\n<ul>\n<li>do i = i+1; while (a[i] &lt; v);<\/li>\n<\/ul>\n<p style=\"text-align: justify\">The above statement is a do-while loop. The value of \u2018i&#8217; is incremented by one in the body of the loop. The value of an array variable is checked to continue or exit from the loop. The three-address code could be generated either using symbolic labels or using position line numbers and both are given in Table 22.1<\/p>\n<p>&nbsp;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-188 aligncenter\" src=\"http:\/\/csp10.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/50\/2018\/07\/Untitled-106.png\" alt=\"\" width=\"689\" height=\"438\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp10\/wp-content\/uploads\/sites\/50\/2018\/07\/Untitled-106.png 689w, https:\/\/ebooks.inflibnet.ac.in\/csp10\/wp-content\/uploads\/sites\/50\/2018\/07\/Untitled-106-300x191.png 300w, https:\/\/ebooks.inflibnet.ac.in\/csp10\/wp-content\/uploads\/sites\/50\/2018\/07\/Untitled-106-65x41.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp10\/wp-content\/uploads\/sites\/50\/2018\/07\/Untitled-106-225x143.png 225w, https:\/\/ebooks.inflibnet.ac.in\/csp10\/wp-content\/uploads\/sites\/50\/2018\/07\/Untitled-106-350x222.png 350w\" sizes=\"auto, (max-width: 689px) 100vw, 689px\" \/><\/p>\n<p style=\"text-align: justify\">As can be seen from table 22.1, all the instructions obey one of the types of three-address codes already mentioned. Now, the later sections of this module and the next module would discuss the work involved in generating this code from the input high level language. After generating the code, it has to be stored. The next section would discuss means of storing the generated 3-address code.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>22.2Three-address code representation<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p>Three-address code could be represented using quadruples, triples and indirect triples. All the representations use tables to store the information. The following subsection discusses these representations.<\/p>\n<p>&nbsp;<\/p>\n<p>22.2.1 Quadruples<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">This has four fields in the table to represent op, arg1, arg2 and result where \u2018op\u2019 refers to the operator, \u2018arg1\u2019 and \u2018arg2\u2019 refers to the operands and the \u2018result\u2019 field stores the result of computing \u2018arg1\u2019 and \u2018arg2\u2019 on the operator \u2018op\u2019. Consider the following example :<\/p>\n<p>&nbsp;<\/p>\n<p>a := b * &#8211; c + b * &#8211; c<\/p>\n<p>&nbsp;<\/p>\n<p>The three-address code corresponding to the above example is given below:<\/p>\n<p>&nbsp;<\/p>\n<p>t1 : = -c<\/p>\n<p>t2 := b * t1<\/p>\n<p>t3 : = &#8211; c<\/p>\n<p>t4 := b * t3<\/p>\n<p>t5 := t2 + t4<\/p>\n<p>a\u00a0 := t5<\/p>\n<p>&nbsp;<\/p>\n<p>The generated three-address is stored as a quadruple in Table 22.2<\/p>\n<p>&nbsp;<\/p>\n<p>Table 22.2 Quadruple example<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-189 aligncenter\" src=\"http:\/\/csp10.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/50\/2018\/07\/Untitled-107.png\" alt=\"\" width=\"670\" height=\"702\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp10\/wp-content\/uploads\/sites\/50\/2018\/07\/Untitled-107.png 670w, https:\/\/ebooks.inflibnet.ac.in\/csp10\/wp-content\/uploads\/sites\/50\/2018\/07\/Untitled-107-286x300.png 286w, https:\/\/ebooks.inflibnet.ac.in\/csp10\/wp-content\/uploads\/sites\/50\/2018\/07\/Untitled-107-65x68.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp10\/wp-content\/uploads\/sites\/50\/2018\/07\/Untitled-107-225x236.png 225w, https:\/\/ebooks.inflibnet.ac.in\/csp10\/wp-content\/uploads\/sites\/50\/2018\/07\/Untitled-107-350x367.png 350w\" sizes=\"auto, (max-width: 670px) 100vw, 670px\" \/><\/p>\n<div>\n<p>This uses a triple representation but the triples are remembered in another table for quick reference and reuse. We use a list of pointers to triples to remember this. For the example of Table 22.3, the list of pointers is given in Table 22.5.<\/p>\n<p>&nbsp;<\/p>\n<p>Table 22.5 Indirect triple to maintain list of Triple pointers<\/p>\n<p>&nbsp;<\/p>\n<table class=\"aligncenter\">\n<tbody>\n<tr>\n<td style=\"width: 146.063px\"><\/td>\n<td style=\"width: 514.063px\">Triple reference<\/td>\n<\/tr>\n<tr>\n<td style=\"width: 146.063px\">(10)<\/td>\n<td style=\"width: 514.063px\">(0)<\/td>\n<\/tr>\n<tr>\n<td style=\"width: 146.063px\">(11)<\/td>\n<td style=\"width: 514.063px\">(1)<\/td>\n<\/tr>\n<tr>\n<td style=\"width: 146.063px\">(12)<\/td>\n<td style=\"width: 514.063px\">(2)<\/td>\n<\/tr>\n<tr>\n<td style=\"width: 146.063px\">(13)<\/td>\n<td style=\"width: 514.063px\">(3)<\/td>\n<\/tr>\n<tr>\n<td style=\"width: 146.063px\">(14)<\/td>\n<td style=\"width: 514.063px\">(4)<\/td>\n<\/tr>\n<tr>\n<td style=\"width: 146.063px\">(15)<\/td>\n<td style=\"width: 514.063px\">(5)<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<p>&nbsp;<\/p>\n<p><strong>22.3 SDT to Three-address code<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">After discussing how to store a three-address code representation, we would now discuss how to generate three-address code. Three-address code is constructed based on the grammar that is defined for each programming construct. The typical attributes based on which the three-address code is generated are code, place, and value. The code attribute corresponds to the actual code which would reflect in the final assembly code to perform some computation. The place attribute refers to the address to store a variable. The value refers to the result of the computation or the value of the variable or the constant.<\/p>\n<p>&nbsp;<\/p>\n<p>Table 22.6 gives the semantic rules for the Expression grammar to generate three-address code.<\/p>\n<p>&nbsp;<\/p>\n<p>Table 22.6 Example semantic rules to generate three-address code for expressions<\/p>\n<p>&nbsp;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-190 aligncenter\" src=\"http:\/\/csp10.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/50\/2018\/07\/Untitled-108.png\" alt=\"\" width=\"384\" height=\"536\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp10\/wp-content\/uploads\/sites\/50\/2018\/07\/Untitled-108.png 384w, https:\/\/ebooks.inflibnet.ac.in\/csp10\/wp-content\/uploads\/sites\/50\/2018\/07\/Untitled-108-215x300.png 215w, https:\/\/ebooks.inflibnet.ac.in\/csp10\/wp-content\/uploads\/sites\/50\/2018\/07\/Untitled-108-65x91.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp10\/wp-content\/uploads\/sites\/50\/2018\/07\/Untitled-108-225x314.png 225w, https:\/\/ebooks.inflibnet.ac.in\/csp10\/wp-content\/uploads\/sites\/50\/2018\/07\/Untitled-108-350x489.png 350w\" sizes=\"auto, (max-width: 384px) 100vw, 384px\" \/><\/p>\n<p>&nbsp;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-191 aligncenter\" src=\"http:\/\/csp10.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/50\/2018\/07\/Untitled-109.png\" alt=\"\" width=\"997\" height=\"760\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp10\/wp-content\/uploads\/sites\/50\/2018\/07\/Untitled-109.png 997w, https:\/\/ebooks.inflibnet.ac.in\/csp10\/wp-content\/uploads\/sites\/50\/2018\/07\/Untitled-109-300x229.png 300w, https:\/\/ebooks.inflibnet.ac.in\/csp10\/wp-content\/uploads\/sites\/50\/2018\/07\/Untitled-109-768x585.png 768w, https:\/\/ebooks.inflibnet.ac.in\/csp10\/wp-content\/uploads\/sites\/50\/2018\/07\/Untitled-109-65x50.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp10\/wp-content\/uploads\/sites\/50\/2018\/07\/Untitled-109-225x172.png 225w, https:\/\/ebooks.inflibnet.ac.in\/csp10\/wp-content\/uploads\/sites\/50\/2018\/07\/Untitled-109-350x267.png 350w\" sizes=\"auto, (max-width: 997px) 100vw, 997px\" \/><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">which is the address of \u2018c\u2019 and\u2018d\u2019. Using the given semantic rule, we need to create a new temporary variable for E2 and let us call that as t1. The code corresponding to the \u2018*\u201d node is generated using \u2018gen\u2019 as E2.addr \u2018=\u2019 E3.addr \u2018*\u2019 E4.addr and is given below<\/p>\n<p>&nbsp;<\/p>\n<p>t1 = c * d<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">Now, t1 corresponds to the address of E2 and afte r generating code corresponding to E2, we need to generate code corresponding to E1. E1 is a single variable and hence address of E1 is \u2018a\u2019. Using the \u2018gen\u2019 we create a new address corresponding to E as t2 which is the address of the \u2018+\u2019 node. Thus the following code is generated for the \u2018+\u2019 node.<\/p>\n<p>&nbsp;<\/p>\n<p>t2 = b + t1<\/p>\n<p>&nbsp;<\/p>\n<p>After this we have the code corresponding to E in address t2. Thus we generate a new address for the LHS variable as t3 and we generate the code corresponding to it as<\/p>\n<p>&nbsp;<\/p>\n<p>t3 = t2<\/p>\n<p>&nbsp;<\/p>\n<p>Finally, for the root node, the code will be as<\/p>\n<p>a : = t3<\/p>\n<p>&nbsp;<\/p>\n<p><strong>22.4 Declarations<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">Declarations can be in a procedure or part of the main program. Declarations of variable, constants are done in any procedure so that the variables could be used as part of the code. The main objective of generating three-address code for variables is to keep track of scope information, in addition to allocating a width to store the variable which is based on the data type. The scope information of a variable is maintained in the symbol table along with ot her information like data type, address, width, value, etc. Computing the address of variables is done by semantic rules related to three-address code. Type, width and offset are three attributes associated with generating three-address code for declarations. Table 22.7 gives the semantic rules corresponding to three-address code generation of declarations.<\/p>\n<p>&nbsp;<\/p>\n<p>Table 22.7 Three-address code for declarations<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-192 aligncenter\" src=\"http:\/\/csp10.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/50\/2018\/07\/Untitled-110.png\" alt=\"\" width=\"699\" height=\"750\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp10\/wp-content\/uploads\/sites\/50\/2018\/07\/Untitled-110.png 699w, https:\/\/ebooks.inflibnet.ac.in\/csp10\/wp-content\/uploads\/sites\/50\/2018\/07\/Untitled-110-280x300.png 280w, https:\/\/ebooks.inflibnet.ac.in\/csp10\/wp-content\/uploads\/sites\/50\/2018\/07\/Untitled-110-65x70.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp10\/wp-content\/uploads\/sites\/50\/2018\/07\/Untitled-110-225x241.png 225w, https:\/\/ebooks.inflibnet.ac.in\/csp10\/wp-content\/uploads\/sites\/50\/2018\/07\/Untitled-110-350x376.png 350w\" sizes=\"auto, (max-width: 699px) 100vw, 699px\" \/><\/p>\n<p style=\"text-align: justify\">The initial production is P \u00e0 D\u00a0 For each procedure a new symbol table is created and a beginning of a procedure is indicated using the \u2018proc\u2019 keyword. The production for declarations looks like<\/p>\n<p>&nbsp;<\/p>\n<p>D \u00e0 D ; D | id = T | proc id ; D ; S<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">A new symbol table is created when proc id; D; S is encountered. The procedures in which all symbol tables are linked and kept track of are discussed in the next module along with their semantic rules.<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\"><strong>Summary<\/strong>: In this module we discussed the various ways of representing Intermediate code representations like Quadruples, Triples, and Indirect Triples. Their procedure and advantages were discussed. We also discussed an example of semantic rules set to generate 3-address code for expressions and declarations in Pascal language. The subseq uent module would discuss the semantic rules for generating three- address code for other programming constructs.<\/p>\n","protected":false},"author":4,"menu_order":22,"template":"","meta":{"pb_show_title":"on","pb_short_title":"","pb_subtitle":"","pb_authors":["dr-rajeswari-sridhar"],"pb_section_license":""},"chapter-type":[],"contributor":[59],"license":[],"class_list":["post-187","chapter","type-chapter","status-publish","hentry","contributor-dr-rajeswari-sridhar"],"part":3,"_links":{"self":[{"href":"https:\/\/ebooks.inflibnet.ac.in\/csp10\/wp-json\/pressbooks\/v2\/chapters\/187","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ebooks.inflibnet.ac.in\/csp10\/wp-json\/pressbooks\/v2\/chapters"}],"about":[{"href":"https:\/\/ebooks.inflibnet.ac.in\/csp10\/wp-json\/wp\/v2\/types\/chapter"}],"author":[{"embeddable":true,"href":"https:\/\/ebooks.inflibnet.ac.in\/csp10\/wp-json\/wp\/v2\/users\/4"}],"version-history":[{"count":3,"href":"https:\/\/ebooks.inflibnet.ac.in\/csp10\/wp-json\/pressbooks\/v2\/chapters\/187\/revisions"}],"predecessor-version":[{"id":195,"href":"https:\/\/ebooks.inflibnet.ac.in\/csp10\/wp-json\/pressbooks\/v2\/chapters\/187\/revisions\/195"}],"part":[{"href":"https:\/\/ebooks.inflibnet.ac.in\/csp10\/wp-json\/pressbooks\/v2\/parts\/3"}],"metadata":[{"href":"https:\/\/ebooks.inflibnet.ac.in\/csp10\/wp-json\/pressbooks\/v2\/chapters\/187\/metadata\/"}],"wp:attachment":[{"href":"https:\/\/ebooks.inflibnet.ac.in\/csp10\/wp-json\/wp\/v2\/media?parent=187"}],"wp:term":[{"taxonomy":"chapter-type","embeddable":true,"href":"https:\/\/ebooks.inflibnet.ac.in\/csp10\/wp-json\/pressbooks\/v2\/chapter-type?post=187"},{"taxonomy":"contributor","embeddable":true,"href":"https:\/\/ebooks.inflibnet.ac.in\/csp10\/wp-json\/wp\/v2\/contributor?post=187"},{"taxonomy":"license","embeddable":true,"href":"https:\/\/ebooks.inflibnet.ac.in\/csp10\/wp-json\/wp\/v2\/license?post=187"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}