SlideShare ist ein Scribd-Unternehmen logo
1 von 32
Presentation On
“SR Parsing and SLR Basics”
1
2
SR PARSING
Shift-Reduce (SR) parsing
‱ A shift-reduce parser is a class of efficient, table-driven bottom-
up parsing methods for computer languages and other
notations formally defined by a grammar.
3
4
‱ The parsing methods most commonly used for parsing
programming languages, LR parsing and its variations, are
shift-reduce methods.
‱ All shift-reduce parsers have similar outward effects, in the
incremental order in which they build a parse tree or call
specific output actions
Shift-Reduce (SR) parsing
Basic steps
A shift-reduce parser works by doing some combination of Shift steps
and Reduce steps, hence the name.
A Shift step advances in the input stream by one symbol. That shifted symbol
becomes a new single-node parse tree.
A Reduce step applies a completed grammar rule to some of the recent parse
trees, joining them together as one tree with a new root symbol.
5
Actions of SR parsing
A shift-reduce parser has just four canonical actions:
 Shift  This involves moving of symbols from input buffer onto the stack.
 Reduce  If the handle appears on top of the stack then, its reduction by
using appropriate production rule is done i.e. RHS of production rule is
popped out of stack and LHS of production rule is pushed onto the stack.
6
Cont 

 Accept  If only start symbol is present in the stack and the input buffer is
empty then, the parsing action is called accept. When accept action is
obtained, it is means successful parsing is done.
 Error  This is the situation in which the parser can neither perform shift
action nor reduce action and not even accept action.
7
Conflicts
There are two kinds of conflicts that can occur in an SR parsing table.
 A shift-reduce conflict occurs in a state that requests both a shift action and a
reduce action.
 A reduce-reduce conflict occurs in a state that requests two or more different
reduce actions.
8
Example
Consider the grammar
S –> S + S
S –> S * S
S –> id
Perform Shift Reduce parsing for input string “id + id + id”.
9
Parsing Table
10
11
SLR BASICS
LR Parser
The LR parser is a
‱ non-recursive
‱ shift-reduce
‱ bottom-up parser
It uses a wide class of context-free grammar which makes it the most
efficient syntax analysis technique.
12
13
LR parsers are also known as LR(k) parsers Where
o L = left-to-right scanning of the input stream;
o R = the construction of right-most derivation in reverse
o and k denotes the number of look-ahead symbols to make decisions.
14
Types of LR parser
2.LR(1) – Simple LR Parser(SLR)
3.LALR(1) – Look-Ahead LR Parser
1.LR(0) parser
4.Canonical LR (1) PARSER
SLR(1) – Simple LR Parser:
15
– Works on complete set of LR(1) Grammar
– Generates large table and large number of states
– Slow construction
SLR(1) parser
‱ 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.
16
Item of LR(0)
‱ An LR(0) item of a grammar G is a production of G, a dot at the some
position of the right side.
Example:
A → aBb
Possible LR(0) Items:
A →.aBb
A → a.Bb
A → aB.b
A → aBb.
(four different possibility)
17
Cont.

