{"id":214,"date":"2018-07-20T06:46:07","date_gmt":"2018-07-20T06:46:07","guid":{"rendered":"http:\/\/csp10.epgpbooks.inflibnet.ac.in\/?post_type=chapter&#038;p=214"},"modified":"2018-07-20T06:54:21","modified_gmt":"2018-07-20T06:54:21","slug":"control-flow-statements-and-boolean-expressions","status":"publish","type":"chapter","link":"https:\/\/ebooks.inflibnet.ac.in\/csp10\/chapter\/control-flow-statements-and-boolean-expressions\/","title":{"rendered":"Control Flow statements and Boolean Expressions"},"content":{"raw":"<p style=\"text-align: justify\">In this module we learn to generate three address code for control flow statements. We will also try to incorporate short circuit information in Boolean expression\u2019s 3-address code generation. As the flow in control flow\u2019s statements involve Boolean expressions, we will look at the semantic rules for generating three-address code involving Boolean expressions with Control flow.<\/p>\r\n&nbsp;\r\n\r\n<strong>25.1 Control Flow statements<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">We have seen semantic rules that would help generate three-address code for declarations, array accesses, arithmetic expressions, Boolean expressions and to keep track of scope information. Thus, a context free grammar is used to define every programming construct and we write semantic rules based on this context free grammar to generate three-address code.<\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify\">Control flow statements are essential components of programming languages that allows executing code based on a decision. The typically available contro l flow statements are if-then, if- then-else, while-do statement and do \u2013 while statements. The control flow statements have an expression that needs to be evaluated and based on the true or false value of the expression the appropriate branching is taken. The context free grammar for defining control flow statements can be defined as<\/p>\r\n&nbsp;\r\n\r\nS \u00e0 if E then S1 | if E then S1 else S2 | while E do S1 | do S1 while E\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">The LHS symbol \u2018S\u2019 stands for statement. The production defines a statement could be a simple \u201cif Expression then Statement\u201d or \u201cif Expression then statement else alternate statement\u201d, \u201cwhile Expression is true do the statements repeatedly\u201d or do a statement repeatedly while the expression is true. In all these statements \u201cE\u201d corresponds to a Boolean expression or sometimes it could be an assignment statement. Thus to generate three-address code for a control flow statement, the first step is to generate code for the expression. This expression could be a sequence of expressions combined with relational and logical operators. Let us discuss each type of control flow statements and the semantic rules used for generating three-address code in the subsequent sections.<\/p>\r\n&nbsp;\r\n\r\n<strong>25.1.1 Three-address code for if-then statements<\/strong>\r\n\r\n&nbsp;\r\n\r\nThe schematic for generating three-address code for if- then statements is given in figure 25.1. As discussed already the expression E needs to be executed first and hence the code corresponding to E is generated first. The value of the expression is evaluated and if it is found to be \u201ctrue\u201d the statement corresponding to the body of the if- then is executed followed by the statement following the if-then statement. If the expression is false, the body of the if- then is skipped and directly we go to the statement that is following the if-then statement\u2019s body.\r\n\r\n&nbsp;\r\n\r\n<img class=\"size-full wp-image-217 aligncenter\" src=\"http:\/\/csp10.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/50\/2018\/07\/Untitled-123.png\" alt=\"\" width=\"685\" height=\"875\" \/>\r\n\r\n<img class=\"size-full wp-image-218 aligncenter\" src=\"http:\/\/csp10.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/50\/2018\/07\/Untitled-124.png\" alt=\"\" width=\"666\" height=\"834\" \/>\r\n\r\n<img class=\"size-full wp-image-219 aligncenter\" src=\"http:\/\/csp10.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/50\/2018\/07\/Untitled-125.png\" alt=\"\" width=\"641\" height=\"863\" \/>\r\n\r\n<img class=\"size-full wp-image-220 aligncenter\" src=\"http:\/\/csp10.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/50\/2018\/07\/Untitled-126.png\" alt=\"\" width=\"655\" height=\"822\" \/>\r\n<p style=\"text-align: justify\">The sections 25.1.1 to 25.1.4 discussed the various semantic rules for, if- then, if-then-else, while, do-while loops. A \u2018for\u2019 loop construct may be considered as a \u2018while\u2019 construct and this can be used to generate 3-address code<\/p>\r\n&nbsp;\r\n\r\n<strong>25.2 Control flow with Boolean expression<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">In the previous module we discussed about how to generate three-address code for Boolean expressions. We had generated two new variable and one was set to value \u20180\u2019 if the expression is false and other set to \u20181\u2019 if the expression is true. However, we could avoid generating code for the Boolean expression based on the logical operators if they are connected by one. Avoiding generation of code for some expression in a Boolean expression is referred to as short circuit based code generation.<\/p>\r\n&nbsp;\r\n\r\nShort circuit avoids computing the full expression involving logical operators. Consider the example where the expression is given by \u201ca &gt; b\u201d . The corresponding code for this expression would be\r\n\r\n&nbsp;\r\n\r\n100 If a &gt; b goto E.true\r\n\r\n&nbsp;\r\n\r\n101 goto E.false\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">This is different from creating a temporary variable and assigning it 0 \/ 1. In other words if an expression E is given as E1 or E2 where \u2018or\u2019 is a logical operator, then if E1 is true, then this can directly be associated to E.true and we could skip generating code for E2. However, if E1 is false then E2 need to be evaluated. The table 25.5 summarizes the various semantic rules associated by incorporating this short circuit information to generate three-address code.<\/p>\r\n&nbsp;\r\n\r\n<img class=\"size-full wp-image-221 aligncenter\" src=\"http:\/\/csp10.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/50\/2018\/07\/Untitled-127.png\" alt=\"\" width=\"607\" height=\"683\" \/>\r\n\r\n<img class=\"size-full wp-image-222 aligncenter\" src=\"http:\/\/csp10.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/50\/2018\/07\/Untitled-128.png\" alt=\"\" width=\"597\" height=\"432\" \/>\r\n\r\n<img class=\"size-full wp-image-223 aligncenter\" src=\"http:\/\/csp10.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/50\/2018\/07\/Untitled-129.png\" alt=\"\" width=\"630\" height=\"831\" \/>\r\n\r\n<img class=\"size-full wp-image-224 aligncenter\" src=\"http:\/\/csp10.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/50\/2018\/07\/Untitled-130.png\" alt=\"\" width=\"622\" height=\"679\" \/>\r\n\r\n&nbsp;","rendered":"<p style=\"text-align: justify\">In this module we learn to generate three address code for control flow statements. We will also try to incorporate short circuit information in Boolean expression\u2019s 3-address code generation. As the flow in control flow\u2019s statements involve Boolean expressions, we will look at the semantic rules for generating three-address code involving Boolean expressions with Control flow.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>25.1 Control Flow statements<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">We have seen semantic rules that would help generate three-address code for declarations, array accesses, arithmetic expressions, Boolean expressions and to keep track of scope information. Thus, a context free grammar is used to define every programming construct and we write semantic rules based on this context free grammar to generate three-address code.<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">Control flow statements are essential components of programming languages that allows executing code based on a decision. The typically available contro l flow statements are if-then, if- then-else, while-do statement and do \u2013 while statements. The control flow statements have an expression that needs to be evaluated and based on the true or false value of the expression the appropriate branching is taken. The context free grammar for defining control flow statements can be defined as<\/p>\n<p>&nbsp;<\/p>\n<p>S \u00e0 if E then S1 | if E then S1 else S2 | while E do S1 | do S1 while E<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">The LHS symbol \u2018S\u2019 stands for statement. The production defines a statement could be a simple \u201cif Expression then Statement\u201d or \u201cif Expression then statement else alternate statement\u201d, \u201cwhile Expression is true do the statements repeatedly\u201d or do a statement repeatedly while the expression is true. In all these statements \u201cE\u201d corresponds to a Boolean expression or sometimes it could be an assignment statement. Thus to generate three-address code for a control flow statement, the first step is to generate code for the expression. This expression could be a sequence of expressions combined with relational and logical operators. Let us discuss each type of control flow statements and the semantic rules used for generating three-address code in the subsequent sections.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>25.1.1 Three-address code for if-then statements<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p>The schematic for generating three-address code for if- then statements is given in figure 25.1. As discussed already the expression E needs to be executed first and hence the code corresponding to E is generated first. The value of the expression is evaluated and if it is found to be \u201ctrue\u201d the statement corresponding to the body of the if- then is executed followed by the statement following the if-then statement. If the expression is false, the body of the if- then is skipped and directly we go to the statement that is following the if-then statement\u2019s body.<\/p>\n<p>&nbsp;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-217 aligncenter\" src=\"http:\/\/csp10.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/50\/2018\/07\/Untitled-123.png\" alt=\"\" width=\"685\" height=\"875\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp10\/wp-content\/uploads\/sites\/50\/2018\/07\/Untitled-123.png 685w, https:\/\/ebooks.inflibnet.ac.in\/csp10\/wp-content\/uploads\/sites\/50\/2018\/07\/Untitled-123-235x300.png 235w, https:\/\/ebooks.inflibnet.ac.in\/csp10\/wp-content\/uploads\/sites\/50\/2018\/07\/Untitled-123-65x83.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp10\/wp-content\/uploads\/sites\/50\/2018\/07\/Untitled-123-225x287.png 225w, https:\/\/ebooks.inflibnet.ac.in\/csp10\/wp-content\/uploads\/sites\/50\/2018\/07\/Untitled-123-350x447.png 350w\" sizes=\"auto, (max-width: 685px) 100vw, 685px\" \/><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-218 aligncenter\" src=\"http:\/\/csp10.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/50\/2018\/07\/Untitled-124.png\" alt=\"\" width=\"666\" height=\"834\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp10\/wp-content\/uploads\/sites\/50\/2018\/07\/Untitled-124.png 666w, https:\/\/ebooks.inflibnet.ac.in\/csp10\/wp-content\/uploads\/sites\/50\/2018\/07\/Untitled-124-240x300.png 240w, https:\/\/ebooks.inflibnet.ac.in\/csp10\/wp-content\/uploads\/sites\/50\/2018\/07\/Untitled-124-65x81.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp10\/wp-content\/uploads\/sites\/50\/2018\/07\/Untitled-124-225x282.png 225w, https:\/\/ebooks.inflibnet.ac.in\/csp10\/wp-content\/uploads\/sites\/50\/2018\/07\/Untitled-124-350x438.png 350w\" sizes=\"auto, (max-width: 666px) 100vw, 666px\" \/><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-219 aligncenter\" src=\"http:\/\/csp10.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/50\/2018\/07\/Untitled-125.png\" alt=\"\" width=\"641\" height=\"863\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp10\/wp-content\/uploads\/sites\/50\/2018\/07\/Untitled-125.png 641w, https:\/\/ebooks.inflibnet.ac.in\/csp10\/wp-content\/uploads\/sites\/50\/2018\/07\/Untitled-125-223x300.png 223w, https:\/\/ebooks.inflibnet.ac.in\/csp10\/wp-content\/uploads\/sites\/50\/2018\/07\/Untitled-125-65x88.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp10\/wp-content\/uploads\/sites\/50\/2018\/07\/Untitled-125-225x303.png 225w, https:\/\/ebooks.inflibnet.ac.in\/csp10\/wp-content\/uploads\/sites\/50\/2018\/07\/Untitled-125-350x471.png 350w\" sizes=\"auto, (max-width: 641px) 100vw, 641px\" \/><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-220 aligncenter\" src=\"http:\/\/csp10.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/50\/2018\/07\/Untitled-126.png\" alt=\"\" width=\"655\" height=\"822\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp10\/wp-content\/uploads\/sites\/50\/2018\/07\/Untitled-126.png 655w, https:\/\/ebooks.inflibnet.ac.in\/csp10\/wp-content\/uploads\/sites\/50\/2018\/07\/Untitled-126-239x300.png 239w, https:\/\/ebooks.inflibnet.ac.in\/csp10\/wp-content\/uploads\/sites\/50\/2018\/07\/Untitled-126-65x82.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp10\/wp-content\/uploads\/sites\/50\/2018\/07\/Untitled-126-225x282.png 225w, https:\/\/ebooks.inflibnet.ac.in\/csp10\/wp-content\/uploads\/sites\/50\/2018\/07\/Untitled-126-350x439.png 350w\" sizes=\"auto, (max-width: 655px) 100vw, 655px\" \/><\/p>\n<p style=\"text-align: justify\">The sections 25.1.1 to 25.1.4 discussed the various semantic rules for, if- then, if-then-else, while, do-while loops. A \u2018for\u2019 loop construct may be considered as a \u2018while\u2019 construct and this can be used to generate 3-address code<\/p>\n<p>&nbsp;<\/p>\n<p><strong>25.2 Control flow with Boolean expression<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">In the previous module we discussed about how to generate three-address code for Boolean expressions. We had generated two new variable and one was set to value \u20180\u2019 if the expression is false and other set to \u20181\u2019 if the expression is true. However, we could avoid generating code for the Boolean expression based on the logical operators if they are connected by one. Avoiding generation of code for some expression in a Boolean expression is referred to as short circuit based code generation.<\/p>\n<p>&nbsp;<\/p>\n<p>Short circuit avoids computing the full expression involving logical operators. Consider the example where the expression is given by \u201ca &gt; b\u201d . The corresponding code for this expression would be<\/p>\n<p>&nbsp;<\/p>\n<p>100 If a &gt; b goto E.true<\/p>\n<p>&nbsp;<\/p>\n<p>101 goto E.false<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">This is different from creating a temporary variable and assigning it 0 \/ 1. In other words if an expression E is given as E1 or E2 where \u2018or\u2019 is a logical operator, then if E1 is true, then this can directly be associated to E.true and we could skip generating code for E2. However, if E1 is false then E2 need to be evaluated. The table 25.5 summarizes the various semantic rules associated by incorporating this short circuit information to generate three-address code.<\/p>\n<p>&nbsp;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-221 aligncenter\" src=\"http:\/\/csp10.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/50\/2018\/07\/Untitled-127.png\" alt=\"\" width=\"607\" height=\"683\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp10\/wp-content\/uploads\/sites\/50\/2018\/07\/Untitled-127.png 607w, https:\/\/ebooks.inflibnet.ac.in\/csp10\/wp-content\/uploads\/sites\/50\/2018\/07\/Untitled-127-267x300.png 267w, https:\/\/ebooks.inflibnet.ac.in\/csp10\/wp-content\/uploads\/sites\/50\/2018\/07\/Untitled-127-65x73.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp10\/wp-content\/uploads\/sites\/50\/2018\/07\/Untitled-127-225x253.png 225w, https:\/\/ebooks.inflibnet.ac.in\/csp10\/wp-content\/uploads\/sites\/50\/2018\/07\/Untitled-127-350x394.png 350w\" sizes=\"auto, (max-width: 607px) 100vw, 607px\" \/><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-222 aligncenter\" src=\"http:\/\/csp10.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/50\/2018\/07\/Untitled-128.png\" alt=\"\" width=\"597\" height=\"432\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp10\/wp-content\/uploads\/sites\/50\/2018\/07\/Untitled-128.png 597w, https:\/\/ebooks.inflibnet.ac.in\/csp10\/wp-content\/uploads\/sites\/50\/2018\/07\/Untitled-128-300x217.png 300w, https:\/\/ebooks.inflibnet.ac.in\/csp10\/wp-content\/uploads\/sites\/50\/2018\/07\/Untitled-128-65x47.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp10\/wp-content\/uploads\/sites\/50\/2018\/07\/Untitled-128-225x163.png 225w, https:\/\/ebooks.inflibnet.ac.in\/csp10\/wp-content\/uploads\/sites\/50\/2018\/07\/Untitled-128-350x253.png 350w\" sizes=\"auto, (max-width: 597px) 100vw, 597px\" \/><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-223 aligncenter\" src=\"http:\/\/csp10.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/50\/2018\/07\/Untitled-129.png\" alt=\"\" width=\"630\" height=\"831\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp10\/wp-content\/uploads\/sites\/50\/2018\/07\/Untitled-129.png 630w, https:\/\/ebooks.inflibnet.ac.in\/csp10\/wp-content\/uploads\/sites\/50\/2018\/07\/Untitled-129-227x300.png 227w, https:\/\/ebooks.inflibnet.ac.in\/csp10\/wp-content\/uploads\/sites\/50\/2018\/07\/Untitled-129-65x86.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp10\/wp-content\/uploads\/sites\/50\/2018\/07\/Untitled-129-225x297.png 225w, https:\/\/ebooks.inflibnet.ac.in\/csp10\/wp-content\/uploads\/sites\/50\/2018\/07\/Untitled-129-350x462.png 350w\" sizes=\"auto, (max-width: 630px) 100vw, 630px\" \/><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-224 aligncenter\" src=\"http:\/\/csp10.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/50\/2018\/07\/Untitled-130.png\" alt=\"\" width=\"622\" height=\"679\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp10\/wp-content\/uploads\/sites\/50\/2018\/07\/Untitled-130.png 622w, https:\/\/ebooks.inflibnet.ac.in\/csp10\/wp-content\/uploads\/sites\/50\/2018\/07\/Untitled-130-275x300.png 275w, https:\/\/ebooks.inflibnet.ac.in\/csp10\/wp-content\/uploads\/sites\/50\/2018\/07\/Untitled-130-65x71.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp10\/wp-content\/uploads\/sites\/50\/2018\/07\/Untitled-130-225x246.png 225w, https:\/\/ebooks.inflibnet.ac.in\/csp10\/wp-content\/uploads\/sites\/50\/2018\/07\/Untitled-130-350x382.png 350w\" sizes=\"auto, (max-width: 622px) 100vw, 622px\" \/><\/p>\n<p>&nbsp;<\/p>\n","protected":false},"author":4,"menu_order":25,"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-214","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\/214","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\/214\/revisions"}],"predecessor-version":[{"id":225,"href":"https:\/\/ebooks.inflibnet.ac.in\/csp10\/wp-json\/pressbooks\/v2\/chapters\/214\/revisions\/225"}],"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\/214\/metadata\/"}],"wp:attachment":[{"href":"https:\/\/ebooks.inflibnet.ac.in\/csp10\/wp-json\/wp\/v2\/media?parent=214"}],"wp:term":[{"taxonomy":"chapter-type","embeddable":true,"href":"https:\/\/ebooks.inflibnet.ac.in\/csp10\/wp-json\/pressbooks\/v2\/chapter-type?post=214"},{"taxonomy":"contributor","embeddable":true,"href":"https:\/\/ebooks.inflibnet.ac.in\/csp10\/wp-json\/wp\/v2\/contributor?post=214"},{"taxonomy":"license","embeddable":true,"href":"https:\/\/ebooks.inflibnet.ac.in\/csp10\/wp-json\/wp\/v2\/license?post=214"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}