{"id":112,"date":"2018-07-19T12:10:26","date_gmt":"2018-07-19T12:10:26","guid":{"rendered":"http:\/\/csp10.epgpbooks.inflibnet.ac.in\/?post_type=chapter&#038;p=112"},"modified":"2018-07-19T12:28:00","modified_gmt":"2018-07-19T12:28:00","slug":"semantic-phase-dependency-graph-topological-sorting","status":"publish","type":"chapter","link":"https:\/\/ebooks.inflibnet.ac.in\/csp10\/chapter\/semantic-phase-dependency-graph-topological-sorting\/","title":{"rendered":"Semantic Phase, Dependency Graph, Topological sorting"},"content":{"raw":"In this module we discuss the semantic phase of the compiler and understand the changes that happen in the semantic phase of the compiler.\r\n\r\n&nbsp;\r\n\r\n<strong>20.1 Functions of the semantic phase of the compiler<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">The syntax phase of the compiler converted the input into a derivation tree and validated whether the input belongs to the grammar or not. Thus the output of the parser will be a derivation tree. This derivation tree needs to have information to generate code. The information is derived from the grammar of the programming language. The necessary information needs to be added to the derivation tree and this derivation tree is converted to a representation which is easier to generate assembly language code. Syntax Directed Definition (SDD) and Syntax Directed Translation (SDT) helps in converting the parse tree to an annotated parse tree which has information to generate code as well as the order of evaluating the derivation tree.<\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify\">Thus SDD and SDT are the primary goals of the semantic phase of the compiler. After converting the derivation tree to a parse tree, the semantic phase of the compiler helps in writing semantic rules to verify the semantic correctness of all the statements. Type checking, flow checking are some of the semantic correctness verifying information. Figure 20.1 shows the integration of the lexical and syntactic phase of the compiler with the semantic phase<\/p>\r\n<img class=\"size-full wp-image-115 aligncenter\" src=\"http:\/\/csp10.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/50\/2018\/07\/Untitled-58.png\" alt=\"\" width=\"667\" height=\"230\" \/>\r\n<div>\r\n\r\nAs can be seen from figure 20.1, the YACC specification file could incorporate the semantic rules associated to perform SDD and SDT. Let us discuss the features of SDD and SDT in detail in this module.\r\n\r\n&nbsp;\r\n\r\n<strong>20.2 Syntax Directed Definition<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">A syntax-directed definition adds set of semantic rules to productions. Terminals and non-terminals have attributes. A depth-first traversal algorithm is used to compute the values of the attributes in the parse tree using the semantic rules. After the traversal is completed, the attributes contain the translated form of the input.\u00a0\u00a0<span style=\"font-size: 1em\">For each production semantic rules are formulated to convert the derivation tree to another representation. The semantic rules for the expression grammar is given in Table 20.1 As an expression evaluates to a value, every grammar symbol is associated with the value and this is described in the semantic rule of the expression grammar<\/span><\/p>\r\n\r\n<\/div>\r\n<img class=\"size-full wp-image-117 aligncenter\" src=\"http:\/\/csp10.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/50\/2018\/07\/Untitled-59.png\" alt=\"\" width=\"711\" height=\"693\" \/>\r\n\r\n<img class=\"size-full wp-image-118 aligncenter\" src=\"http:\/\/csp10.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/50\/2018\/07\/Untitled-60.png\" alt=\"\" width=\"730\" height=\"841\" \/>\r\n\r\n<img class=\"size-full wp-image-119 aligncenter\" src=\"http:\/\/csp10.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/50\/2018\/07\/Untitled-61.png\" alt=\"\" width=\"664\" height=\"877\" \/>\r\n\r\n<img class=\"size-full wp-image-120 aligncenter\" src=\"http:\/\/csp10.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/50\/2018\/07\/Untitled-62.png\" alt=\"\" width=\"690\" height=\"923\" \/>\r\n\r\n<img class=\"size-full wp-image-121 aligncenter\" src=\"http:\/\/csp10.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/50\/2018\/07\/Untitled-63.png\" alt=\"\" width=\"674\" height=\"881\" \/>\r\n\r\n<img class=\"size-full wp-image-122 aligncenter\" src=\"http:\/\/csp10.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/50\/2018\/07\/Untitled-64.png\" alt=\"\" width=\"716\" height=\"883\" \/>\r\n\r\n<img class=\"size-full wp-image-123 aligncenter\" src=\"http:\/\/csp10.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/50\/2018\/07\/Untitled-65.png\" alt=\"\" width=\"691\" height=\"873\" \/>\r\n\r\n<img class=\"size-full wp-image-124 aligncenter\" src=\"http:\/\/csp10.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/50\/2018\/07\/Untitled-66.png\" alt=\"\" width=\"677\" height=\"885\" \/>\r\n\r\n<img class=\"size-full wp-image-125 aligncenter\" src=\"http:\/\/csp10.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/50\/2018\/07\/Untitled-67.png\" alt=\"\" width=\"683\" height=\"836\" \/>","rendered":"<p>In this module we discuss the semantic phase of the compiler and understand the changes that happen in the semantic phase of the compiler.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>20.1 Functions of the semantic phase of the compiler<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">The syntax phase of the compiler converted the input into a derivation tree and validated whether the input belongs to the grammar or not. Thus the output of the parser will be a derivation tree. This derivation tree needs to have information to generate code. The information is derived from the grammar of the programming language. The necessary information needs to be added to the derivation tree and this derivation tree is converted to a representation which is easier to generate assembly language code. Syntax Directed Definition (SDD) and Syntax Directed Translation (SDT) helps in converting the parse tree to an annotated parse tree which has information to generate code as well as the order of evaluating the derivation tree.<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">Thus SDD and SDT are the primary goals of the semantic phase of the compiler. After converting the derivation tree to a parse tree, the semantic phase of the compiler helps in writing semantic rules to verify the semantic correctness of all the statements. Type checking, flow checking are some of the semantic correctness verifying information. Figure 20.1 shows the integration of the lexical and syntactic phase of the compiler with the semantic phase<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-115 aligncenter\" src=\"http:\/\/csp10.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/50\/2018\/07\/Untitled-58.png\" alt=\"\" width=\"667\" height=\"230\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp10\/wp-content\/uploads\/sites\/50\/2018\/07\/Untitled-58.png 667w, https:\/\/ebooks.inflibnet.ac.in\/csp10\/wp-content\/uploads\/sites\/50\/2018\/07\/Untitled-58-300x103.png 300w, https:\/\/ebooks.inflibnet.ac.in\/csp10\/wp-content\/uploads\/sites\/50\/2018\/07\/Untitled-58-65x22.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp10\/wp-content\/uploads\/sites\/50\/2018\/07\/Untitled-58-225x78.png 225w, https:\/\/ebooks.inflibnet.ac.in\/csp10\/wp-content\/uploads\/sites\/50\/2018\/07\/Untitled-58-350x121.png 350w\" sizes=\"auto, (max-width: 667px) 100vw, 667px\" \/><\/p>\n<div>\n<p>As can be seen from figure 20.1, the YACC specification file could incorporate the semantic rules associated to perform SDD and SDT. Let us discuss the features of SDD and SDT in detail in this module.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>20.2 Syntax Directed Definition<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">A syntax-directed definition adds set of semantic rules to productions. Terminals and non-terminals have attributes. A depth-first traversal algorithm is used to compute the values of the attributes in the parse tree using the semantic rules. After the traversal is completed, the attributes contain the translated form of the input.\u00a0\u00a0<span style=\"font-size: 1em\">For each production semantic rules are formulated to convert the derivation tree to another representation. The semantic rules for the expression grammar is given in Table 20.1 As an expression evaluates to a value, every grammar symbol is associated with the value and this is described in the semantic rule of the expression grammar<\/span><\/p>\n<\/div>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-117 aligncenter\" src=\"http:\/\/csp10.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/50\/2018\/07\/Untitled-59.png\" alt=\"\" width=\"711\" height=\"693\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp10\/wp-content\/uploads\/sites\/50\/2018\/07\/Untitled-59.png 711w, https:\/\/ebooks.inflibnet.ac.in\/csp10\/wp-content\/uploads\/sites\/50\/2018\/07\/Untitled-59-300x292.png 300w, https:\/\/ebooks.inflibnet.ac.in\/csp10\/wp-content\/uploads\/sites\/50\/2018\/07\/Untitled-59-65x63.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp10\/wp-content\/uploads\/sites\/50\/2018\/07\/Untitled-59-225x219.png 225w, https:\/\/ebooks.inflibnet.ac.in\/csp10\/wp-content\/uploads\/sites\/50\/2018\/07\/Untitled-59-350x341.png 350w\" sizes=\"auto, (max-width: 711px) 100vw, 711px\" \/><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-118 aligncenter\" src=\"http:\/\/csp10.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/50\/2018\/07\/Untitled-60.png\" alt=\"\" width=\"730\" height=\"841\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp10\/wp-content\/uploads\/sites\/50\/2018\/07\/Untitled-60.png 730w, https:\/\/ebooks.inflibnet.ac.in\/csp10\/wp-content\/uploads\/sites\/50\/2018\/07\/Untitled-60-260x300.png 260w, https:\/\/ebooks.inflibnet.ac.in\/csp10\/wp-content\/uploads\/sites\/50\/2018\/07\/Untitled-60-65x75.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp10\/wp-content\/uploads\/sites\/50\/2018\/07\/Untitled-60-225x259.png 225w, https:\/\/ebooks.inflibnet.ac.in\/csp10\/wp-content\/uploads\/sites\/50\/2018\/07\/Untitled-60-350x403.png 350w\" sizes=\"auto, (max-width: 730px) 100vw, 730px\" \/><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-119 aligncenter\" src=\"http:\/\/csp10.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/50\/2018\/07\/Untitled-61.png\" alt=\"\" width=\"664\" height=\"877\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp10\/wp-content\/uploads\/sites\/50\/2018\/07\/Untitled-61.png 664w, https:\/\/ebooks.inflibnet.ac.in\/csp10\/wp-content\/uploads\/sites\/50\/2018\/07\/Untitled-61-227x300.png 227w, https:\/\/ebooks.inflibnet.ac.in\/csp10\/wp-content\/uploads\/sites\/50\/2018\/07\/Untitled-61-65x86.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp10\/wp-content\/uploads\/sites\/50\/2018\/07\/Untitled-61-225x297.png 225w, https:\/\/ebooks.inflibnet.ac.in\/csp10\/wp-content\/uploads\/sites\/50\/2018\/07\/Untitled-61-350x462.png 350w\" sizes=\"auto, (max-width: 664px) 100vw, 664px\" \/><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-120 aligncenter\" src=\"http:\/\/csp10.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/50\/2018\/07\/Untitled-62.png\" alt=\"\" width=\"690\" height=\"923\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp10\/wp-content\/uploads\/sites\/50\/2018\/07\/Untitled-62.png 690w, https:\/\/ebooks.inflibnet.ac.in\/csp10\/wp-content\/uploads\/sites\/50\/2018\/07\/Untitled-62-224x300.png 224w, https:\/\/ebooks.inflibnet.ac.in\/csp10\/wp-content\/uploads\/sites\/50\/2018\/07\/Untitled-62-65x87.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp10\/wp-content\/uploads\/sites\/50\/2018\/07\/Untitled-62-225x301.png 225w, https:\/\/ebooks.inflibnet.ac.in\/csp10\/wp-content\/uploads\/sites\/50\/2018\/07\/Untitled-62-350x468.png 350w\" sizes=\"auto, (max-width: 690px) 100vw, 690px\" \/><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-121 aligncenter\" src=\"http:\/\/csp10.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/50\/2018\/07\/Untitled-63.png\" alt=\"\" width=\"674\" height=\"881\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp10\/wp-content\/uploads\/sites\/50\/2018\/07\/Untitled-63.png 674w, https:\/\/ebooks.inflibnet.ac.in\/csp10\/wp-content\/uploads\/sites\/50\/2018\/07\/Untitled-63-230x300.png 230w, https:\/\/ebooks.inflibnet.ac.in\/csp10\/wp-content\/uploads\/sites\/50\/2018\/07\/Untitled-63-65x85.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp10\/wp-content\/uploads\/sites\/50\/2018\/07\/Untitled-63-225x294.png 225w, https:\/\/ebooks.inflibnet.ac.in\/csp10\/wp-content\/uploads\/sites\/50\/2018\/07\/Untitled-63-350x457.png 350w\" sizes=\"auto, (max-width: 674px) 100vw, 674px\" \/><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-122 aligncenter\" src=\"http:\/\/csp10.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/50\/2018\/07\/Untitled-64.png\" alt=\"\" width=\"716\" height=\"883\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp10\/wp-content\/uploads\/sites\/50\/2018\/07\/Untitled-64.png 716w, https:\/\/ebooks.inflibnet.ac.in\/csp10\/wp-content\/uploads\/sites\/50\/2018\/07\/Untitled-64-243x300.png 243w, https:\/\/ebooks.inflibnet.ac.in\/csp10\/wp-content\/uploads\/sites\/50\/2018\/07\/Untitled-64-65x80.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp10\/wp-content\/uploads\/sites\/50\/2018\/07\/Untitled-64-225x277.png 225w, https:\/\/ebooks.inflibnet.ac.in\/csp10\/wp-content\/uploads\/sites\/50\/2018\/07\/Untitled-64-350x432.png 350w\" sizes=\"auto, (max-width: 716px) 100vw, 716px\" \/><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-123 aligncenter\" src=\"http:\/\/csp10.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/50\/2018\/07\/Untitled-65.png\" alt=\"\" width=\"691\" height=\"873\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp10\/wp-content\/uploads\/sites\/50\/2018\/07\/Untitled-65.png 691w, https:\/\/ebooks.inflibnet.ac.in\/csp10\/wp-content\/uploads\/sites\/50\/2018\/07\/Untitled-65-237x300.png 237w, https:\/\/ebooks.inflibnet.ac.in\/csp10\/wp-content\/uploads\/sites\/50\/2018\/07\/Untitled-65-65x82.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp10\/wp-content\/uploads\/sites\/50\/2018\/07\/Untitled-65-225x284.png 225w, https:\/\/ebooks.inflibnet.ac.in\/csp10\/wp-content\/uploads\/sites\/50\/2018\/07\/Untitled-65-350x442.png 350w\" sizes=\"auto, (max-width: 691px) 100vw, 691px\" \/><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-124 aligncenter\" src=\"http:\/\/csp10.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/50\/2018\/07\/Untitled-66.png\" alt=\"\" width=\"677\" height=\"885\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp10\/wp-content\/uploads\/sites\/50\/2018\/07\/Untitled-66.png 677w, https:\/\/ebooks.inflibnet.ac.in\/csp10\/wp-content\/uploads\/sites\/50\/2018\/07\/Untitled-66-229x300.png 229w, https:\/\/ebooks.inflibnet.ac.in\/csp10\/wp-content\/uploads\/sites\/50\/2018\/07\/Untitled-66-65x85.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp10\/wp-content\/uploads\/sites\/50\/2018\/07\/Untitled-66-225x294.png 225w, https:\/\/ebooks.inflibnet.ac.in\/csp10\/wp-content\/uploads\/sites\/50\/2018\/07\/Untitled-66-350x458.png 350w\" sizes=\"auto, (max-width: 677px) 100vw, 677px\" \/><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-125 aligncenter\" src=\"http:\/\/csp10.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/50\/2018\/07\/Untitled-67.png\" alt=\"\" width=\"683\" height=\"836\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp10\/wp-content\/uploads\/sites\/50\/2018\/07\/Untitled-67.png 683w, https:\/\/ebooks.inflibnet.ac.in\/csp10\/wp-content\/uploads\/sites\/50\/2018\/07\/Untitled-67-245x300.png 245w, https:\/\/ebooks.inflibnet.ac.in\/csp10\/wp-content\/uploads\/sites\/50\/2018\/07\/Untitled-67-65x80.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp10\/wp-content\/uploads\/sites\/50\/2018\/07\/Untitled-67-225x275.png 225w, https:\/\/ebooks.inflibnet.ac.in\/csp10\/wp-content\/uploads\/sites\/50\/2018\/07\/Untitled-67-350x428.png 350w\" sizes=\"auto, (max-width: 683px) 100vw, 683px\" \/><\/p>\n","protected":false},"author":4,"menu_order":20,"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-112","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\/112","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\/112\/revisions"}],"predecessor-version":[{"id":126,"href":"https:\/\/ebooks.inflibnet.ac.in\/csp10\/wp-json\/pressbooks\/v2\/chapters\/112\/revisions\/126"}],"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\/112\/metadata\/"}],"wp:attachment":[{"href":"https:\/\/ebooks.inflibnet.ac.in\/csp10\/wp-json\/wp\/v2\/media?parent=112"}],"wp:term":[{"taxonomy":"chapter-type","embeddable":true,"href":"https:\/\/ebooks.inflibnet.ac.in\/csp10\/wp-json\/pressbooks\/v2\/chapter-type?post=112"},{"taxonomy":"contributor","embeddable":true,"href":"https:\/\/ebooks.inflibnet.ac.in\/csp10\/wp-json\/wp\/v2\/contributor?post=112"},{"taxonomy":"license","embeddable":true,"href":"https:\/\/ebooks.inflibnet.ac.in\/csp10\/wp-json\/wp\/v2\/license?post=112"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}