Open In App

SLR Parser (with Examples)

Prerequisite: LR Parser

LR parsers :
It is an efficient bottom-up syntax analysis technique that can be used to parse large classes of context free grammar is called LR(0) parsing. 
L stands for the left to right scanning
R stands for rightmost  derivation in reverse
0 stands for no. of input symbols of lookahead
 



Advantages of LR parsing :

Types of LR parsing methods :



  1. SLR
  2. CLR
  3. LALR

SLR Parser :
SLR is simple LR. It is the smallest class of grammar having few number of states. SLR is very easy to construct and is similar to LR parsing. The only difference between SLR parser and LR(0) parser is that in LR(0) parsing table, there’s a chance of ‘shift reduced’ conflict because we are entering ‘reduce’ corresponding to all terminal states. We can solve this problem by entering ‘reduce’ corresponding to FOLLOW of LHS of production in the terminating state. This is called SLR(1) collection of items

Steps for constructing the SLR parsing table :

  1. Writing augmented grammar
  2. LR(0) collection of items to be found
  3. Find FOLLOW of LHS of production
  4. Defining 2 functions:goto[list of terminals] and action[list of non-terminals] in the parsing table

EXAMPLE – Construct LR parsing table for the given context-free grammar

 S–>AA   
 A–>aA|b

Solution:

STEP1 – Find augmented grammar
The augmented grammar of the given grammar is:-

 S’–>.S    [0th production]    
 S–>.AA  [1st production]     
 A–>.aA [2nd production]      
 A–>.b  [3rd production]

STEP2 – Find LR(0) collection of items
Below is the figure showing the LR(0) collection of items. We will understand everything one by one.

The terminals of this grammar are {a,b}.
The non-terminals of this grammar are {S,A}

RULE – 
If any non-terminal has ‘ . ‘ preceding it, we have to write all its production and add ‘ . ‘ preceding each of its production.

RULE – 
from each state to the next state, the ‘ . ‘ shifts to one place to the right.

STEP3 – 
Find FOLLOW of LHS of production

FOLLOW(S)=$
FOLLOW(A)=a,b,$

To find FOLLOW of non-terminals, please read follow set in syntax analysis.

STEP 4- 
Defining 2 functions:goto[list of non-terminals] and action[list of terminals] in the parsing table. Below is the SLR parsing table.

Article Tags :