# Verifying whether the string id * id + id is accepted by a given grammar using SLR parsing Consider the SLR parsing table for the Grammar E → E + T E → T T → T ∗ F T → F F → (E) F → id Check whether the string id * id + id is accepted or not by using the SLR parsing table constructed in the example.

Compiler DesignProgramming LanguagesComputer Programming

#### Web Design for Beginners: Build Websites in HTML & CSS 2022

68 Lectures 8 hours

#### HTML5 & CSS3 Site Design

61 Lectures 6 hours

## Solution

Initially, LR Parser in state 0.

Put $at the end of the string, i.e., id * id + id$.

StackInput StringReason
0id ∗ id + idAction [0, id] = s5 ∴ Shift id and state 5
0 id 5∗ id + id $Action [5,∗] = r6. ∴ Reduce by F → id. goto(0, F) = 3 0 F 3∗ id + id$Action [3,∗] = r4, Reduce by T → F goto(0, T) = 2
0 T 2∗ id + id $Action [2,∗] = s7, shift ∗, 7 0T2*7id + id$Action [7, id] = s5, shift id, 5
0T2*7 id 5+id $Action [5, +] = r6, Reduce by F → id goto(7, F) = 10 0T2*7 F 10+id$Action [10, +] = r3S, Reduce by T → T ∗ F, goto(0, T) = 2
0 T 2+id $Action [2, +] = r2, Reduce by E → T goto(0, E) = 1 0 E 1+id$Action [1, +] = s6, Shift +, 6
0 E 1 + 6id $Action [6, id] = s5, Shift id, 5. 0 E 1 + 6 id 5$Action [5, $] = s6, Reduce by F → id, goto(6, F) = 3 0 E 1 + 6 F 3$Action [3, $] = r4, Reduce by T → F, goto(6, F) = 9 0 E 1 + 6 T 9$Action [9, $] = r1, Reduce by E → E + T, goto(0, E) = 1 0 E 1$Action [1, $] = accept In the first row, we have 0 at the top of the stack, and the id symbol is to be processed. So, check Row (0) and column (id) i.e., Action [0, id] = s5, i.e., Shift id from string and state 5 onto stack. In Second Row, Action [5, *] = r3 means reduce by production number 6 i.e., F → id. Pop id from the stack and push F. Then see goto (0, F) = 3, so push 3 onto Stack. In Row seven, Action [10, +] = r3 i.e., reduce by production number 3 i.e., T → T ∗ F. Pop T2 * 7F10 from the stack, push T into the stack. Then see goto [0, T] = 2. So, push 2 onto stack In Last Row Action [1,$] = accept. That means the parser will accept the string.

Algorithm of SLR Parsing Table

There are two parts of the SLR Parsing table

• Action
• goto

Action and goto table can be filled using the following Algorithm −

Algorithm

Input− An Augmented Grammar G′

Output− SLR Parsing Table

Method

• Initially construct a set of items

C = {I0, I1, I2 … … In} where C is a set of LR (0) items for Grammar.

• Parsing actions are based on each item or state I1.

Various Actions are −

• If A → α ∙ a β is in Ii and goto (Ii, a) = Ij then set Action [i, a] = shift j".
• If A → α ∙ is in Ii then set Action [i, a] to "reduce A → α" for all symbol a, where a ∈ FOLLOW (A).
• If S′ → S ∙ is in Ii then the entry in action table Action [i, \$] = accept".
• The goto part of the SLR table can be filled as− The goto transition for the state i is considered for non-terminals only. If goto (Ii, A) = Ij then goto [i, A] = j
• All entries not defined by rules 2 and 3 are considered to be "error. "