Open In App

Problem on LR(0) parser

Prerequisite: LR Parser.

The LR parser is an efficient bottom-up syntax analysis technique that can be used for a large class of context-free grammar. This technique is also 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.



Augmented grammar :
If G 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 to 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 non-terminals) and action(list of terminals) in the parsing table.

Q. Construct an 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 nonterminal 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 – defining 2 functions: goto[list of non-terminals] and action[list of terminals] in the parsing table

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

Article Tags :