{"id":196,"date":"2018-07-20T06:35:22","date_gmt":"2018-07-20T06:35:22","guid":{"rendered":"http:\/\/csp10.epgpbooks.inflibnet.ac.in\/?post_type=chapter&#038;p=196"},"modified":"2018-07-20T06:35:35","modified_gmt":"2018-07-20T06:35:35","slug":"three-address-code-symbol-table-and-arrays","status":"publish","type":"chapter","link":"https:\/\/ebooks.inflibnet.ac.in\/csp10\/chapter\/three-address-code-symbol-table-and-arrays\/","title":{"rendered":"THREE ADDRESS CODE, SYMBOL TABLE AND ARRAYS"},"content":{"raw":"<div>\r\n<p style=\"text-align: justify\">In this module, we learn how to write semantic rules for declarations by declaring scope information. The use of symbol tables for declaring scope information for symbol tables is also discussed in this module. In this module, we also discuss the grammar for array declarations.<\/p>\r\n&nbsp;\r\n\r\n<strong>23.1 Symbol Table<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">We have discussed the three address code generation for declarations. The attributes, offset and type helps to keep track of the amount of memory and address information necessary for every variable. In addition to the address and memory information, scope details are necessary for the variables. Consider the following production for declarations:<\/p>\r\n&nbsp;\r\n\r\n\u2022\u00a0\u00a0\u00a0\u00a0\u00a0 P \u00e0 D\r\n\r\n\u2022\u00a0\u00a0\u00a0\u00a0\u00a0 D \u00e0 D ; D | id= T | proc id ; D ; S\r\n\r\n&nbsp;\r\n\r\nThe moment the compiler encounters proc id; D;S , a new symbol table is created. The details about the scope contents are achieved by the following functions:\r\n\r\n&nbsp;\r\n\r\n\u00b7\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 mktable (previous) \u2013 This function returns a pointer to a new table that is linked to a previous table in the outer scope. \u201cprevious\u201d is the address of the previous table, and the first table is called with \u201cnull\u201d.\r\n\r\n\u2022\u00a0\u00a0\u00a0\u00a0\u00a0 enter(table, name, type, offset) \u2013 This function is used to create a new entry in table. \u201ctable\u201d refers to the address of the current table, \u201cname\u201d is the variable name, \u201ctype\u201d refers to the data type of the variable and \u201coffset\u201d is the value used to compute the address of the variable. The attributes \u201ctype\u201d and \u201coffset\u201d are the values generated using the three address code for declarations\r\n\r\n\u2022\u00a0\u00a0\u00a0\u00a0\u00a0 addwidth(table, width) \u2013 This function is used to determine the total width of all entries in table\r\n\r\n\u2022\u00a0\u00a0\u00a0\u00a0\u00a0 enterproc(table, name, newtable) \u2013 This function creates a new entry in table for procedure with local scope newtable. Here \u201ctable\u201d refers to the current table, \u201cname\u201d refers to the name of the newtable, \u201cnewtable\u201d refers to the address of the new table\r\n\r\n\u2022\u00a0\u00a0\u00a0\u00a0\u00a0 lookup (table, name) \u2013 This returns a pointer to the entry in the table for name by following linked tables.\r\n\r\n&nbsp;\r\n\r\nLet us consider an example to demonstrate the linking of the various tables using the above discussed functions. Consider a structure \u201cS\u201d to have two integer variables and let \u2018s\u2019 be an instance of this structure.\r\n\r\n<\/div>\r\n&nbsp;\r\n\r\nstruct S\r\n\r\n{ int a; int b;\r\n\r\n} s;\r\n\r\n&nbsp;\r\n\r\nLet us define a function \u201cswap\u201d with two reference integer variables and the body of the function is defined as follows:\r\n\r\nvoid swap(int&amp; a, int&amp; b)\r\n\r\n&nbsp;\r\n\r\n{ int t; t = a; a = b;\r\n\r\nb = t;\r\n\r\n}\r\n\r\nConsider the following main program which has calls the function swap() and another function foo().\r\n\r\nmain()\r\n\r\n{struct S s; swap(a, b);\r\n\r\nfoo();\r\n\r\n}\r\n\r\n&nbsp;\r\n\r\nThe symbol table integrated information is shown in figure 23.1\r\n\r\n&nbsp;\r\n\r\n<img class=\"size-full wp-image-197 aligncenter\" src=\"http:\/\/csp10.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/50\/2018\/07\/Untitled-111.png\" alt=\"\" width=\"586\" height=\"319\" \/>\r\n<p style=\"text-align: justify\">As can be seen from figure 23.1, the initial symbol table is created when the compiler encounters the main() function using the mktable(nil) function. Once the structure variable is declared using the table information for structure, another table is created as mktable(main) and thus there is a link from main table to the table of the structure. The variable\u2018s\u2019 of the structure has its own symbol table to have information about its fields. The \u201centerproc\u201d function helps enter the details about the symbol table of \u2018s\u2019 in the current symbol table. The details about\u2018s\u2019 is entered in the current symbol table using the \u201center\u201d function. The width information about the structure variable is computed using addwidth() and in this example it is computed as \u201c8\u201d and s tored in the header field of the symbol table. Similarly, the function swap will have a total width as \u201c12\u201d and is stored in the table corresponding to the function \u201cswap\u201d. Let us see how the compiler builds the symbol table to remember the scope information. Consider the following function having calls to three functions A, B, C and assume function A in turn calls function D.<\/p>\r\n&nbsp;\r\n\r\nVoid foo( )\r\n\r\n&nbsp;\r\n\r\n{\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 call A()\r\n\r\ncall B()\r\n\r\ncall (C)\r\n\r\n}\r\n\r\nVoid A()\r\n\r\n{\r\n\r\ncall D()\r\n\r\n}\r\n\r\n&nbsp;\r\n\r\nThe calling stack phenomenon is given in figure 23.2 (a) through (d)\r\n\r\n&nbsp;\r\n\r\n<img class=\"size-full wp-image-198 aligncenter\" src=\"http:\/\/csp10.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/50\/2018\/07\/Untitled-112.png\" alt=\"\" width=\"615\" height=\"255\" \/>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">As can be seen from figure 23 (a), when function A is called, it is pushed into the calling stack and in turn as A calls D, the information about D is pushed onto the stack. Once D returns, it is removed from the stack and so is the case with A. When B is called B is pushed onto the stack and a similar scenario happens with function call to C and is given in figures 23 (b) and (c). When C also terminates, it is also removed from the stack. The address of the symbol tables are maintained as a stack. This information is used to refer to the variables of the functions which are available in the top of the stack thus ensuring scope information. Once, a function terminates, the address of this symbol table is removed from this stack.<\/p>\r\n&nbsp;\r\n<ol start=\"23\">\r\n \t<li><strong> 2 Semantic rules for generating Three-address code for scope information<\/strong><\/li>\r\n<\/ol>\r\n<p style=\"text-align: justify\">\u00a0 \u00a0 \u00a0The functions discussed in the previous section are used to generate three-address code for maintaining scope information. Table 23.1 gives the semantic rules for generating three address code for procedures to maintain scope information. The semantic rule makes use of the functions discussed in the previous section. A stack of addresses of the symbol table indicated as \u201ctblptr\u201d is maintained and is useful in accessing the topmost symbol table at any point of time.<\/p>\r\n&nbsp;\r\n\r\nTable 23.1 Semantic rules for maintaining scope information\r\n\r\n&nbsp;\r\n\r\n<img class=\"size-full wp-image-200 aligncenter\" src=\"http:\/\/csp10.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/50\/2018\/07\/Untitled-114.png\" alt=\"\" width=\"596\" height=\"474\" \/>\r\n\r\n&nbsp;\r\n\r\n<img class=\"size-full wp-image-199 aligncenter\" src=\"http:\/\/csp10.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/50\/2018\/07\/Untitled-113.png\" alt=\"\" width=\"586\" height=\"759\" \/>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">The details in Table 23.1 gives the semantic rules for maintaining the scope of symbol tables using two stacks, one of the stack <strong>tblptr<\/strong> is used to keep track of the available symbol table and the other one, <strong>offse<\/strong>t is used to determine the relative addresses of variables in a function. When a new procedure is called a symbol table is created and its pointer pushed to this stack with offset information in another stack. When the function call seizes this symbol table address is popped off the stack.<\/p>\r\n&nbsp;\r\n\r\n<strong>23.3 Semantic rules for Records in Pascal<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">After discussing the semantic rules for generating three-address code to maintain scope information, we will conclude with generating three-address code for records. The semantic rules for generating three-address code for records are given in Table 23.2<\/p>\r\n&nbsp;\r\n\r\n<img class=\"size-full wp-image-201 aligncenter\" src=\"http:\/\/csp10.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/50\/2018\/07\/Untitled-115.png\" alt=\"\" width=\"932\" height=\"947\" \/>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">This uses a function \u201cemit\u201d which is used to generate three address codes to output file. As can be seen from Figure 23.1, the second statement involves generating a three address code, id.place := E.place whereas a sequence of statements doesn\u2019t involve any code.<\/p>\r\n&nbsp;\r\n\r\n<strong>23.5 Three-address code for assignment statements<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">Names in the symbol table are the variables which are referred to addresses. The function Lookup(id.name) identifies variable \u201cid\u201d in symbol table. The function emit is used to emit three address statements to output file. Table 23.3 gives the set of semantic rules for the expression grammar using emit function. This is similar to the one already discussed in the previous module with \u201cgen\u201d function replaced with \u201cemit\u201d function.<\/p>\r\n&nbsp;\r\n\r\n<img class=\"size-full wp-image-202 aligncenter\" src=\"http:\/\/csp10.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/50\/2018\/07\/Untitled-116.png\" alt=\"\" width=\"875\" height=\"482\" \/>\r\n<div>\r\n<p style=\"text-align: justify\">From tables 23.3 and 22.6 of the previous module, it can be seen that if the expression is long, the number of temporary variables involved also increased. To tackle this situation, we keep track of a counter wherein the number of temporary variables could be reused. We modify <em>newtemp<\/em>() to use a \u201cstack\u201d. We keep a counter<em> c <\/em>initialized to 0, and is used to track the number of temporary variables. <em>newtemp<\/em>() increments <em>c<\/em> and returns temporary $<em>c.<\/em> We decrement counter on each use of a $i in a three-address statement.<\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify\">Consider the production <em>E<\/em>1 + <em>E<\/em>2. The correct evaluation is to evaluate <em>E<\/em>1 into t1 and <em>E<\/em>2 into t2 and is shown below.<\/p>\r\n&nbsp;\r\n\r\nt3:= t1 + t2\r\n\r\n<\/div>\r\n<p style=\"text-align: justify\">As t1 is no longer required, we can reuse t1 instead of using new temp t3. This is the fundamental idea. This is explained in the following example.<\/p>\r\n<p style=\"text-align: justify\">Consider the expression x : = a * b + c*d \u2013 e * f. The use and re-use of temporary variables is given in table 23.4<\/p>\r\n&nbsp;\r\n\r\n<img class=\"size-full wp-image-203 aligncenter\" src=\"http:\/\/csp10.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/50\/2018\/07\/Untitled-117.png\" alt=\"\" width=\"906\" height=\"864\" \/>\r\n\r\n<img class=\"size-full wp-image-204 aligncenter\" src=\"http:\/\/csp10.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/50\/2018\/07\/Untitled-118.png\" alt=\"\" width=\"744\" height=\"926\" \/>\r\n\r\n\u2013\u00a0 em = em-1\u00a0 + im\r\n\r\n&nbsp;\r\n\r\n\u2013\u00a0 e1 = i1\r\n\r\n&nbsp;\r\n\r\nThis will be discussed in detail in the next module.\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\"><strong>Summary: <\/strong>This module discussed the three-address code for declarations in the view of maintaining scope information using symbol table and keeping an access of all symbol tables. The grammar for arrays in Pascal has been introduced. The next module will discuss about defining semantic rules for generating three-address code for accessing arrays.<\/p>\r\n&nbsp;\r\n<ul>\r\n \t<li>Grammar for arrays have been discussed<\/li>\r\n<\/ul>\r\n<p style=\"text-align: justify\"><\/p>","rendered":"<div>\n<p style=\"text-align: justify\">In this module, we learn how to write semantic rules for declarations by declaring scope information. The use of symbol tables for declaring scope information for symbol tables is also discussed in this module. In this module, we also discuss the grammar for array declarations.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>23.1 Symbol Table<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">We have discussed the three address code generation for declarations. The attributes, offset and type helps to keep track of the amount of memory and address information necessary for every variable. In addition to the address and memory information, scope details are necessary for the variables. Consider the following production for declarations:<\/p>\n<p>&nbsp;<\/p>\n<p>\u2022\u00a0\u00a0\u00a0\u00a0\u00a0 P \u00e0 D<\/p>\n<p>\u2022\u00a0\u00a0\u00a0\u00a0\u00a0 D \u00e0 D ; D | id= T | proc id ; D ; S<\/p>\n<p>&nbsp;<\/p>\n<p>The moment the compiler encounters proc id; D;S , a new symbol table is created. The details about the scope contents are achieved by the following functions:<\/p>\n<p>&nbsp;<\/p>\n<p>\u00b7\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 mktable (previous) \u2013 This function returns a pointer to a new table that is linked to a previous table in the outer scope. \u201cprevious\u201d is the address of the previous table, and the first table is called with \u201cnull\u201d.<\/p>\n<p>\u2022\u00a0\u00a0\u00a0\u00a0\u00a0 enter(table, name, type, offset) \u2013 This function is used to create a new entry in table. \u201ctable\u201d refers to the address of the current table, \u201cname\u201d is the variable name, \u201ctype\u201d refers to the data type of the variable and \u201coffset\u201d is the value used to compute the address of the variable. The attributes \u201ctype\u201d and \u201coffset\u201d are the values generated using the three address code for declarations<\/p>\n<p>\u2022\u00a0\u00a0\u00a0\u00a0\u00a0 addwidth(table, width) \u2013 This function is used to determine the total width of all entries in table<\/p>\n<p>\u2022\u00a0\u00a0\u00a0\u00a0\u00a0 enterproc(table, name, newtable) \u2013 This function creates a new entry in table for procedure with local scope newtable. Here \u201ctable\u201d refers to the current table, \u201cname\u201d refers to the name of the newtable, \u201cnewtable\u201d refers to the address of the new table<\/p>\n<p>\u2022\u00a0\u00a0\u00a0\u00a0\u00a0 lookup (table, name) \u2013 This returns a pointer to the entry in the table for name by following linked tables.<\/p>\n<p>&nbsp;<\/p>\n<p>Let us consider an example to demonstrate the linking of the various tables using the above discussed functions. Consider a structure \u201cS\u201d to have two integer variables and let \u2018s\u2019 be an instance of this structure.<\/p>\n<\/div>\n<p>&nbsp;<\/p>\n<p>struct S<\/p>\n<p>{ int a; int b;<\/p>\n<p>} s;<\/p>\n<p>&nbsp;<\/p>\n<p>Let us define a function \u201cswap\u201d with two reference integer variables and the body of the function is defined as follows:<\/p>\n<p>void swap(int&amp; a, int&amp; b)<\/p>\n<p>&nbsp;<\/p>\n<p>{ int t; t = a; a = b;<\/p>\n<p>b = t;<\/p>\n<p>}<\/p>\n<p>Consider the following main program which has calls the function swap() and another function foo().<\/p>\n<p>main()<\/p>\n<p>{struct S s; swap(a, b);<\/p>\n<p>foo();<\/p>\n<p>}<\/p>\n<p>&nbsp;<\/p>\n<p>The symbol table integrated information is shown in figure 23.1<\/p>\n<p>&nbsp;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-197 aligncenter\" src=\"http:\/\/csp10.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/50\/2018\/07\/Untitled-111.png\" alt=\"\" width=\"586\" height=\"319\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp10\/wp-content\/uploads\/sites\/50\/2018\/07\/Untitled-111.png 586w, https:\/\/ebooks.inflibnet.ac.in\/csp10\/wp-content\/uploads\/sites\/50\/2018\/07\/Untitled-111-300x163.png 300w, https:\/\/ebooks.inflibnet.ac.in\/csp10\/wp-content\/uploads\/sites\/50\/2018\/07\/Untitled-111-65x35.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp10\/wp-content\/uploads\/sites\/50\/2018\/07\/Untitled-111-225x122.png 225w, https:\/\/ebooks.inflibnet.ac.in\/csp10\/wp-content\/uploads\/sites\/50\/2018\/07\/Untitled-111-350x191.png 350w\" sizes=\"auto, (max-width: 586px) 100vw, 586px\" \/><\/p>\n<p style=\"text-align: justify\">As can be seen from figure 23.1, the initial symbol table is created when the compiler encounters the main() function using the mktable(nil) function. Once the structure variable is declared using the table information for structure, another table is created as mktable(main) and thus there is a link from main table to the table of the structure. The variable\u2018s\u2019 of the structure has its own symbol table to have information about its fields. The \u201centerproc\u201d function helps enter the details about the symbol table of \u2018s\u2019 in the current symbol table. The details about\u2018s\u2019 is entered in the current symbol table using the \u201center\u201d function. The width information about the structure variable is computed using addwidth() and in this example it is computed as \u201c8\u201d and s tored in the header field of the symbol table. Similarly, the function swap will have a total width as \u201c12\u201d and is stored in the table corresponding to the function \u201cswap\u201d. Let us see how the compiler builds the symbol table to remember the scope information. Consider the following function having calls to three functions A, B, C and assume function A in turn calls function D.<\/p>\n<p>&nbsp;<\/p>\n<p>Void foo( )<\/p>\n<p>&nbsp;<\/p>\n<p>{\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 call A()<\/p>\n<p>call B()<\/p>\n<p>call (C)<\/p>\n<p>}<\/p>\n<p>Void A()<\/p>\n<p>{<\/p>\n<p>call D()<\/p>\n<p>}<\/p>\n<p>&nbsp;<\/p>\n<p>The calling stack phenomenon is given in figure 23.2 (a) through (d)<\/p>\n<p>&nbsp;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-198 aligncenter\" src=\"http:\/\/csp10.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/50\/2018\/07\/Untitled-112.png\" alt=\"\" width=\"615\" height=\"255\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp10\/wp-content\/uploads\/sites\/50\/2018\/07\/Untitled-112.png 615w, https:\/\/ebooks.inflibnet.ac.in\/csp10\/wp-content\/uploads\/sites\/50\/2018\/07\/Untitled-112-300x124.png 300w, https:\/\/ebooks.inflibnet.ac.in\/csp10\/wp-content\/uploads\/sites\/50\/2018\/07\/Untitled-112-65x27.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp10\/wp-content\/uploads\/sites\/50\/2018\/07\/Untitled-112-225x93.png 225w, https:\/\/ebooks.inflibnet.ac.in\/csp10\/wp-content\/uploads\/sites\/50\/2018\/07\/Untitled-112-350x145.png 350w\" sizes=\"auto, (max-width: 615px) 100vw, 615px\" \/><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">As can be seen from figure 23 (a), when function A is called, it is pushed into the calling stack and in turn as A calls D, the information about D is pushed onto the stack. Once D returns, it is removed from the stack and so is the case with A. When B is called B is pushed onto the stack and a similar scenario happens with function call to C and is given in figures 23 (b) and (c). When C also terminates, it is also removed from the stack. The address of the symbol tables are maintained as a stack. This information is used to refer to the variables of the functions which are available in the top of the stack thus ensuring scope information. Once, a function terminates, the address of this symbol table is removed from this stack.<\/p>\n<p>&nbsp;<\/p>\n<ol start=\"23\">\n<li><strong> 2 Semantic rules for generating Three-address code for scope information<\/strong><\/li>\n<\/ol>\n<p style=\"text-align: justify\">\u00a0 \u00a0 \u00a0The functions discussed in the previous section are used to generate three-address code for maintaining scope information. Table 23.1 gives the semantic rules for generating three address code for procedures to maintain scope information. The semantic rule makes use of the functions discussed in the previous section. A stack of addresses of the symbol table indicated as \u201ctblptr\u201d is maintained and is useful in accessing the topmost symbol table at any point of time.<\/p>\n<p>&nbsp;<\/p>\n<p>Table 23.1 Semantic rules for maintaining scope information<\/p>\n<p>&nbsp;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-200 aligncenter\" src=\"http:\/\/csp10.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/50\/2018\/07\/Untitled-114.png\" alt=\"\" width=\"596\" height=\"474\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp10\/wp-content\/uploads\/sites\/50\/2018\/07\/Untitled-114.png 596w, https:\/\/ebooks.inflibnet.ac.in\/csp10\/wp-content\/uploads\/sites\/50\/2018\/07\/Untitled-114-300x239.png 300w, https:\/\/ebooks.inflibnet.ac.in\/csp10\/wp-content\/uploads\/sites\/50\/2018\/07\/Untitled-114-65x52.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp10\/wp-content\/uploads\/sites\/50\/2018\/07\/Untitled-114-225x179.png 225w, https:\/\/ebooks.inflibnet.ac.in\/csp10\/wp-content\/uploads\/sites\/50\/2018\/07\/Untitled-114-350x278.png 350w\" sizes=\"auto, (max-width: 596px) 100vw, 596px\" \/><\/p>\n<p>&nbsp;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-199 aligncenter\" src=\"http:\/\/csp10.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/50\/2018\/07\/Untitled-113.png\" alt=\"\" width=\"586\" height=\"759\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp10\/wp-content\/uploads\/sites\/50\/2018\/07\/Untitled-113.png 586w, https:\/\/ebooks.inflibnet.ac.in\/csp10\/wp-content\/uploads\/sites\/50\/2018\/07\/Untitled-113-232x300.png 232w, https:\/\/ebooks.inflibnet.ac.in\/csp10\/wp-content\/uploads\/sites\/50\/2018\/07\/Untitled-113-65x84.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp10\/wp-content\/uploads\/sites\/50\/2018\/07\/Untitled-113-225x291.png 225w, https:\/\/ebooks.inflibnet.ac.in\/csp10\/wp-content\/uploads\/sites\/50\/2018\/07\/Untitled-113-350x453.png 350w\" sizes=\"auto, (max-width: 586px) 100vw, 586px\" \/><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">The details in Table 23.1 gives the semantic rules for maintaining the scope of symbol tables using two stacks, one of the stack <strong>tblptr<\/strong> is used to keep track of the available symbol table and the other one, <strong>offse<\/strong>t is used to determine the relative addresses of variables in a function. When a new procedure is called a symbol table is created and its pointer pushed to this stack with offset information in another stack. When the function call seizes this symbol table address is popped off the stack.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>23.3 Semantic rules for Records in Pascal<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">After discussing the semantic rules for generating three-address code to maintain scope information, we will conclude with generating three-address code for records. The semantic rules for generating three-address code for records are given in Table 23.2<\/p>\n<p>&nbsp;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-201 aligncenter\" src=\"http:\/\/csp10.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/50\/2018\/07\/Untitled-115.png\" alt=\"\" width=\"932\" height=\"947\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp10\/wp-content\/uploads\/sites\/50\/2018\/07\/Untitled-115.png 932w, https:\/\/ebooks.inflibnet.ac.in\/csp10\/wp-content\/uploads\/sites\/50\/2018\/07\/Untitled-115-295x300.png 295w, https:\/\/ebooks.inflibnet.ac.in\/csp10\/wp-content\/uploads\/sites\/50\/2018\/07\/Untitled-115-768x780.png 768w, https:\/\/ebooks.inflibnet.ac.in\/csp10\/wp-content\/uploads\/sites\/50\/2018\/07\/Untitled-115-65x66.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp10\/wp-content\/uploads\/sites\/50\/2018\/07\/Untitled-115-225x229.png 225w, https:\/\/ebooks.inflibnet.ac.in\/csp10\/wp-content\/uploads\/sites\/50\/2018\/07\/Untitled-115-350x356.png 350w\" sizes=\"auto, (max-width: 932px) 100vw, 932px\" \/><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">This uses a function \u201cemit\u201d which is used to generate three address codes to output file. As can be seen from Figure 23.1, the second statement involves generating a three address code, id.place := E.place whereas a sequence of statements doesn\u2019t involve any code.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>23.5 Three-address code for assignment statements<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">Names in the symbol table are the variables which are referred to addresses. The function Lookup(id.name) identifies variable \u201cid\u201d in symbol table. The function emit is used to emit three address statements to output file. Table 23.3 gives the set of semantic rules for the expression grammar using emit function. This is similar to the one already discussed in the previous module with \u201cgen\u201d function replaced with \u201cemit\u201d function.<\/p>\n<p>&nbsp;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-202 aligncenter\" src=\"http:\/\/csp10.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/50\/2018\/07\/Untitled-116.png\" alt=\"\" width=\"875\" height=\"482\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp10\/wp-content\/uploads\/sites\/50\/2018\/07\/Untitled-116.png 875w, https:\/\/ebooks.inflibnet.ac.in\/csp10\/wp-content\/uploads\/sites\/50\/2018\/07\/Untitled-116-300x165.png 300w, https:\/\/ebooks.inflibnet.ac.in\/csp10\/wp-content\/uploads\/sites\/50\/2018\/07\/Untitled-116-768x423.png 768w, https:\/\/ebooks.inflibnet.ac.in\/csp10\/wp-content\/uploads\/sites\/50\/2018\/07\/Untitled-116-65x36.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp10\/wp-content\/uploads\/sites\/50\/2018\/07\/Untitled-116-225x124.png 225w, https:\/\/ebooks.inflibnet.ac.in\/csp10\/wp-content\/uploads\/sites\/50\/2018\/07\/Untitled-116-350x193.png 350w\" sizes=\"auto, (max-width: 875px) 100vw, 875px\" \/><\/p>\n<div>\n<p style=\"text-align: justify\">From tables 23.3 and 22.6 of the previous module, it can be seen that if the expression is long, the number of temporary variables involved also increased. To tackle this situation, we keep track of a counter wherein the number of temporary variables could be reused. We modify <em>newtemp<\/em>() to use a \u201cstack\u201d. We keep a counter<em> c <\/em>initialized to 0, and is used to track the number of temporary variables. <em>newtemp<\/em>() increments <em>c<\/em> and returns temporary $<em>c.<\/em> We decrement counter on each use of a $i in a three-address statement.<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">Consider the production <em>E<\/em>1 + <em>E<\/em>2. The correct evaluation is to evaluate <em>E<\/em>1 into t1 and <em>E<\/em>2 into t2 and is shown below.<\/p>\n<p>&nbsp;<\/p>\n<p>t3:= t1 + t2<\/p>\n<\/div>\n<p style=\"text-align: justify\">As t1 is no longer required, we can reuse t1 instead of using new temp t3. This is the fundamental idea. This is explained in the following example.<\/p>\n<p style=\"text-align: justify\">Consider the expression x : = a * b + c*d \u2013 e * f. The use and re-use of temporary variables is given in table 23.4<\/p>\n<p>&nbsp;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-203 aligncenter\" src=\"http:\/\/csp10.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/50\/2018\/07\/Untitled-117.png\" alt=\"\" width=\"906\" height=\"864\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp10\/wp-content\/uploads\/sites\/50\/2018\/07\/Untitled-117.png 906w, https:\/\/ebooks.inflibnet.ac.in\/csp10\/wp-content\/uploads\/sites\/50\/2018\/07\/Untitled-117-300x286.png 300w, https:\/\/ebooks.inflibnet.ac.in\/csp10\/wp-content\/uploads\/sites\/50\/2018\/07\/Untitled-117-768x732.png 768w, https:\/\/ebooks.inflibnet.ac.in\/csp10\/wp-content\/uploads\/sites\/50\/2018\/07\/Untitled-117-65x62.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp10\/wp-content\/uploads\/sites\/50\/2018\/07\/Untitled-117-225x215.png 225w, https:\/\/ebooks.inflibnet.ac.in\/csp10\/wp-content\/uploads\/sites\/50\/2018\/07\/Untitled-117-350x334.png 350w\" sizes=\"auto, (max-width: 906px) 100vw, 906px\" \/><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-204 aligncenter\" src=\"http:\/\/csp10.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/50\/2018\/07\/Untitled-118.png\" alt=\"\" width=\"744\" height=\"926\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp10\/wp-content\/uploads\/sites\/50\/2018\/07\/Untitled-118.png 744w, https:\/\/ebooks.inflibnet.ac.in\/csp10\/wp-content\/uploads\/sites\/50\/2018\/07\/Untitled-118-241x300.png 241w, https:\/\/ebooks.inflibnet.ac.in\/csp10\/wp-content\/uploads\/sites\/50\/2018\/07\/Untitled-118-65x81.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp10\/wp-content\/uploads\/sites\/50\/2018\/07\/Untitled-118-225x280.png 225w, https:\/\/ebooks.inflibnet.ac.in\/csp10\/wp-content\/uploads\/sites\/50\/2018\/07\/Untitled-118-350x436.png 350w\" sizes=\"auto, (max-width: 744px) 100vw, 744px\" \/><\/p>\n<p>\u2013\u00a0 em = em-1\u00a0 + im<\/p>\n<p>&nbsp;<\/p>\n<p>\u2013\u00a0 e1 = i1<\/p>\n<p>&nbsp;<\/p>\n<p>This will be discussed in detail in the next module.<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\"><strong>Summary: <\/strong>This module discussed the three-address code for declarations in the view of maintaining scope information using symbol table and keeping an access of all symbol tables. The grammar for arrays in Pascal has been introduced. The next module will discuss about defining semantic rules for generating three-address code for accessing arrays.<\/p>\n<p>&nbsp;<\/p>\n<ul>\n<li>Grammar for arrays have been discussed<\/li>\n<\/ul>\n<p style=\"text-align: justify\">\n","protected":false},"author":4,"menu_order":23,"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-196","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\/196","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":1,"href":"https:\/\/ebooks.inflibnet.ac.in\/csp10\/wp-json\/pressbooks\/v2\/chapters\/196\/revisions"}],"predecessor-version":[{"id":205,"href":"https:\/\/ebooks.inflibnet.ac.in\/csp10\/wp-json\/pressbooks\/v2\/chapters\/196\/revisions\/205"}],"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\/196\/metadata\/"}],"wp:attachment":[{"href":"https:\/\/ebooks.inflibnet.ac.in\/csp10\/wp-json\/wp\/v2\/media?parent=196"}],"wp:term":[{"taxonomy":"chapter-type","embeddable":true,"href":"https:\/\/ebooks.inflibnet.ac.in\/csp10\/wp-json\/pressbooks\/v2\/chapter-type?post=196"},{"taxonomy":"contributor","embeddable":true,"href":"https:\/\/ebooks.inflibnet.ac.in\/csp10\/wp-json\/wp\/v2\/contributor?post=196"},{"taxonomy":"license","embeddable":true,"href":"https:\/\/ebooks.inflibnet.ac.in\/csp10\/wp-json\/wp\/v2\/license?post=196"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}