Open In App

SLR Parser (with Examples)

Last Updated : 30 Jul, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

LR parsers is an efficient bottom-up syntax analysis technique that can be used to parse large classes of context-free grammar is called LR(k) parsing. 
L stands for left-to-right scanning
R stands for rightmost derivation in reverse
k is several input symbols. when k is omitted k is assumed to be 1.

Advantages of LR parsing

  • LR parsers handle context-free grammars. These grammars describe the structure of programming languages-how statements, expressions, and other language constructs fit together.
  • LR parsers ensure that your code adheres to these rules.
  • It is able to detect syntactic errors
  • It is an efficient non-backtracking shift shift-reducing parsing method.

Types of LR Parsing Methods

  • SLR parser
  • LALR parser
  • Canonical LR parser

SLR Parser

  • LR parser is also called as SLR parser
  • it is weakest of the three methods but easier to implement
  • a grammar for which SLR parser can be constructed is called SLR grammar

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.

  • In the figure, I0 consists of augmented grammar.
  • Io goes to I1 when  ' . ' of 0th production is shifted towards the right of S(S'->S.). this state is the accepted state. S is seen by the compiler.
  • Io goes to I2 when  ' . ' of 1st production is shifted towards right (S->A.A) . A is seen by the compiler
  • I0 goes to I3 when  ' . ' of the 2nd production is shifted towards right (A->a.A) . a is seen by the compiler.
  • I0 goes to I4 when  ' . ' of the 3rd production is shifted towards right (A->b.) . b is seen by the compiler.
  • I2 goes to I5 when  ' . ' of 1st production is shifted towards right (S->AA.) . A is seen by the compiler
  • I2 goes to I4 when  ' . ' of 3rd production is shifted towards right (A->b.) . b is seen by the compiler.
  • I2 goes to I3 when  ' . ' of the 2nd production is shifted towards right (A->a.A) . a is seen by the compiler.
  • I3 goes to I4 when  ' . ' of the 3rd production is shifted towards right (A->b.) . b is seen by the compiler.
  • I3 goes to I6 when  ' . ' of 2nd production is shifted towards the right (A->aA.) . A is seen by the compiler
  • I3 goes to I3 when  ' . ' of the 2nd production is shifted towards right (A->a.A) . a is seen by the compiler.

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.

  • $ is by default a nonterminal that takes accepting state.
  • 0,1,2,3,4,5,6 denotes I0,I1,I2,I3,I4,I5,I6
  • I0 gives A in I2, so 2 is added to the A column and 0 rows.
  • I0 gives S in I1,so 1 is added to the S column and 1 row.
  • similarly  5 is written in  A column and 2 row, 6 is written in A column and 3 row.
  • I0 gives a in I3 .so S3(shift 3) is added to a column and 0 row.
  • I0 gives b in I4 .so S4(shift 4) is added to the b column and 0 row.
  • Similarly, S3(shift 3) is added on a column and 2,3 row ,S4(shift 4) is added on b column and 2,3 rows.
  • I4 is reduced state as ' . ' is at the end. I4 is the 3rd production of grammar(A-->.b).LHS of this production is A. FOLLOW(A)=a,b,$  . write r3(reduced 3) in the columns of a,b,$ and 4th row
  • I5 is reduced state as ' . ' is at the end. I5 is the 1st production of grammar(S-->.AA). LHS of this production is S.
    FOLLOW(S)=$  . write r1(reduced 1) in the column of $ and 5th row
  • I6 is a reduced state as ' . ' is at the end. I6 is the 2nd production of grammar( A-->.aA). The LHS of this production is A.
    FOLLOW(A)=a,b,$  . write r2(reduced 2) in the columns of a,b,$ and 6th row

APPLICATIONS GALORE:

  • Compiler
  • Data Validation
  • Natural Language Processing(NLP)
  • Protocol Parsing

Next Article
Article Tags :

Similar Reads