‱ Sets of LR(0) items will be the states of ‘action’ and ‘goto’ table of the
SLR parser.
‱ A collection of sets of LR(0) items (the canonical LR(0) collection) is
the basis for constructing SLR parsers.
18
19
Augmented Grammar:
G’ is G with a new production rule E’→E where E’ is the new starting symbol
EXAMPLE
E’ → E
E  E+T
E  T
T  T*F
T  F
F  ( E )
F  id
Closure Operation
‱ If I is a set of LR(0) items for a grammar G, then closure(I) is the set of
LR(0) items constructed from I by the two rules:
1. Initially, every LR(0) item in I is added to closure(I).
2. If A → α.BÎČ is in closure(I) and B→γ is a production rule of G;
then B→.γ will be in the closure(I).
We will apply this rule until no more new LR(0) items can be
add d t l (I) ed to closure(I).
20
Example
Grammar : I= {E’ →.E}
E’ → E
E  E+T
E  T
T  T*F
T  F
F  ( E )
F  id
closure({E’ →.E}) =
E’ →.E
E →.E+T
E →.T
T →.T*F
T →.F
F →.(E)
F →.id
21
Goto Function
If I is a set of items and X ∈ (VnU VT ), then, goto(I, X) is the closure of
the set of all items A → αX. ÎČ such that A → α. XÎČ is in I.
If I is a set of items valid for a prefix α of a right-sentential form,
then, Goto(I, X) is valid for the prefix αX
22
23
Goto(I, X) represents the transition of the automaton
from state I and input X.
GOTO(I=state ,X=input)
24
Grammar :
Eâ€Č → E
E → E + T
E → T
T → T ∗ F
T → F
F → (E)
F → id
I2
E → E+.T
T → .T ∗ F
T → . F
F → .(E)
F → . id
I0
E →.E+T
E →.T
T →.T*F
T →.F
F →.(E)
F →.id
I1
Eâ€Č → E. ,
E → E. +T
Example
GOTO(I0,E) GOTO(I1,+)
Example
If I1 = {Eâ€Č → E. , E → E. +T}, then:
goto(I1,+) = closure({E → E+.T}),
is the set:
E → E+.T
T → .T ∗ F
T → . F
F → .(E)
F → . id
Grammar :
Eâ€Č → E
E → E + T
E → T
T → T ∗ F
T → F
F → (E)
F → id
25
Canonical Collection
Algorithm. Canonical Collection for an Augmented Grammar Gâ€Č
1. Initially, C = {closure({Sâ€Č →. S})};
2. For each set of items I in C and each Grammar symbol X
If goto(I, X) ≠ ∅ and Goto(I, X) /∈ C, then add Goto(I, X) to C;
3. Go to step 2 if new items have been added, otherwise stop
26
Example
Grammer :
S → E
E → E + T | T
T → T * F | F
F → id
27
S’  .E
E  .E+T
E  .T
T  .T*F
T  .F
F  .id
S’  E.
E  E.+T
E  E+.T
T  .T*F
T  .F
F  .id
E  T.
T  T.*F
T  T*.F
F  .id
E  E+T.
T  T.*f
T  T*F.
T  F.
F  id.
Canonical Collection
28
E
T
F
id
+
*
T
*
F
id
id
F
I0
I1
I5
I7
I8
I6
I4
I2
I3
Table Construction Rules
α, ÎČ = any string of terminals and/or non‐terminals
X, S’, S = non‐terminals
(When dot is in middle)
1. if [A ‐‐> α.aÎČ] Δ Ii and read on ‘a’ produces Ij then ACTION [i , a] = SHIFT j.
2. if [A ‐‐> α.XÎČ] Δ Ii and read on ‘X’ produces Ij then GOTO [i , X] = j.
29
Cont..
(When dot is at end)
3. if [A ‐‐> α.] Δ Ii then ACTION [i , a] = REDUCE on A ‐> α for all a Δ FOLLOW(A).
4. if [S’ ‐‐> S.] Δ Ii then ACTION [i , $] = ACCEPT.
30
Parsing Table
31
32
Thanks to all

Weitere Àhnliche Inhalte

Was ist angesagt?

Intermediate code generation (Compiler Design)
Intermediate code generation (Compiler Design)   Intermediate code generation (Compiler Design)
Intermediate code generation (Compiler Design) Tasif Tanzim
 
Parsing LL(1), SLR, LR(1)
Parsing LL(1), SLR, LR(1)Parsing LL(1), SLR, LR(1)
Parsing LL(1), SLR, LR(1)Nitin Mohan Sharma
 
Lexical analyzer generator lex
Lexical analyzer generator lexLexical analyzer generator lex
Lexical analyzer generator lexAnusuya123
 
Control Strategies in AI
Control Strategies in AI Control Strategies in AI
Control Strategies in AI Bharat Bhushan
 
