CLR and Lalr
CLR and Lalr
Steps:
1. create augment grammar
2. generate kernel items
3. find closure
4. compute goto()
5. construct parsing table
6. parse the string
S->L=R
S->R
L->*R
L->id
R->L
• Step :1 Create augment grammar
S is start symbol in the given grammar
Augment grammar is S’-> S
.
S’-> S
• Step :3 Find closure
(Rule: A -> α.Xβ i.eS’->
if there
.S is nonterminal next topresence
Since dot then Include X production))
of S (nonterminal)next to dot, introduce S production
L-> . *R
L-> . Id
R-> . L
CLR PARSER
Steps:
1. create augment grammar
2. generate kernel items and add 2nd component
3. find closure
4. compute goto()
5. construct parsing table
Rule to find 2nd component:
6. parse the string
Consider the production of the form : A-> α .Bβ , a
Let us consider grammar: THEN 2nd component of B is : β , if it is terminal
First (β ) if it is non terminal
S->L=R a, if there is no β
S->R
L->*R
L->id
R->L
• Step :1 Create augment grammar
S is start symbol in the given grammar
Augment grammar is S’-> S
.
S’-> S , $
• Step :3 Find closure
(Rule: A -> α.Xβ i.e if thereS’-> .S , $
is nonterminal next to dot then Include X production))
S-> . L=R
S-> . R I0
L-> . *R
L-> . Id
R-> . L
S’-> .S , $ Next find 2nd component:
S-> . L=R, $ I0 compare each of the production with A-> α .Bβ , a
S-> . R, $ S’ -> . s, $ here no β, so $ is 2nd comp to S
L-> . *R, =/$
L-> . Id,=/$ S -> . L=R,$ here β is = so add it as 2nd comp to L
L
*
I5’
I4’
I5
id
R *
*
I3
I5’
To construct: LALR PARSER
We notice that some states in CLR parser have the same core items and differ only in
possible lookahead symbols.
Such as
I4 and I4’
I5 and I5’
I7 and I7’
I8 and I8’
So we shrink the obtained CLR parser by merging such states to form LALR Parser
Hence