{"id":78,"date":"2018-07-19T11:18:32","date_gmt":"2018-07-19T11:18:32","guid":{"rendered":"http:\/\/csp10.epgpbooks.inflibnet.ac.in\/?post_type=chapter&#038;p=78"},"modified":"2018-07-19T11:19:25","modified_gmt":"2018-07-19T11:19:25","slug":"slr-parser-slr-parsing-table","status":"publish","type":"chapter","link":"https:\/\/ebooks.inflibnet.ac.in\/csp10\/chapter\/slr-parser-slr-parsing-table\/","title":{"rendered":"SLR Parser \u2013 SLR Parsing table"},"content":{"raw":"<div>\r\n<p style=\"text-align: justify\">In the previous module, we discussed the construction of the set of items. These items which are referred to canonical items are used to construct the SLR parsing table. In this module we will discuss the construction of the SLR parsing table. We also brief on the SLR parsing procedure in this module.<\/p>\r\n&nbsp;\r\n\r\n<strong>15.1 SLR Parsing Table construction<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">This algorithm does not produce a uniquely defined parsing action tables for all grammar. However, it produces fairly good results on many grammars used for programming languages. The input to this table is the canonical set of items which is given as the Deterministic finite automata as already discussed in the previous module. The DFA is designed to recognize viable prefixes. The SLR parsing table is constructed between the set of canonical items against the grammar symbols. The non-terminals section of the parsing table corresponds to the goto() function and the terminals section of the table corresponds to shift, reduce, accept and error action. The steps involved in the SLR parsing table construction is detailed in Algorithm 15.1<\/p>\r\n&nbsp;\r\n\r\nAlgorithm 15.1 SLR Parsing table\r\n\r\n&nbsp;\r\n\r\n\u2022\u00a0\u00a0\u00a0\u00a0\u00a0 Input: Augmented Grammar G\u2019\r\n\r\n\u2022\u00a0\u00a0\u00a0\u00a0\u00a0 Output: SLR parsing table with functions, shift, reduce and accept SLR_Parsing_Table(Augmened grammar G\u2019)\r\n\r\n{\r\n\r\n&nbsp;\r\n\r\n1.\u00a0\u00a0\u00a0\u00a0\u00a0 Construct the set <em>C<\/em>={<em>I<\/em>0,<em>I<\/em>1,\u2026,<em>I<\/em><em>n<\/em>} of <em>LR(0) items<\/em>\r\n\r\n2.\u00a0\u00a0\u00a0\u00a0\u00a0 State \u2018i\u2019 is constructed from Ii. The parsing action for state \u2018i' are determined as follows:\r\n\r\na.\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 If [<em>A<\/em>\u00aea\u2022<em>a<\/em>b] \u00ce <em>I<\/em><em>i<\/em> and <em>goto<\/em>(<em>I<\/em><em>i<\/em>,<em>a<\/em>)=<em>I<\/em><em>j<\/em> then set <em>action<\/em>[<em>i<\/em>,<em>a<\/em>]=shift <em>j, where a is a<\/em> <em>terminal<\/em>\r\n\r\nb.\u00a0\u00a0\u00a0\u00a0\u00a0 If [<em>A<\/em>\u00aea\u2022] \u00ce <em>I<\/em><em>i<\/em> then set <em>action<\/em>[<em>i<\/em>,<em>a<\/em>]=reduce A\u00aea for all <em>a<\/em> \u00ce FOLLOW(<em>A<\/em>) where <em>A<\/em><em>\u00b9<\/em><em>S<\/em>\u2019)\r\n\r\nc.\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 If [<em>S<\/em>\u2019\u00ae<em>S<\/em>\u2022] is in <em>I<\/em><em>i<\/em> then set <em>action<\/em>[<em>i<\/em>,<strong>$<\/strong>]=accept\r\n\r\n3.\u00a0\u00a0\u00a0\u00a0\u00a0 If <em>goto<\/em>(<em>I<\/em><em>i<\/em>,<em>A<\/em>)=<em>I<\/em><em>j<\/em> then set <em>goto<\/em>[<em>i<\/em>,<em>A<\/em>]=<em>j<\/em>\r\n\r\n4.\u00a0\u00a0\u00a0\u00a0\u00a0 All other entries other than 2, 3 is declared as error\r\n\r\na.\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 Repeat for all the items until no more entries added\r\n\r\n<\/div>\r\n<ol start=\"5\">\r\n \t<li>The initial state <em>i<\/em> is the <em>I<\/em><em>i<\/em> holding item [<em>S<\/em>\u2019\u00ae\u2022<em>S<\/em>]<\/li>\r\n<\/ol>\r\n}\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">The rows of the parsing table correspond to the items number. These are referred to as states. The columns correspond to the non-terminals and terminals. The non-terminals correspond to the goto() function. The table entry between the state and the termina ls will reflect one of the four actions: shift, reduce, accept, and error. On the other hand, the table entry between the state entry and the non-terminals correspond to the goto section.<\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify\">From algorithm 15.1, step 2 of the algorithm handles the actions. In 2a, it considers the canonical collection of items and we perform a goto(I,X) computation where X is a terminal. It considers the target item number from which the goto() is initiated and the destination item number which is being formed. Sets it as a shift action at the table entry [i,a] if goto(Ii,a)=Ij. The step 2b of the algorithm is for reduce action, where we consider the kernel canonical items that have a dot at the end and the table entry [ i,a] is set as reduce by the production A\u00aea, where \u2018a\u2019 corresponds to the FOLLOW(A). Step 2c of the algorithm is very simple, where it considers the item that has the augmented grammar\u2019s kernel item and marks the table entry [kernel item number, $] as accept. The remaining entries of the action section of the parsing table are set to error.<\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify\">Step 3 of the algorithm generates table entries between states and the non-terminals. The table entry [i,X] = j, if goto(Ii,X)=Ij where X is a non-terminal. The remaining entries of the parsing table are considered as error.<\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify\">Example 15.1 Let us construct the parsing table for the expression grammar. The grammar is given here for reference. The canonical collection of items is also given for quick reference in Table 15.1.<\/p>\r\n\r\n<ul>\r\n \t<li>E\u2019 \u00e0E<\/li>\r\n \t<li>E \u00e0 E +T<\/li>\r\n \t<li>E \u00e0 T<\/li>\r\n \t<li>T \u00e0 T * F<\/li>\r\n \t<li>T \u00e0 F<\/li>\r\n \t<li>F \u00e0 (E)<\/li>\r\n \t<li>F \u00e0 id<\/li>\r\n<\/ul>\r\n<img class=\"size-full wp-image-79 aligncenter\" src=\"http:\/\/csp10.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/50\/2018\/07\/Untitled-35.png\" alt=\"\" width=\"668\" height=\"863\" \/>\r\n<p style=\"text-align: justify\">Since, E is the start symbol of the original grammar, E gets $ in the FOLLOW() set. Using the production F \u00e0 (E), E gets the \u2018)\u2019 symbol. Using the production, E \u00e0 E+T, E gets \u201c+\u201d. Using the production E \u00e0 T, FOLLOW(T) = FOLLOW(E). In addition, to the FOLLOW(E), FOLLOW(T) gets, \u201c*\u201d from the production T \u00e0 T*F. T\u00e0 F yields, FOLLOW(F) = FOLLOW(T) and thus the FOLLOW(T) and FOLLOW(F) results in the same set. The computation of FOLLOW() is given below in equations 15.1 to 15.3<\/p>\r\n<img class=\"size-full wp-image-80 aligncenter\" src=\"http:\/\/csp10.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/50\/2018\/07\/Untitled-36.png\" alt=\"\" width=\"680\" height=\"777\" \/>\r\n\r\nGoto(I3, ( ) = Goto(I6, ( ) = Goto(I10, ( ) =I3 =&gt; [3, ( ] = [6, (] = [10, ( ] = s3 Goto(I7, ) ) = I9 =&gt; [7, ) ] = s9\r\n\r\n&nbsp;\r\n\r\nSimilarly for the goto () section of the parsing table, consider Goto(I0, E ) = 1 and so at the table entry [0, E] we set it to 1. The other entries are given below\r\n\r\n&nbsp;\r\n\r\nGoto(I0, T) = Goto(I3, T ) = 2 =&gt; [0,T] = [3, T] = 2\r\n\r\n&nbsp;\r\n\r\nGoto(I0, F ) = Goto(I3, F ) = Goto(I6, F ) = 4, =&gt; [0,F] = [3, F] = [6, F] = 4\r\n\r\n&nbsp;\r\n\r\nGoto(I3, E ) = 7 =&gt; [3, E] = 7\r\n\r\n&nbsp;\r\n\r\nGoto(I6, T ) = 8 =&gt; [6,T] = 8\r\n\r\n&nbsp;\r\n\r\nGoto(I10, F) = 11 =&gt; [10, F] =11\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">After looking at the shift and goto action, it is now convenient to set the accept action. Accept action is set based on the augmented grammar\u2019s first production. So at the item number where [S\u2019\u00e0 S.] is available, we set this item number against $ as accept.<\/p>\r\n&nbsp;\r\n\r\nIn the expression grammar [E\u2019 \u00e0 E<strong>.<\/strong>] is available in Item 1. So at the entry [1, $], we mark \u201cacc\u201d, to indicate accept.\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">Now onto the reduce actions. The item numbers that has the kernel item qualifies for this action. Table 15.3 summarizes the item numbers that has this kernel item as one of its component. These items may or may not have other items in it and is not considered. As can be seen from table 15.3, there are 6 productions in the grammar and thus there are 6 kernel items. The reduce action procedure is explained in Table 15.3<\/p>\r\n&nbsp;\r\n\r\nTable 15.3 Reduce action computation\r\n\r\n<img class=\"size-full wp-image-81 aligncenter\" src=\"http:\/\/csp10.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/50\/2018\/07\/Untitled-37.png\" alt=\"\" width=\"557\" height=\"565\" \/>\r\n\r\nUsing table 15.3 we construct Table 15.2 which has the reduce action incorporated. This parser however is not a very powerful parser.\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">After constructing the parsing table, the SLR parsing algorithm uses this table, along with a stack to validate a string. The steps involved in the parsing algorithm are given below:<\/p>\r\n\r\n<ul>\r\n \t<li>If <em>action<\/em>[<em>s<\/em><em>m<\/em> ,<em>a<\/em><em>i<\/em>] = shift <em>s<\/em>, then push <em>a<\/em><em>i<\/em>, push <em>s<\/em>, and advance input:<\/li>\r\n<\/ul>\r\n(<em>s<\/em>0 <em>X<\/em>1 <em>s<\/em>1 <em>X<\/em>2 <em>s<\/em>2 <em>\u2026 X<\/em><em>m<\/em> <em>s<\/em><em>m<\/em> <em>a<\/em><em>i<\/em> <em>s<\/em>,\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <em>a<\/em><em>i<\/em>+1 \u2026<em> a<\/em><em>n<\/em> <strong>$<\/strong>)\r\n<ul>\r\n \t<li>If <em>action<\/em>[<em>s<\/em><em>m<\/em> ,<em>a<\/em><em>i<\/em>] = reduce A \u00ae b and <em>goto<\/em>[<em>s<\/em><em>m -r<\/em>,<em>A<\/em>] = <em>s<\/em> with <em>r<\/em>=|b| then pop 2<em>r<\/em> symbols, push <em>A<\/em>, and push <em>s<\/em>:<\/li>\r\n<\/ul>\r\n(<em>s<\/em>0 <em>X<\/em>1 <em>s<\/em>1 <em>X<\/em>2 <em>s<\/em>2 <em>\u2026 X<\/em><em>m -r<\/em> <em>s<\/em><em>m-r<\/em> <em>A s<\/em>,\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <em>a<\/em><em>i<\/em><em> a<\/em><em>i<\/em>+1 \u2026<em> a<\/em><em>n<\/em> <strong>$<\/strong>)\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">The configuration of the stack contains alternately state number corresponding to item number and grammar symbol, with the top of the stack being a state number. The input contains a terminal. The top of the stack state is compared with the input and we go for a push action if the action is shift, and would pop if the action is reduce. The algorithm is detailed in the next module with more explanation and example.<\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify\"><strong>Summary<\/strong>: In this module, we learnt to construct the follow() for the input grammar as it is necessary for the parsing table\u2019s reduce action. We also studied the manner in which the canonical collection of items is used to construct the parsing table\u2019s shift, reduce, accept and error actions. Based on this parsing table, the next module will discuss the parsing action and how to handle the various errors and conflicts of the SLR parser.<\/p>","rendered":"<div>\n<p style=\"text-align: justify\">In the previous module, we discussed the construction of the set of items. These items which are referred to canonical items are used to construct the SLR parsing table. In this module we will discuss the construction of the SLR parsing table. We also brief on the SLR parsing procedure in this module.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>15.1 SLR Parsing Table construction<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">This algorithm does not produce a uniquely defined parsing action tables for all grammar. However, it produces fairly good results on many grammars used for programming languages. The input to this table is the canonical set of items which is given as the Deterministic finite automata as already discussed in the previous module. The DFA is designed to recognize viable prefixes. The SLR parsing table is constructed between the set of canonical items against the grammar symbols. The non-terminals section of the parsing table corresponds to the goto() function and the terminals section of the table corresponds to shift, reduce, accept and error action. The steps involved in the SLR parsing table construction is detailed in Algorithm 15.1<\/p>\n<p>&nbsp;<\/p>\n<p>Algorithm 15.1 SLR Parsing table<\/p>\n<p>&nbsp;<\/p>\n<p>\u2022\u00a0\u00a0\u00a0\u00a0\u00a0 Input: Augmented Grammar G\u2019<\/p>\n<p>\u2022\u00a0\u00a0\u00a0\u00a0\u00a0 Output: SLR parsing table with functions, shift, reduce and accept SLR_Parsing_Table(Augmened grammar G\u2019)<\/p>\n<p>{<\/p>\n<p>&nbsp;<\/p>\n<p>1.\u00a0\u00a0\u00a0\u00a0\u00a0 Construct the set <em>C<\/em>={<em>I<\/em>0,<em>I<\/em>1,\u2026,<em>I<\/em><em>n<\/em>} of <em>LR(0) items<\/em><\/p>\n<p>2.\u00a0\u00a0\u00a0\u00a0\u00a0 State \u2018i\u2019 is constructed from Ii. The parsing action for state \u2018i&#8217; are determined as follows:<\/p>\n<p>a.\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 If [<em>A<\/em>\u00aea\u2022<em>a<\/em>b] \u00ce <em>I<\/em><em>i<\/em> and <em>goto<\/em>(<em>I<\/em><em>i<\/em>,<em>a<\/em>)=<em>I<\/em><em>j<\/em> then set <em>action<\/em>[<em>i<\/em>,<em>a<\/em>]=shift <em>j, where a is a<\/em> <em>terminal<\/em><\/p>\n<p>b.\u00a0\u00a0\u00a0\u00a0\u00a0 If [<em>A<\/em>\u00aea\u2022] \u00ce <em>I<\/em><em>i<\/em> then set <em>action<\/em>[<em>i<\/em>,<em>a<\/em>]=reduce A\u00aea for all <em>a<\/em> \u00ce FOLLOW(<em>A<\/em>) where <em>A<\/em><em>\u00b9<\/em><em>S<\/em>\u2019)<\/p>\n<p>c.\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 If [<em>S<\/em>\u2019\u00ae<em>S<\/em>\u2022] is in <em>I<\/em><em>i<\/em> then set <em>action<\/em>[<em>i<\/em>,<strong>$<\/strong>]=accept<\/p>\n<p>3.\u00a0\u00a0\u00a0\u00a0\u00a0 If <em>goto<\/em>(<em>I<\/em><em>i<\/em>,<em>A<\/em>)=<em>I<\/em><em>j<\/em> then set <em>goto<\/em>[<em>i<\/em>,<em>A<\/em>]=<em>j<\/em><\/p>\n<p>4.\u00a0\u00a0\u00a0\u00a0\u00a0 All other entries other than 2, 3 is declared as error<\/p>\n<p>a.\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 Repeat for all the items until no more entries added<\/p>\n<\/div>\n<ol start=\"5\">\n<li>The initial state <em>i<\/em> is the <em>I<\/em><em>i<\/em> holding item [<em>S<\/em>\u2019\u00ae\u2022<em>S<\/em>]<\/li>\n<\/ol>\n<p>}<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">The rows of the parsing table correspond to the items number. These are referred to as states. The columns correspond to the non-terminals and terminals. The non-terminals correspond to the goto() function. The table entry between the state and the termina ls will reflect one of the four actions: shift, reduce, accept, and error. On the other hand, the table entry between the state entry and the non-terminals correspond to the goto section.<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">From algorithm 15.1, step 2 of the algorithm handles the actions. In 2a, it considers the canonical collection of items and we perform a goto(I,X) computation where X is a terminal. It considers the target item number from which the goto() is initiated and the destination item number which is being formed. Sets it as a shift action at the table entry [i,a] if goto(Ii,a)=Ij. The step 2b of the algorithm is for reduce action, where we consider the kernel canonical items that have a dot at the end and the table entry [ i,a] is set as reduce by the production A\u00aea, where \u2018a\u2019 corresponds to the FOLLOW(A). Step 2c of the algorithm is very simple, where it considers the item that has the augmented grammar\u2019s kernel item and marks the table entry [kernel item number, $] as accept. The remaining entries of the action section of the parsing table are set to error.<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">Step 3 of the algorithm generates table entries between states and the non-terminals. The table entry [i,X] = j, if goto(Ii,X)=Ij where X is a non-terminal. The remaining entries of the parsing table are considered as error.<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">Example 15.1 Let us construct the parsing table for the expression grammar. The grammar is given here for reference. The canonical collection of items is also given for quick reference in Table 15.1.<\/p>\n<ul>\n<li>E\u2019 \u00e0E<\/li>\n<li>E \u00e0 E +T<\/li>\n<li>E \u00e0 T<\/li>\n<li>T \u00e0 T * F<\/li>\n<li>T \u00e0 F<\/li>\n<li>F \u00e0 (E)<\/li>\n<li>F \u00e0 id<\/li>\n<\/ul>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-79 aligncenter\" src=\"http:\/\/csp10.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/50\/2018\/07\/Untitled-35.png\" alt=\"\" width=\"668\" height=\"863\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp10\/wp-content\/uploads\/sites\/50\/2018\/07\/Untitled-35.png 668w, https:\/\/ebooks.inflibnet.ac.in\/csp10\/wp-content\/uploads\/sites\/50\/2018\/07\/Untitled-35-232x300.png 232w, https:\/\/ebooks.inflibnet.ac.in\/csp10\/wp-content\/uploads\/sites\/50\/2018\/07\/Untitled-35-65x84.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp10\/wp-content\/uploads\/sites\/50\/2018\/07\/Untitled-35-225x291.png 225w, https:\/\/ebooks.inflibnet.ac.in\/csp10\/wp-content\/uploads\/sites\/50\/2018\/07\/Untitled-35-350x452.png 350w\" sizes=\"auto, (max-width: 668px) 100vw, 668px\" \/><\/p>\n<p style=\"text-align: justify\">Since, E is the start symbol of the original grammar, E gets $ in the FOLLOW() set. Using the production F \u00e0 (E), E gets the \u2018)\u2019 symbol. Using the production, E \u00e0 E+T, E gets \u201c+\u201d. Using the production E \u00e0 T, FOLLOW(T) = FOLLOW(E). In addition, to the FOLLOW(E), FOLLOW(T) gets, \u201c*\u201d from the production T \u00e0 T*F. T\u00e0 F yields, FOLLOW(F) = FOLLOW(T) and thus the FOLLOW(T) and FOLLOW(F) results in the same set. The computation of FOLLOW() is given below in equations 15.1 to 15.3<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-80 aligncenter\" src=\"http:\/\/csp10.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/50\/2018\/07\/Untitled-36.png\" alt=\"\" width=\"680\" height=\"777\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp10\/wp-content\/uploads\/sites\/50\/2018\/07\/Untitled-36.png 680w, https:\/\/ebooks.inflibnet.ac.in\/csp10\/wp-content\/uploads\/sites\/50\/2018\/07\/Untitled-36-263x300.png 263w, https:\/\/ebooks.inflibnet.ac.in\/csp10\/wp-content\/uploads\/sites\/50\/2018\/07\/Untitled-36-65x74.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp10\/wp-content\/uploads\/sites\/50\/2018\/07\/Untitled-36-225x257.png 225w, https:\/\/ebooks.inflibnet.ac.in\/csp10\/wp-content\/uploads\/sites\/50\/2018\/07\/Untitled-36-350x400.png 350w\" sizes=\"auto, (max-width: 680px) 100vw, 680px\" \/><\/p>\n<p>Goto(I3, ( ) = Goto(I6, ( ) = Goto(I10, ( ) =I3 =&gt; [3, ( ] = [6, (] = [10, ( ] = s3 Goto(I7, ) ) = I9 =&gt; [7, ) ] = s9<\/p>\n<p>&nbsp;<\/p>\n<p>Similarly for the goto () section of the parsing table, consider Goto(I0, E ) = 1 and so at the table entry [0, E] we set it to 1. The other entries are given below<\/p>\n<p>&nbsp;<\/p>\n<p>Goto(I0, T) = Goto(I3, T ) = 2 =&gt; [0,T] = [3, T] = 2<\/p>\n<p>&nbsp;<\/p>\n<p>Goto(I0, F ) = Goto(I3, F ) = Goto(I6, F ) = 4, =&gt; [0,F] = [3, F] = [6, F] = 4<\/p>\n<p>&nbsp;<\/p>\n<p>Goto(I3, E ) = 7 =&gt; [3, E] = 7<\/p>\n<p>&nbsp;<\/p>\n<p>Goto(I6, T ) = 8 =&gt; [6,T] = 8<\/p>\n<p>&nbsp;<\/p>\n<p>Goto(I10, F) = 11 =&gt; [10, F] =11<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">After looking at the shift and goto action, it is now convenient to set the accept action. Accept action is set based on the augmented grammar\u2019s first production. So at the item number where [S\u2019\u00e0 S.] is available, we set this item number against $ as accept.<\/p>\n<p>&nbsp;<\/p>\n<p>In the expression grammar [E\u2019 \u00e0 E<strong>.<\/strong>] is available in Item 1. So at the entry [1, $], we mark \u201cacc\u201d, to indicate accept.<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">Now onto the reduce actions. The item numbers that has the kernel item qualifies for this action. Table 15.3 summarizes the item numbers that has this kernel item as one of its component. These items may or may not have other items in it and is not considered. As can be seen from table 15.3, there are 6 productions in the grammar and thus there are 6 kernel items. The reduce action procedure is explained in Table 15.3<\/p>\n<p>&nbsp;<\/p>\n<p>Table 15.3 Reduce action computation<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-81 aligncenter\" src=\"http:\/\/csp10.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/50\/2018\/07\/Untitled-37.png\" alt=\"\" width=\"557\" height=\"565\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp10\/wp-content\/uploads\/sites\/50\/2018\/07\/Untitled-37.png 557w, https:\/\/ebooks.inflibnet.ac.in\/csp10\/wp-content\/uploads\/sites\/50\/2018\/07\/Untitled-37-296x300.png 296w, https:\/\/ebooks.inflibnet.ac.in\/csp10\/wp-content\/uploads\/sites\/50\/2018\/07\/Untitled-37-65x66.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp10\/wp-content\/uploads\/sites\/50\/2018\/07\/Untitled-37-225x228.png 225w, https:\/\/ebooks.inflibnet.ac.in\/csp10\/wp-content\/uploads\/sites\/50\/2018\/07\/Untitled-37-350x355.png 350w\" sizes=\"auto, (max-width: 557px) 100vw, 557px\" \/><\/p>\n<p>Using table 15.3 we construct Table 15.2 which has the reduce action incorporated. This parser however is not a very powerful parser.<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">After constructing the parsing table, the SLR parsing algorithm uses this table, along with a stack to validate a string. The steps involved in the parsing algorithm are given below:<\/p>\n<ul>\n<li>If <em>action<\/em>[<em>s<\/em><em>m<\/em> ,<em>a<\/em><em>i<\/em>] = shift <em>s<\/em>, then push <em>a<\/em><em>i<\/em>, push <em>s<\/em>, and advance input:<\/li>\n<\/ul>\n<p>(<em>s<\/em>0 <em>X<\/em>1 <em>s<\/em>1 <em>X<\/em>2 <em>s<\/em>2 <em>\u2026 X<\/em><em>m<\/em> <em>s<\/em><em>m<\/em> <em>a<\/em><em>i<\/em> <em>s<\/em>,\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <em>a<\/em><em>i<\/em>+1 \u2026<em> a<\/em><em>n<\/em> <strong>$<\/strong>)<\/p>\n<ul>\n<li>If <em>action<\/em>[<em>s<\/em><em>m<\/em> ,<em>a<\/em><em>i<\/em>] = reduce A \u00ae b and <em>goto<\/em>[<em>s<\/em><em>m -r<\/em>,<em>A<\/em>] = <em>s<\/em> with <em>r<\/em>=|b| then pop 2<em>r<\/em> symbols, push <em>A<\/em>, and push <em>s<\/em>:<\/li>\n<\/ul>\n<p>(<em>s<\/em>0 <em>X<\/em>1 <em>s<\/em>1 <em>X<\/em>2 <em>s<\/em>2 <em>\u2026 X<\/em><em>m -r<\/em> <em>s<\/em><em>m-r<\/em> <em>A s<\/em>,\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <em>a<\/em><em>i<\/em><em> a<\/em><em>i<\/em>+1 \u2026<em> a<\/em><em>n<\/em> <strong>$<\/strong>)<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">The configuration of the stack contains alternately state number corresponding to item number and grammar symbol, with the top of the stack being a state number. The input contains a terminal. The top of the stack state is compared with the input and we go for a push action if the action is shift, and would pop if the action is reduce. The algorithm is detailed in the next module with more explanation and example.<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\"><strong>Summary<\/strong>: In this module, we learnt to construct the follow() for the input grammar as it is necessary for the parsing table\u2019s reduce action. We also studied the manner in which the canonical collection of items is used to construct the parsing table\u2019s shift, reduce, accept and error actions. Based on this parsing table, the next module will discuss the parsing action and how to handle the various errors and conflicts of the SLR parser.<\/p>\n","protected":false},"author":4,"menu_order":15,"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-78","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\/78","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\/78\/revisions"}],"predecessor-version":[{"id":82,"href":"https:\/\/ebooks.inflibnet.ac.in\/csp10\/wp-json\/pressbooks\/v2\/chapters\/78\/revisions\/82"}],"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\/78\/metadata\/"}],"wp:attachment":[{"href":"https:\/\/ebooks.inflibnet.ac.in\/csp10\/wp-json\/wp\/v2\/media?parent=78"}],"wp:term":[{"taxonomy":"chapter-type","embeddable":true,"href":"https:\/\/ebooks.inflibnet.ac.in\/csp10\/wp-json\/pressbooks\/v2\/chapter-type?post=78"},{"taxonomy":"contributor","embeddable":true,"href":"https:\/\/ebooks.inflibnet.ac.in\/csp10\/wp-json\/wp\/v2\/contributor?post=78"},{"taxonomy":"license","embeddable":true,"href":"https:\/\/ebooks.inflibnet.ac.in\/csp10\/wp-json\/wp\/v2\/license?post=78"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}