Syntax directed translation
Syntax directed translationSyntax directed translation
Syntax directed translationAkshaya Arunan
 
Compiler design syntax analysis
Compiler design syntax analysisCompiler design syntax analysis
Compiler design syntax analysisRicha Sharma
 
AES-Advanced Encryption Standard
AES-Advanced Encryption StandardAES-Advanced Encryption Standard
AES-Advanced Encryption StandardPrince Rachit
 
Top down parsing
Top down parsingTop down parsing
Top down parsingLakshmiSamivel
 
Shift reduce parser
Shift reduce parserShift reduce parser
Shift reduce parserTEJVEER SINGH
 
Bottom up parser
Bottom up parserBottom up parser
Bottom up parserAkshaya Arunan
 
Chapter 5 Syntax Directed Translation
Chapter 5   Syntax Directed TranslationChapter 5   Syntax Directed Translation
Chapter 5 Syntax Directed TranslationRadhakrishnan Chinnusamy
 
LALR Parser Presentation ppt
LALR Parser Presentation pptLALR Parser Presentation ppt
LALR Parser Presentation pptWPVKP.COM
 
Predictive parser
Predictive parserPredictive parser
Predictive parserJothi Lakshmi
 

Was ist angesagt? (20)

Intermediate code generation (Compiler Design)
Intermediate code generation (Compiler Design)   Intermediate code generation (Compiler Design)
Intermediate code generation (Compiler Design)
 
Top down parsing
Top down parsingTop down parsing
Top down parsing
 
Parsing LL(1), SLR, LR(1)
Parsing LL(1), SLR, LR(1)Parsing LL(1), SLR, LR(1)
Parsing LL(1), SLR, LR(1)
 
Input-Buffering
Input-BufferingInput-Buffering
Input-Buffering
 
Lexical analyzer generator lex
Lexical analyzer generator lexLexical analyzer generator lex
Lexical analyzer generator lex
 
LR Parsing
LR ParsingLR Parsing
LR Parsing
 
CLR AND LALR PARSER
CLR AND LALR PARSERCLR AND LALR PARSER
CLR AND LALR PARSER
 
Control Strategies in AI
Control Strategies in AI Control Strategies in AI
Control Strategies in AI
 
Syntax directed translation
Syntax directed translationSyntax directed translation
Syntax directed translation
 
Compiler design syntax analysis
Compiler design syntax analysisCompiler design syntax analysis
Compiler design syntax analysis
 
AES-Advanced Encryption Standard
AES-Advanced Encryption StandardAES-Advanced Encryption Standard
AES-Advanced Encryption Standard
 
Top down parsing
Top down parsingTop down parsing
Top down parsing
 
COMPILER DESIGN- Syntax Directed Translation
COMPILER DESIGN- Syntax Directed TranslationCOMPILER DESIGN- Syntax Directed Translation
COMPILER DESIGN- Syntax Directed Translation
 
Greedy Algorihm
Greedy AlgorihmGreedy Algorihm
Greedy Algorihm
 
Shift reduce parser
Shift reduce parserShift reduce parser
Shift reduce parser
 
Bottom up parser
Bottom up parserBottom up parser
Bottom up parser
 
Chapter 5 Syntax Directed Translation
Chapter 5   Syntax Directed TranslationChapter 5   Syntax Directed Translation
Chapter 5 Syntax Directed Translation
 
LALR Parser Presentation ppt
LALR Parser Presentation pptLALR Parser Presentation ppt
LALR Parser Presentation ppt
 
Specification-of-tokens
Specification-of-tokensSpecification-of-tokens
Specification-of-tokens
 
Predictive parser
Predictive parserPredictive parser
Predictive parser
 

Ähnlich wie LR(1) and SLR(1) parsing

Ähnlich wie LR(1) and SLR(1) parsing (20)

Lecture11 syntax analysis_7
Lecture11 syntax analysis_7Lecture11 syntax analysis_7
Lecture11 syntax analysis_7
 
LR-Parsing.ppt
LR-Parsing.pptLR-Parsing.ppt
LR-Parsing.ppt
 
