CLR Parser (with Examples) (original) (raw)

Last Updated : 29 Oct, 2022

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(k) parsing.
L stands for the left to right scanning
R stands for rightmost derivation in reverse
k stands for no. of input symbols of lookahead

Advantages of LR parsing :

Types of LR parsing methods :

  1. SLR
  2. CLR
  3. LALR

CLR Parser :
The CLR parser stands for canonical LR parser.It is a more powerful LR parser.It makes use of lookahead symbols. This method uses a large set of items called LR(1) items.The main difference between LR(0) and LR(1) items is that, in LR(1) items, it is possible to carry more information in a state, which will rule out useless reduction states.This extra information is incorporated into the state by the lookahead symbol. The general syntax becomes [A->∝.B, a ]
where A->∝.B is the production and a is a terminal or right end marker $
LR(1) items=LR(0) items + look ahead

How to add lookahead with the production?
CASE 1 -

A->∝.BC, a

Suppose this is the 0th production.Now, since ' . ' precedes B,so we have to write B's productions as well.

B->.D [1st production]

Suppose this is B's production. The look ahead of this production is given as we look at previous productions ie 0th production. Whatever is after B, we find FIRST(of that value) , that is the lookahead of 1st production.So,here in 0th production, after B, C is there. assume FIRST(C)=d, then 1st production become

B->.D, d

CASE 2 -
Now if the 0th production was like this,

A->∝.B, a

Here, we can see there's nothing after B. So the lookahead of 0th production will be the lookahead of 1st production. ie-

B->.D, a

CASE 3 -
Assume a production A->a|b

A->a,$ [0th production] A->b,$ [1st production]

Here, the 1st production is a part of the previous production, so the lookahead will be the same as that of its previous production.
These are the 2 rules of look ahead.

Steps for constructing CLR parsing table :

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

EXAMPLE
Construct a CLR 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 ,a|b [2nd production]
A-->.b ,a|b [3rd production]

Let's apply the rule of lookahead to the above productions

STEP 2 - Find LR(1) collection of items
Below is the figure showing the LR(1) 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-

  1. If any non-terminal has ' . ' preceding it, we have to write all its production and add ' . ' preceding each of its production.
  2. from each state to the next state, the ' . ' shifts to one place to the right.
  3. All the rules of lookahead apply here.

STEP 3- defining 2 functions:goto[list of terminals] and action[list of non-terminals] in the parsing table.Below is the CLR parsing table