Skip to content
Related Articles

Related Articles

Improve Article

Problem on LR(0) parser

  • Last Updated : 23 Jun, 2021

Prerequisite : LR Parser.

The LR parser is an efficient bottom up syntax analysis technique that can be used to large class of context-free grammar.This technique is also called LR(0) parsing.
L stands for left to right scanning
R stands for rightmost  derivation in reverese
0 stands for no. of input symbols of lookahead.

Augmented grammar :
If P is a grammar with starting symbol S,then G’ (augmented grammar for G) is a grammar with a new starting symbol S and productions  S-> .S. The purpose of this new starting production is to indicate the parser when it should stop parsing. The ‘ . ‘ before S indicates the left side of ‘ . ‘  has been read by a compiler and the right side of ‘ . ‘ is yet to be read by a compiler.

Steps for constructing the LR parsing table :

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

Q. Construct a LR parsing table for the given context free grammar –



      S–>AA
      A–>aA|b

Solution :
STEP 1- 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]

STEP 2 –  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 accept 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 – defining 2 functions:goto[list of terminals] and action[list of non-terminals] in the parsing table

  • $ is by default a non terminal which 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 row.
  • I0 gives S in I1,so 1 is added to the S column and 1 row.
  • similarly  5 is written in  A column and 2nd row, 6 is written in A column and 3 row.
  • I0 gives a in I3 to .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 rows ,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. So write r3(reduce 3) in terminals.
  • I5 is reduced state as ‘ . ‘ is at the end. I5 is the 1st production of grammar. So write r1(reduce 1) in terminals.
  • I6 is reduced state as ‘ . ‘ is at the end. I6 is the 2nd production of grammar. So write r2(reduce 2) in terminals.

As each cell has only 1 value in it, hence,the given grammar is LR(0).

Attention reader! Don’t stop learning now.  Practice GATE exam well before the actual exam with the subject-wise and overall quizzes available in GATE Test Series Course.

Learn all GATE CS concepts with Free Live Classes on our youtube channel.

My Personal Notes arrow_drop_up
Recommended Articles
Page :