Bottomupparser
BottomupparserBottomupparser
Bottomupparser
 
Bottomupparser
BottomupparserBottomupparser
Bottomupparser
 
Bottomupparser
BottomupparserBottomupparser
Bottomupparser
 
11-SLR input string parsing, CLR introduction-06-06-2023.docx
11-SLR input string parsing, CLR introduction-06-06-2023.docx11-SLR input string parsing, CLR introduction-06-06-2023.docx
11-SLR input string parsing, CLR introduction-06-06-2023.docx
 
Cd2 [autosaved]
Cd2 [autosaved]Cd2 [autosaved]
Cd2 [autosaved]
 
COMPILER DESIGN- Syntax Analysis
COMPILER DESIGN- Syntax AnalysisCOMPILER DESIGN- Syntax Analysis
COMPILER DESIGN- Syntax Analysis
 
Ch5b.pdf
Ch5b.pdfCh5b.pdf
Ch5b.pdf
 
lr parsers bottom up parsers slr parser.pptx
lr parsers bottom up parsers slr parser.pptxlr parsers bottom up parsers slr parser.pptx
lr parsers bottom up parsers slr parser.pptx
 
Lecture 15 16
Lecture 15 16Lecture 15 16
Lecture 15 16
 
Linear Data Structures_SSD.pdf
Linear Data Structures_SSD.pdfLinear Data Structures_SSD.pdf
Linear Data Structures_SSD.pdf
 
Syntactic analysis in NLP
Syntactic analysis in NLPSyntactic analysis in NLP
Syntactic analysis in NLP
 
Assignment7
Assignment7Assignment7
Assignment7
 
Theory of automata and formal language lab manual
Theory of automata and formal language lab manualTheory of automata and formal language lab manual
Theory of automata and formal language lab manual
 
PARSING.ppt
PARSING.pptPARSING.ppt
PARSING.ppt
 
Ch06
Ch06Ch06
Ch06
 
sameermlr0parser-200701133032.pptx
sameermlr0parser-200701133032.pptxsameermlr0parser-200701133032.pptx
sameermlr0parser-200701133032.pptx
 
Compiler Design Unit 2
Compiler Design Unit 2Compiler Design Unit 2
Compiler Design Unit 2
 
Compiler Design Bottom Up Parsing Technique S
Compiler Design Bottom Up Parsing Technique SCompiler Design Bottom Up Parsing Technique S
Compiler Design Bottom Up Parsing Technique S
 

KĂŒrzlich hochgeladen

BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdfSoniaTolstoy
 
Student login on Anyboli platform.helpin
Student login on Anyboli platform.helpinStudent login on Anyboli platform.helpin
Student login on Anyboli platform.helpinRaunakKeshri1
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104misteraugie
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13Steve Thomason
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformChameera Dedduwage
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfsanyamsingh5019
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Sapana Sha
 
Russian Call Girls in Andheri Airport Mumbai WhatsApp 9167673311 💞 Full Nigh...
Russian Call Girls in Andheri Airport Mumbai WhatsApp  9167673311 💞 Full Nigh...Russian Call Girls in Andheri Airport Mumbai WhatsApp  9167673311 💞 Full Nigh...
Russian Call Girls in Andheri Airport Mumbai WhatsApp 9167673311 💞 Full Nigh...Pooja Nehwal
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxGaneshChakor2
 
APM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAPM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAssociation for Project Management
 
Separation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesSeparation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesFatimaKhan178732
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactdawncurless
 
Advance Mobile Application Development class 07
Advance Mobile Application Development class 07Advance Mobile Application Development class 07
Advance Mobile Application Development class 07Dr. Mazin Mohamed alkathiri
 
Arihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfArihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfchloefrazer622
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactPECB
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)eniolaolutunde
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsTechSoup
 
Disha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdfDisha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdfchloefrazer622
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphThiyagu K
 

KĂŒrzlich hochgeladen (20)

BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
 
