0% found this document useful (0 votes)
83 views18 pages

09 Syntax Analysis SR Conflicts and RR Conflicts in LR Parsing

Uploaded by

Ahsanullah
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
83 views18 pages

09 Syntax Analysis SR Conflicts and RR Conflicts in LR Parsing

Uploaded by

Ahsanullah
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 18

Shift Reduce and Reduce

Conflicts in LR Parsing

Syntax Analysis

Faryal Shamsi
Department of Computer Science
Sukkur IBA University
LR(0) , SLR(1) conflicts
LR(0) , SLR(1) conflicts

• All grammars are not suitable for LR(0), SLR(1) parser because it has
two types of conflicts
• SR conflict
• RR conflict
• When these conflicts will occur in both type of parsers, we have
examples as follows.
• Both conflict(SR,RR) are always happening with reduce moves. i.e R is
common in both conflicts.
• So that we always see canonical item’s reduce move i.e final items.
• So see reduce move like I1 that is due to
augment form.
• See I4
• It has one final item (i.e reduce move
• It has no shift move
• So no any conflict
• See I9
• It has one final item (i.e reduce move
• It has no shift move
• So no any conflict
Shift Reduce (SR) conflict in LR(0)
• Suppose we have canonical items like
I5
I6
a
SA .

• I5 contains final item and


• on I5 terminal a goes to I6
• Put s6 in table
• Let us see the table
• Because SA. is final item, I might have to put reduce move r1 in the
entire row.
• This is called Shift Reduce (SR) conflict
• So all the languages are not suitable for LR(0) parsing due to SR conflict.
Reduce Reduce(RR) conflict in LR(0)
• Suppose we have canonical items like
I5
I6
SA . 1 a
TB. 2

• For the first production in row 5 I have to put r1 under all productions
and
• Similarly for the second production I have to put r2 under all
productions in the same row 5.
• This is called RR conflict
• We cant have two entries in single cell
• So all the languages are not suitable for LR(0) parsing due to SR and RR
conflict.
Shift Reduce (SR) conflict in LR(1)
• Suppose we have canonical items like
I5
I6
a
SA .

• Find the FOLOW of A


• In case if Follow(A) ={a} and
• on I5 terminal a goes to I6 can make double entries in a cell of the
table.
• Then we have RR conflict
Reduce Reduce(RR) conflict in LR(1)

• Suppose we have canonical items like


I5
I6
SA . 1 a
TB. 2

• Find the FOLLOW of S and T


• In case if FOLLOW(S) ∩ FOLLOW(T) ≠ ф
• It means it both production have any common canonical item except
phi, it will create double entries in a cell of table called RR conflict.
• We cant place two entries in single cell
• So all the languages are not suitable for LR(1) parsing due to SR and
RR conflict.
Example SR and RR
Conflict

• A  A+B | B • A -> B + A
• B  B*C | C • A -> id
• C  id • B -> id
Example

• Whether the following grammar is suitable for


• LL(1)
• LR(0) or
• SLR(1)/LR(1)/LR
• Example: Suppose grammar is:
• SdA|aB
• AbA|c
• BbB|c
Example: Is LL(1)?

• Whether the following grammar is suitable for LL(1)


• Example: Suppose grammar is:
• SdA|aB
• AbA|c
• BbB|c
• SdA|aB in parsing table it is placed in row S under column d and a
• AbA|c in parsing table it is placed in row A under column b and c
• BbB|c in parsing table it is placed in row B under column b and c

• So we have no double entries, no conflicts


• It is a suitable grammar for LL(0) parsing
Example: Is LR(0) or SLR(1)?

• Whether the following grammar is suitable for LR(0) and SLR(1)


parsing?
• Example: Suppose grammar is:
• SdA|aB
• AbA|c
• BbB|c
• No need to find conflicts by creating parsing tables for both
type of parsers
• Create only canonical collection of items.
• Two types of conflict SR and RR are due to reduce moves.
• Reduce moves means we have to check conflicts in only final
items.
Example: Is LR(0) or SLR(1)?
• Whether the following grammar is suitable for LR(0) and SLR(1) parsing?
• In the following items I0 does not contain closure of A and B
productions because there is no need of closure in .dA and .aB
Example: Is LR(0) or SLR(1)?

• See I1: Its not final item it Is augmented form that We have added
• See I9: It’s a final item (DOT is on right most position)
• First there is only one reduce move so no RR conflicts
• Second no GOTO/shift moves so no SR conflicts.
• Same as I9 we have all others cases like
• I4
• I6
• I7
• I10
• I11

• S0 this grammar is suitable for LR(0) grammar


• So if a grammar is suitable for LR(0) than its also suitable for SLR(1)
because reducing reduce moves conflict do no arise.
CW

• E  T+E | T
• T  id

• Check if the grammar is :


• LL1
• LR(0)
• LR(1)
• Operator Grammar
CW

• S L=R|R
• L *R|id
• R L

• Check if the grammar is :


• LL1
• LR(0)
• LR(1)
• Operator Grammar
C.W

• S L=R|R
• L *R|id
• R L

• Find LL(1), LR0,LR1 parsing tables

• HINT:
• 0 to 9 ten states
• SR conflict in both LR(0) and LR(1)
Example:

You might also like