{"id":83,"date":"2018-07-19T11:27:32","date_gmt":"2018-07-19T11:27:32","guid":{"rendered":"http:\/\/csp10.epgpbooks.inflibnet.ac.in\/?post_type=chapter&#038;p=83"},"modified":"2018-07-19T11:28:41","modified_gmt":"2018-07-19T11:28:41","slug":"slr-parsing","status":"publish","type":"chapter","link":"https:\/\/ebooks.inflibnet.ac.in\/csp10\/chapter\/slr-parsing\/","title":{"rendered":"SLR PARSING"},"content":{"raw":"<p style=\"text-align: justify\">In this module, we will discuss the parsing action of the SLR parser. The parser uses the SLR parsing table already discussed in the previous module and uses a stack and compares the contents of the stack with the input symbol and manipulates the stack by referring to the parsing table.<\/p>\r\n&nbsp;\r\n\r\n<strong>16.1 Parsing algorithm<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">As in the case of LL(1), Shift-reduce and Operator precedence parser discussed so far, the SLR(1) parser also uses a stack for manipulating the input string and decide on successful or unsuccessful parsing action. The stack is loaded with the initial state symbol 0. As we already discussed in the previous module, the set of items number correspond to the state of the parsing table. The input string is appended with $ symbol. The contents of the stack and the input is shown in figure 16.1<\/p>\r\n<img class=\"size-full wp-image-84 aligncenter\" src=\"http:\/\/csp10.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/50\/2018\/07\/Untitled-38.png\" alt=\"\" width=\"680\" height=\"548\" \/>\r\n<p style=\"text-align: justify\">of the RHS of the production from the stack. We then observe the state number after popping and let the state be sm -r. The LHS of the production A is pushed onto the stack and the combination of sm-r, A is looked in the goto() section of the SLR parsing table and the corresponding state number is pushed onto the stack. The input pointer stays as it is in the previous iteration<\/p>\r\n\r\n<ol start=\"3\">\r\n \t<li>If <em>action<\/em>[<em>s<\/em><em>m<\/em> ,<em>a<\/em><em>i<\/em>] = accept, then stop<\/li>\r\n<\/ol>\r\n<p style=\"text-align: justify\">This is a terminating action of the parsing table where the action corresponds to accept and this would be typically reached only if ai corresponds to \u201c$\u201d as it is the only combination in the table that has the accept action.<\/p>\r\n\r\n<ol start=\"4\">\r\n \t<li>If <em>action<\/em>[<em>s<\/em><em>m<\/em> ,<em>a<\/em><em>i<\/em>] = error, then attempt recovery<\/li>\r\n<\/ol>\r\n<p style=\"text-align: justify\">If the table entry corresponds to an empty entry then we claim it as an error combination and the parser has to go through a error recovery process.<\/p>\r\n&nbsp;\r\n\r\nThe algorithm is given is Algorithm 16.1\r\n\r\n&nbsp;\r\n\r\nSLR_PARSING(SLR_Parsing_table T, Input w$)\r\n\r\n&nbsp;\r\n\r\n{\r\n<ul>\r\n \t<li>Set input to point to the first symbol of w$<\/li>\r\n \t<li>Repeat forever<\/li>\r\n<\/ul>\r\nLet s be the state on the top of the stack\r\n<ol>\r\n \t<li>Let a be the symbol pointed to by ip<\/li>\r\n \t<li>If action [s, a] = shift s\u2019 then\r\n<ol>\r\n \t<li>Push a then s\u2019 on top of the stack<\/li>\r\n<\/ol>\r\n<\/li>\r\n<\/ol>\r\n<ol start=\"2\">\r\n \t<li>Move input to the next input symbol<\/li>\r\n<\/ol>\r\n<ul>\r\n \t<li>Else if action [s, a] = reduce A \u00e0 \u03b2 then\r\n<ol>\r\n \t<li>Pop 2 * | \u03b2 | symbols off the stack<\/li>\r\n<\/ol>\r\n<\/li>\r\n<\/ul>\r\n<ol>\r\n \t<li>Let s\u2019 be the state now on the top of the stack<\/li>\r\n \t<li>Push A then goto [s\u2019, A] on top of the stack<\/li>\r\n \t<li>Output the production A \u00e0 \u03b2<\/li>\r\n \t<li>Else if action[s, a] = accept then return;<\/li>\r\n \t<li>Else error()<\/li>\r\n<\/ol>\r\n}\r\n\r\n&nbsp;\r\n\r\nThe steps from (ii) to (v) correspond to the steps (1) to (4) of the earlier procedure.\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">Example 16.1 Consider the string \u201cid * id +id\u201d to be parsed by the SLR parser. For convenience the SLR parsing table is given in Table 16.1. The input string is appended with $ to indicate end of input. The overall parsing action is given in Table 16.2 where the comments column indicates how parsing action is done.<\/p>\r\n&nbsp;\r\n\r\n<img class=\"size-full wp-image-85 aligncenter\" src=\"http:\/\/csp10.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/50\/2018\/07\/Untitled-39.png\" alt=\"\" width=\"698\" height=\"912\" \/>\r\n\r\n<img class=\"size-full wp-image-86 aligncenter\" src=\"http:\/\/csp10.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/50\/2018\/07\/Untitled-40.png\" alt=\"\" width=\"682\" height=\"641\" \/>\r\n<div>\r\n<p style=\"text-align: justify\">If the input has the next symbol as \u201c)\u201d after the last but one step in Table 16.2, we need to compare [1, )]. As we know from the parsing table 16.1, the table entry is blank which means the input is not accepted and the parser cannot continue parsing. For these situations, error recovery mechanisms in terms of panic \/ phrase mode need to be applied. We can also cla im that \u201cEvery SLR grammar is unambiguous, but <strong>not<\/strong> every unambiguous grammar is SLR\u201d. This means that the SLR parser can parse only a small class of grammar.<\/p>\r\n\r\n<\/div>\r\n&nbsp;\r\n\r\n<img class=\"size-full wp-image-87 aligncenter\" src=\"http:\/\/csp10.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/50\/2018\/07\/Untitled-41.png\" alt=\"\" width=\"651\" height=\"873\" \/>\r\n\r\n<img class=\"size-full wp-image-88 aligncenter\" src=\"http:\/\/csp10.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/50\/2018\/07\/Untitled-42.png\" alt=\"\" width=\"678\" height=\"892\" \/>\r\n\r\n&nbsp;\r\n\r\n<img class=\"size-full wp-image-89 aligncenter\" src=\"http:\/\/csp10.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/50\/2018\/07\/Untitled-43.png\" alt=\"\" width=\"676\" height=\"371\" \/>","rendered":"<p style=\"text-align: justify\">In this module, we will discuss the parsing action of the SLR parser. The parser uses the SLR parsing table already discussed in the previous module and uses a stack and compares the contents of the stack with the input symbol and manipulates the stack by referring to the parsing table.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>16.1 Parsing algorithm<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">As in the case of LL(1), Shift-reduce and Operator precedence parser discussed so far, the SLR(1) parser also uses a stack for manipulating the input string and decide on successful or unsuccessful parsing action. The stack is loaded with the initial state symbol 0. As we already discussed in the previous module, the set of items number correspond to the state of the parsing table. The input string is appended with $ symbol. The contents of the stack and the input is shown in figure 16.1<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-84 aligncenter\" src=\"http:\/\/csp10.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/50\/2018\/07\/Untitled-38.png\" alt=\"\" width=\"680\" height=\"548\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp10\/wp-content\/uploads\/sites\/50\/2018\/07\/Untitled-38.png 680w, https:\/\/ebooks.inflibnet.ac.in\/csp10\/wp-content\/uploads\/sites\/50\/2018\/07\/Untitled-38-300x242.png 300w, https:\/\/ebooks.inflibnet.ac.in\/csp10\/wp-content\/uploads\/sites\/50\/2018\/07\/Untitled-38-65x52.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp10\/wp-content\/uploads\/sites\/50\/2018\/07\/Untitled-38-225x181.png 225w, https:\/\/ebooks.inflibnet.ac.in\/csp10\/wp-content\/uploads\/sites\/50\/2018\/07\/Untitled-38-350x282.png 350w\" sizes=\"auto, (max-width: 680px) 100vw, 680px\" \/><\/p>\n<p style=\"text-align: justify\">of the RHS of the production from the stack. We then observe the state number after popping and let the state be sm -r. The LHS of the production A is pushed onto the stack and the combination of sm-r, A is looked in the goto() section of the SLR parsing table and the corresponding state number is pushed onto the stack. The input pointer stays as it is in the previous iteration<\/p>\n<ol start=\"3\">\n<li>If <em>action<\/em>[<em>s<\/em><em>m<\/em> ,<em>a<\/em><em>i<\/em>] = accept, then stop<\/li>\n<\/ol>\n<p style=\"text-align: justify\">This is a terminating action of the parsing table where the action corresponds to accept and this would be typically reached only if ai corresponds to \u201c$\u201d as it is the only combination in the table that has the accept action.<\/p>\n<ol start=\"4\">\n<li>If <em>action<\/em>[<em>s<\/em><em>m<\/em> ,<em>a<\/em><em>i<\/em>] = error, then attempt recovery<\/li>\n<\/ol>\n<p style=\"text-align: justify\">If the table entry corresponds to an empty entry then we claim it as an error combination and the parser has to go through a error recovery process.<\/p>\n<p>&nbsp;<\/p>\n<p>The algorithm is given is Algorithm 16.1<\/p>\n<p>&nbsp;<\/p>\n<p>SLR_PARSING(SLR_Parsing_table T, Input w$)<\/p>\n<p>&nbsp;<\/p>\n<p>{<\/p>\n<ul>\n<li>Set input to point to the first symbol of w$<\/li>\n<li>Repeat forever<\/li>\n<\/ul>\n<p>Let s be the state on the top of the stack<\/p>\n<ol>\n<li>Let a be the symbol pointed to by ip<\/li>\n<li>If action [s, a] = shift s\u2019 then\n<ol>\n<li>Push a then s\u2019 on top of the stack<\/li>\n<\/ol>\n<\/li>\n<\/ol>\n<ol start=\"2\">\n<li>Move input to the next input symbol<\/li>\n<\/ol>\n<ul>\n<li>Else if action [s, a] = reduce A \u00e0 \u03b2 then\n<ol>\n<li>Pop 2 * | \u03b2 | symbols off the stack<\/li>\n<\/ol>\n<\/li>\n<\/ul>\n<ol>\n<li>Let s\u2019 be the state now on the top of the stack<\/li>\n<li>Push A then goto [s\u2019, A] on top of the stack<\/li>\n<li>Output the production A \u00e0 \u03b2<\/li>\n<li>Else if action[s, a] = accept then return;<\/li>\n<li>Else error()<\/li>\n<\/ol>\n<p>}<\/p>\n<p>&nbsp;<\/p>\n<p>The steps from (ii) to (v) correspond to the steps (1) to (4) of the earlier procedure.<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">Example 16.1 Consider the string \u201cid * id +id\u201d to be parsed by the SLR parser. For convenience the SLR parsing table is given in Table 16.1. The input string is appended with $ to indicate end of input. The overall parsing action is given in Table 16.2 where the comments column indicates how parsing action is done.<\/p>\n<p>&nbsp;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-85 aligncenter\" src=\"http:\/\/csp10.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/50\/2018\/07\/Untitled-39.png\" alt=\"\" width=\"698\" height=\"912\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp10\/wp-content\/uploads\/sites\/50\/2018\/07\/Untitled-39.png 698w, https:\/\/ebooks.inflibnet.ac.in\/csp10\/wp-content\/uploads\/sites\/50\/2018\/07\/Untitled-39-230x300.png 230w, https:\/\/ebooks.inflibnet.ac.in\/csp10\/wp-content\/uploads\/sites\/50\/2018\/07\/Untitled-39-65x85.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp10\/wp-content\/uploads\/sites\/50\/2018\/07\/Untitled-39-225x294.png 225w, https:\/\/ebooks.inflibnet.ac.in\/csp10\/wp-content\/uploads\/sites\/50\/2018\/07\/Untitled-39-350x457.png 350w\" sizes=\"auto, (max-width: 698px) 100vw, 698px\" \/><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-86 aligncenter\" src=\"http:\/\/csp10.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/50\/2018\/07\/Untitled-40.png\" alt=\"\" width=\"682\" height=\"641\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp10\/wp-content\/uploads\/sites\/50\/2018\/07\/Untitled-40.png 682w, https:\/\/ebooks.inflibnet.ac.in\/csp10\/wp-content\/uploads\/sites\/50\/2018\/07\/Untitled-40-300x282.png 300w, https:\/\/ebooks.inflibnet.ac.in\/csp10\/wp-content\/uploads\/sites\/50\/2018\/07\/Untitled-40-65x61.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp10\/wp-content\/uploads\/sites\/50\/2018\/07\/Untitled-40-225x211.png 225w, https:\/\/ebooks.inflibnet.ac.in\/csp10\/wp-content\/uploads\/sites\/50\/2018\/07\/Untitled-40-350x329.png 350w\" sizes=\"auto, (max-width: 682px) 100vw, 682px\" \/><\/p>\n<div>\n<p style=\"text-align: justify\">If the input has the next symbol as \u201c)\u201d after the last but one step in Table 16.2, we need to compare [1, )]. As we know from the parsing table 16.1, the table entry is blank which means the input is not accepted and the parser cannot continue parsing. For these situations, error recovery mechanisms in terms of panic \/ phrase mode need to be applied. We can also cla im that \u201cEvery SLR grammar is unambiguous, but <strong>not<\/strong> every unambiguous grammar is SLR\u201d. This means that the SLR parser can parse only a small class of grammar.<\/p>\n<\/div>\n<p>&nbsp;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-87 aligncenter\" src=\"http:\/\/csp10.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/50\/2018\/07\/Untitled-41.png\" alt=\"\" width=\"651\" height=\"873\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp10\/wp-content\/uploads\/sites\/50\/2018\/07\/Untitled-41.png 651w, https:\/\/ebooks.inflibnet.ac.in\/csp10\/wp-content\/uploads\/sites\/50\/2018\/07\/Untitled-41-224x300.png 224w, https:\/\/ebooks.inflibnet.ac.in\/csp10\/wp-content\/uploads\/sites\/50\/2018\/07\/Untitled-41-65x87.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp10\/wp-content\/uploads\/sites\/50\/2018\/07\/Untitled-41-225x302.png 225w, https:\/\/ebooks.inflibnet.ac.in\/csp10\/wp-content\/uploads\/sites\/50\/2018\/07\/Untitled-41-350x469.png 350w\" sizes=\"auto, (max-width: 651px) 100vw, 651px\" \/><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-88 aligncenter\" src=\"http:\/\/csp10.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/50\/2018\/07\/Untitled-42.png\" alt=\"\" width=\"678\" height=\"892\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp10\/wp-content\/uploads\/sites\/50\/2018\/07\/Untitled-42.png 678w, https:\/\/ebooks.inflibnet.ac.in\/csp10\/wp-content\/uploads\/sites\/50\/2018\/07\/Untitled-42-228x300.png 228w, https:\/\/ebooks.inflibnet.ac.in\/csp10\/wp-content\/uploads\/sites\/50\/2018\/07\/Untitled-42-65x86.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp10\/wp-content\/uploads\/sites\/50\/2018\/07\/Untitled-42-225x296.png 225w, https:\/\/ebooks.inflibnet.ac.in\/csp10\/wp-content\/uploads\/sites\/50\/2018\/07\/Untitled-42-350x460.png 350w\" sizes=\"auto, (max-width: 678px) 100vw, 678px\" \/><\/p>\n<p>&nbsp;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-89 aligncenter\" src=\"http:\/\/csp10.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/50\/2018\/07\/Untitled-43.png\" alt=\"\" width=\"676\" height=\"371\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp10\/wp-content\/uploads\/sites\/50\/2018\/07\/Untitled-43.png 676w, https:\/\/ebooks.inflibnet.ac.in\/csp10\/wp-content\/uploads\/sites\/50\/2018\/07\/Untitled-43-300x165.png 300w, https:\/\/ebooks.inflibnet.ac.in\/csp10\/wp-content\/uploads\/sites\/50\/2018\/07\/Untitled-43-65x36.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp10\/wp-content\/uploads\/sites\/50\/2018\/07\/Untitled-43-225x123.png 225w, https:\/\/ebooks.inflibnet.ac.in\/csp10\/wp-content\/uploads\/sites\/50\/2018\/07\/Untitled-43-350x192.png 350w\" sizes=\"auto, (max-width: 676px) 100vw, 676px\" \/><\/p>\n","protected":false},"author":4,"menu_order":16,"template":"","meta":{"_acf_changed":false,"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-83","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\/83","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\/83\/revisions"}],"predecessor-version":[{"id":90,"href":"https:\/\/ebooks.inflibnet.ac.in\/csp10\/wp-json\/pressbooks\/v2\/chapters\/83\/revisions\/90"}],"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\/83\/metadata\/"}],"wp:attachment":[{"href":"https:\/\/ebooks.inflibnet.ac.in\/csp10\/wp-json\/wp\/v2\/media?parent=83"}],"wp:term":[{"taxonomy":"chapter-type","embeddable":true,"href":"https:\/\/ebooks.inflibnet.ac.in\/csp10\/wp-json\/pressbooks\/v2\/chapter-type?post=83"},{"taxonomy":"contributor","embeddable":true,"href":"https:\/\/ebooks.inflibnet.ac.in\/csp10\/wp-json\/wp\/v2\/contributor?post=83"},{"taxonomy":"license","embeddable":true,"href":"https:\/\/ebooks.inflibnet.ac.in\/csp10\/wp-json\/wp\/v2\/license?post=83"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}