You are on page 1of 8

Compiler Construction

Unit 3 Part-5
LL(1), LR(0). SLR(1)
CSE

By Himanshu Swarnkar
Engineering College Banswara
Parser

Top Down Bottom Up Parser


Parser (BUP)
(TDP) SR Parser(shift Reduce)

TDP with Full TDP Without Operator LR Parser


Backtracking Backtracking Precedence (Scanning from L to R, Reverse
Parser of Right most derivation)

Brute force
Method Recursive Descent Non Recursive LR(0) SLR(1) LNR(1) CLR(1)
Descent(LL(1))
SR and RR Conflict in LR(0) Parser
a A→γ.
A→γ. b c
B→β. a

I5 I6 SR Conflict 5 r1/S6 r1 r1
RR Conflict 6 r1/r2 r1/r2 r1/r2

Note: 1. If a grammar contains SR or RR Conflict than it is not a LR(0) Grammar.


2. RR conflict in SLR(1) Parser if there is intersection in final items in a state.
SLR(1) “Simple LR Parsing”
 SLR (1) refers to simple LR Parsing. It is same as LR(0) parsing. The only difference is in the parsing table.
 To construct SLR (1) parsing table, we use canonical collection of LR (0) item.
 In the SLR (1) parsing, we place the reduce move only in the follow of left hand side.
SLR(1) Parsing Table
 Various steps involved in the SLR (1) Parsing Action Goto
 For the given input string write a context free grammar (for Terminals) (for Variables)
 Check the ambiguity of the grammar
 Add Augment production in the given grammar a b $ A S
 Create Canonical collection of LR (0) items S3
I0 S4 I2 I1
 Draw a data flow diagram (DFA)
 Construct a SLR (1) parsing table I1 Accept

I2 S3 S4 I5
States
I3 S3 S4 I6

I4 r3 r3 r3

I5 r1

I6 r2 r2 r2
In order to check if a grammar is LL(1) , You must make sure that
1.) The grammar is not an ambiguous grammar
2.)The grammar should not be left recursive
3.)The grammar should be deterministic.
4.)Basically the idea is that if you construct the LL(1) parsing table , no cell should have more than one entry
In order to check if a grammar is LR(0) , You must make sure that
1.) Check SR and RR Conflict. If not than the grammar is LR(0).

In order to check if a grammar is SLR(1) , You must make sure that


1.) if RR Conflict in SLR(1) Parser and if there is intersection of RR conflict final items is not ф than grammar is not SLR(1)
Example: 1 LL(1) Example: 2
LL(1) X  Given Grammar is ambiguous so it is
LR(0) Not LL(1)
SLR(1) LR(0) X
SLR(1)X
 Given Grammar contains two reduce
Move at one state so it is not LR(0)

 Given Grammar contains two reduce


With common follow which is $ so it is
Also not SLR(1).
Example: 3 LL(1) X
LR(0) X
SLR(1)

 Give grammar is not LL(1), Because it contains


Left Recursive.
 Give grammar is not LR(0), Because it contains
SR conflict at state I2.

a b + * $
 Follow(E)={+,$}
I2 s4 s2 r2 r2
 Follow(T)={+,$,a,b}
r
I3 r4 r4 r4 S8 4
I7 r3 r3 r3 S8 r3
I9 S4 S5 r1 r1
LL(1) X
Example: 3 Example: 4
LR(0) X
LL(1) X SLR(1)
LR(0)
SLR(1)

LR(0) Parsing Table SLR(1) Parsing Table

You might also like