Student login on Anyboli platform.helpin
Student login on Anyboli platform.helpinStudent login on Anyboli platform.helpin
Student login on Anyboli platform.helpin
 
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptxINDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy Reform
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdf
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
 
Russian Call Girls in Andheri Airport Mumbai WhatsApp 9167673311 💞 Full Nigh...
Russian Call Girls in Andheri Airport Mumbai WhatsApp  9167673311 💞 Full Nigh...Russian Call Girls in Andheri Airport Mumbai WhatsApp  9167673311 💞 Full Nigh...
Russian Call Girls in Andheri Airport Mumbai WhatsApp 9167673311 💞 Full Nigh...
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptx
 
APM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAPM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across Sectors
 
Separation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesSeparation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and Actinides
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impact
 
Advance Mobile Application Development class 07
Advance Mobile Application Development class 07Advance Mobile Application Development class 07
Advance Mobile Application Development class 07
 
Arihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfArihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdf
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global Impact
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The Basics
 
Disha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdfDisha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdf
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot Graph
 

LR(1) and SLR(1) parsing

  • 1. Presentation On “SR Parsing and SLR Basics” 1
  • 3. Shift-Reduce (SR) parsing ‱ A shift-reduce parser is a class of efficient, table-driven bottom- up parsing methods for computer languages and other notations formally defined by a grammar. 3
  • 4. 4 ‱ The parsing methods most commonly used for parsing programming languages, LR parsing and its variations, are shift-reduce methods. ‱ All shift-reduce parsers have similar outward effects, in the incremental order in which they build a parse tree or call specific output actions Shift-Reduce (SR) parsing
  • 5. Basic steps A shift-reduce parser works by doing some combination of Shift steps and Reduce steps, hence the name. A Shift step advances in the input stream by one symbol. That shifted symbol becomes a new single-node parse tree. A Reduce step applies a completed grammar rule to some of the recent parse trees, joining them together as one tree with a new root symbol. 5
  • 6. Actions of SR parsing A shift-reduce parser has just four canonical actions:  Shift  This involves moving of symbols from input buffer onto the stack.  Reduce  If the handle appears on top of the stack then, its reduction by using appropriate production rule is done i.e. RHS of production rule is popped out of stack and LHS of production rule is pushed onto the stack. 6
  • 7. Cont 
  Accept  If only start symbol is present in the stack and the input buffer is empty then, the parsing action is called accept. When accept action is obtained, it is means successful parsing is done.  Error  This is the situation in which the parser can neither perform shift action nor reduce action and not even accept action. 7
  • 8. Conflicts There are two kinds of conflicts that can occur in an SR parsing table.  A shift-reduce conflict occurs in a state that requests both a shift action and a reduce action.  A reduce-reduce conflict occurs in a state that requests two or more different reduce actions. 8
  • 9. Example Consider the grammar S –> S + S S –> S * S S –> id Perform Shift Reduce parsing for input string “id + id + id”. 9
  • 12. LR Parser The LR parser is a ‱ non-recursive ‱ shift-reduce ‱ bottom-up parser It uses a wide class of context-free grammar which makes it the most efficient syntax analysis technique. 12
  • 13. 13 LR parsers are also known as LR(k) parsers Where o L = left-to-right scanning of the input stream; o R = the construction of right-most derivation in reverse o and k denotes the number of look-ahead symbols to make decisions.
  • 14. 14 Types of LR parser 2.LR(1) – Simple LR Parser(SLR) 3.LALR(1) – Look-Ahead LR Parser 1.LR(0) parser 4.Canonical LR (1) PARSER
  • 15. SLR(1) – Simple LR Parser: 15 – Works on complete set of LR(1) Grammar – Generates large table and large number of states – Slow construction
  • 16. SLR(1) parser ‱ 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. 16
  • 17. Item of LR(0) ‱ An LR(0) item of a grammar G is a production of G, a dot at the some position of the right side. Example: A → aBb Possible LR(0) Items: A →.aBb A → a.Bb A → aB.b A → aBb. (four different possibility) 17
  • 18. Cont.
 ‱ Sets of LR(0) items will be the states of ‘action’ and ‘goto’ table of the SLR parser. ‱ A collection of sets of LR(0) items (the canonical LR(0) collection) is the basis for constructing SLR parsers. 18
  • 19. 19 Augmented Grammar: G’ is G with a new production rule E’→E where E’ is the new starting symbol EXAMPLE E’ → E E  E+T E  T T  T*F T  F F  ( E ) F  id
  • 20. Closure Operation ‱ If I is a set of LR(0) items for a grammar G, then closure(I) is the set of LR(0) items constructed from I by the two rules: 1. Initially, every LR(0) item in I is added to closure(I). 2. If A → α.BÎČ is in closure(I) and B→γ is a production rule of G; then B→.Îł will be in the closure(I). We will apply this rule until no more new LR(0) items can be add d t l (I) ed to closure(I). 20
  • 21. Example Grammar : I= {E’ →.E} E’ → E E  E+T E  T T  T*F T  F F  ( E ) F  id closure({E’ →.E}) = E’ →.E E →.E+T E →.T T →.T*F T →.F F →.(E) F →.id 21
  • 22. Goto Function If I is a set of items and X ∈ (VnU VT ), then, goto(I, X) is the closure of the set of all items A → αX. ÎČ such that A → α. XÎČ is in I. If I is a set of items valid for a prefix α of a right-sentential form, then, Goto(I, X) is valid for the prefix αX 22
  • 23. 23 Goto(I, X) represents the transition of the automaton from state I and input X. GOTO(I=state ,X=input)
  • 24. 24 Grammar : Eâ€Č → E E → E + T E → T T → T ∗ F T → F F → (E) F → id I2 E → E+.T T → .T ∗ F T → . F F → .(E) F → . id I0 E →.E+T E →.T T →.T*F T →.F F →.(E) F →.id I1 Eâ€Č → E. , E → E. +T Example GOTO(I0,E) GOTO(I1,+)
  • 25. Example If I1 = {Eâ€Č → E. , E → E. +T}, then: goto(I1,+) = closure({E → E+.T}), is the set: E → E+.T T → .T ∗ F T → . F F → .(E) F → . id Grammar : Eâ€Č → E E → E + T E → T T → T ∗ F T → F F → (E) F → id 25
  • 26. Canonical Collection Algorithm. Canonical Collection for an Augmented Grammar Gâ€Č 1. Initially, C = {closure({Sâ€Č →. S})}; 2. For each set of items I in C and each Grammar symbol X If goto(I, X) ≠ ∅ and Goto(I, X) /∈ C, then add Goto(I, X) to C; 3. Go to step 2 if new items have been added, otherwise stop 26
  • 27. Example Grammer : S → E E → E + T | T T → T * F | F F → id 27
  • 28. S’  .E E  .E+T E  .T T  .T*F T  .F F  .id S’  E. E  E.+T E  E+.T T  .T*F T  .F F  .id E  T. T  T.*F T  T*.F F  .id E  E+T. T  T.*f T  T*F. T  F. F  id. Canonical Collection 28 E T F id + * T * F id id F I0 I1 I5 I7 I8 I6 I4 I2 I3
  • 29. Table Construction Rules α, ÎČ = any string of terminals and/or non‐terminals X, S’, S = non‐terminals (When dot is in middle) 1. if [A ‐‐> α.aÎČ] Δ Ii and read on ‘a’ produces Ij then ACTION [i , a] = SHIFT j. 2. if [A ‐‐> α.XÎČ] Δ Ii and read on ‘X’ produces Ij then GOTO [i , X] = j. 29
  • 30. Cont.. (When dot is at end) 3. if [A ‐‐> α.] Δ Ii then ACTION [i , a] = REDUCE on A ‐> α for all a Δ FOLLOW(A). 4. if [S’ ‐‐> S.] Δ Ii then ACTION [i , $] = ACCEPT. 30