SlideShare a Scribd company logo
1 of 15
Parsing Methods
Parsing
Top down parsing Bottom up parsing (Shift reduce)
Back tracking
Parsing without
backtracking (predictive
Parsing)
LR parsing
Operator precedence
LALR
CLR
SLR
Recursive
descent
LL(1)
LR parser
• LR parsing is most efficient method of bottom up parsing which can be used to
parse large class of context free grammar.
• The technique is called LR(k) parsing:
1. The “L” is for left to right scanning of input symbol,
2. The “R” for constructing right most derivation in reverse,
3. The “k” for the number of input symbols of look ahead that are used in
making parsing decision.
LR parsing
program
INPUT
OUTPUT
a + b $
X
Y
Z
$
Parsing Table
Action Goto
Parsing Methods
Parsing
Top down parsing Bottom up parsing (Shift reduce)
Back tracking
Parsing without
backtracking (predictive
parsing)
LR parsing
Operator precedence
LALR
CLR
SLR
Recursive
descent
LL(1)
Computation of closure & go to function
X Xb
Closure(I):
X X b
Goto(I,X)
X X b
.
.
Steps to construct SLR parser
1. Construct Canonical set of LR(0) items
2. Construct SLR parsing table
3. Parse the input string
Example: SLR(1)- simple LR
S  AA
A  aA | b
S’.S
S. AA
A. aA
A. b
S’ S.
S A . A
A. aA
A. b
A. aA
A. b
A a . A
A b.
A. aA
A. b
A a . A
A b.
A. aA
A. b
A a . A
A b.
S AA .
A aA .
𝑰𝟎
𝑰𝟏
𝑰𝟐
𝑰3 𝑰6
𝑰5
𝑰3
𝑰3
𝑰4
𝑰4
𝑰4
𝐺𝑜 𝑡𝑜 (𝐼0, 𝑆)
𝐺𝑜 𝑡𝑜 (𝐼0, 𝑏)
𝐺𝑜 𝑡𝑜 (𝐼2, 𝑏)
𝐺𝑜 𝑡𝑜 (𝐼2, 𝐴)
𝐺𝑜 𝑡𝑜 (𝐼3, 𝑎)
Augmented
grammar
LR(0) item set
Rules to construct SLR parsing table
1. Construct 𝐶 = { 𝐼0, 𝐼1, … … . 𝐼𝑛}, the collection of sets of LR(0) items for 𝐺’.
2. State 𝑖 is constructed from 𝐼𝑖. The parsing actions for state 𝑖 are determined as
follow :
a) If [ 𝐴 → 𝛼. 𝑎𝛽 ] is in 𝐼𝑖 and GOTO (𝐼𝑖 , 𝑎) = 𝐼𝑗 , then set 𝐴𝐶𝑇𝐼𝑂𝑁[𝑖, 𝑎] to “shift j”.
Here a must be terminal.
b) If [𝐴 → 𝛼. ] is in 𝐼𝑖, then set 𝐴𝐶𝑇𝐼𝑂𝑁[𝑖, 𝑎] to “reduce A 𝛼” for all a in 𝐹𝑂𝐿𝐿𝑂𝑊(𝐴);
here A may not be S’.
c) If [𝑆 → 𝑆. ] is in 𝐼𝑖, then set action [𝑖, $] to “accept”.
3. The goto transitions for state i are constructed for all non terminals A using
the𝑖𝑓 𝐺𝑂𝑇𝑂( 𝐼𝑖 , 𝐴 ) = 𝐼𝑗 𝑡ℎ𝑒𝑛 𝐺𝑂𝑇𝑂 [𝑖, 𝐴] = 𝑗.
4. All entries not defined by rules 2 and 3 are made error.
Example: SLR(1)- simple LR
Action Go to
Item
set
a b $ S A
0 S3 S4 1 2
1 Accept
2 S3 S4 5
3 S3 S4 6
4 R3 R3 R3
5 R1
6 R2 R2 R2
S’. S
S. AA
A. aA
A. b
S’ S.
S A . A
A. aA
A. b
A. aA
A. b
A a . A
A b.
A. aA
A. b
A a . A
A b.
A. aA
A. b
A a . A
A b.
S AA .
A aA .
𝑰𝟎
𝑰𝟏
𝑰𝟐
𝑰3 𝑰6
𝑰5
𝑰3
𝑰3
𝑰4
𝑰4
𝑰4
𝐺𝑜 𝑡𝑜 (𝐼0, 𝑆)
𝐺𝑜 𝑡𝑜 (𝐼0, 𝑏)
𝐺𝑜 𝑡𝑜 (𝐼2, 𝑏)
𝐺𝑜 𝑡𝑜 (𝐼2, 𝐴)
𝐺𝑜 𝑡𝑜 (𝐼3, 𝑎)
S  AA
A  aA | b
𝐹𝑜𝑙𝑙𝑜𝑤(𝑆) = {$}
𝐹𝑜𝑙𝑙𝑜𝑤(𝐴) = {𝑎, 𝑏, $}
Parsing Methods
Parsing
Top down parsing Bottom up parsing (Shift reduce)
Back tracking
Parsing without
backtracking (predictive
Parsing)
LR parsing
Operator precedence
LALR
CLR
SLR
Recursive
descent
LL(1)
How to calculate look ahead?
How to calculate look ahead?
SCC
C cC | d
Closure(I)
S’.S,$
S.CC,
C.cC,
C.d,
S’  . S , $
A  𝜶 . X 𝜷 , 𝒂
Lookahead = First 𝜷𝒂
First $
= $
$
S  . C C , $
A  𝜶 . X 𝜷 , 𝒂
Lookahead = First 𝜷𝒂
First 𝐶$
= 𝒄, 𝒅
c|d
c|d
Example: CLR(1)- canonical LR
S  AA
A  aA | b
S’.S,$
S.AA,$
A.aA, a|b
A.b, a|b
S’ S., $
S A.A,$
A.aA, $
A. b, $
A.aA ,a|b
A. b, a|b
Aa.A, a|b
A b., a|b
A. aA,$
A. b, $
A a.A,$
A b. ,$
A.aA , a|b
A.b , a|b
A a.A , a|b
A b., a|b
S AA. ,$
A aA.,a|b
𝑰𝟎
𝑰𝟏
𝑰𝟐
𝑰3
𝑰8
𝑰5 𝑰6
𝑰3
𝑰7
𝑰4
𝑰4
𝐺𝑜 𝑡𝑜 (𝐼0, 𝑆)
𝐺𝑜 𝑡𝑜 (𝐼0, 𝑏)
𝐺𝑜 𝑡𝑜 (𝐼2, 𝑏)
𝐺𝑜 𝑡𝑜 (𝐼2, 𝐴)
𝐺𝑜 𝑡𝑜 (𝐼3, 𝑎)
Augmented
grammar
A aA.,$
𝑰9
A. aA,$
A. b, $
A a.A,$
A b. ,S
𝑰6
𝑰7
LR(1) item set
Example: CLR(1)- canonical LR
S  AA
A  aA | b
S’.S,$
S.AA,$
A.aA, a|b
A.b, a|b
S’ S., $
S A.A,$
A.aA, $
A. b, $
A.aA ,a|b
A. b, a|b
Aa.A, a|b
A b., a|b
A. aA,$
A. b, $
A a.A,$
A b. ,S
A.aA , a|b
A.b , a|b
A a.A , a|b
A b., a|b
S AA. ,$
A aA.,a|b
𝑰𝟎
𝑰𝟏
𝑰𝟐
𝑰3
𝑰8
𝑰5 𝑰6
𝑰3
𝑰7
𝑰4
𝑰4
𝐺𝑜 𝑡𝑜 (𝐼0, 𝑆)
𝐺𝑜 𝑡𝑜 (𝐼0, 𝑏)
𝐺𝑜 𝑡𝑜 (𝐼2, 𝑏)
𝐺𝑜 𝑡𝑜 (𝐼2, 𝐴)
𝐺𝑜 𝑡𝑜 (𝐼3, 𝑎)
A aA.,$
𝑰9
Item
set
Action Go to
a b $ S A
0 S3 S4 1 2
1 Accept
2 S6 S7 5
3 S3 S4 8
4 R3 R3
5 R1
6 S6 S7 9
7 R3
8 R2 R2
9 R2
A. aA,$
A. b, $
A a.A,$
A b. ,S
𝑰6
𝑰7
Parsing Methods
Parsing
Top down parsing Bottom up parsing (Shift reduce)
Back tracking
Parsing without
backtracking (predictive
Parsing)
LR parsing
Operator precedence
LALR
CLR
SLR
Recursive
descent
LL(1)
A aA.,a|b
Example: LALR(1)- look ahead LR
S  AA
A  aA | b
S’.S,$
S.AA,$
A.aA, a|b
A.b, a|b
S’ S., $
S A.A,$
A.aA, $
A. b, $
A.aA ,a|b
A. b, a|b
Aa.A, a|b
A b., a|b
A. aA,$
A. b, $
A a.A,$
A b. ,$
A.aA , a|b
A.b , a|b
A a.A , a|b
A b., a|b
S AA. ,$
𝑰𝟎
𝑰𝟏
𝑰𝟐
𝑰3
𝑰8
𝑰5 𝑰6
𝑰3
𝑰7
𝑰4
𝑰4
𝐺𝑜 𝑡𝑜 (𝐼0, 𝑆)
𝐺𝑜 𝑡𝑜 (𝐼0, 𝑏)
𝐺𝑜 𝑡𝑜 (𝐼2, 𝑏)
𝐺𝑜 𝑡𝑜 (𝐼2, 𝐴)
𝐺𝑜 𝑡𝑜 (𝐼3, 𝑎)
A aA.,$
𝑰9
A. aA,$
A. b, $
A a.A,$
A b. ,$
𝑰6
𝑰7
CLR
𝑰36
A.aA , a|b|$
A. b, a|b|$
Aa.A, a|b|$
A b., a|b|$
𝑰47
A aA.,a|b|$
𝑰89
Example: LALR(1)- look ahead LR
Item
set
Action Go to
a b $ S A
0 S3 S4 1 2
1 Accept
2 S6 S7 5
3 S3 S4 8
4 R3 R3
5 R1
6 S6 S7 9
7 R3
8 R2 R2
9 R2
Item
set
Action Go to
a b $ S A
0 S36 S47 1 2
1 Accept
2 S36 S47 5
36 S36 S47 89
47 R3 R3 R3
5 R1
89 R2 R2 R2
CLR Parsing Table LALR Parsing Table

More Related Content

What's hot

شرح مبسط عن الخوارزميات الجينية باستخدام الحاسبات
شرح مبسط عن الخوارزميات الجينية باستخدام الحاسباتشرح مبسط عن الخوارزميات الجينية باستخدام الحاسبات
شرح مبسط عن الخوارزميات الجينية باستخدام الحاسبات
sayAAhmad
 
Context free grammars
Context free grammarsContext free grammars
Context free grammars
Ronak Thakkar
 

What's hot (20)

Bottom up parser
Bottom up parserBottom up parser
Bottom up parser
 
شرح مبسط عن الخوارزميات الجينية باستخدام الحاسبات
شرح مبسط عن الخوارزميات الجينية باستخدام الحاسباتشرح مبسط عن الخوارزميات الجينية باستخدام الحاسبات
شرح مبسط عن الخوارزميات الجينية باستخدام الحاسبات
 
Lex & yacc
Lex & yaccLex & yacc
Lex & yacc
 
Reduction & Handle Pruning
Reduction & Handle PruningReduction & Handle Pruning
Reduction & Handle Pruning
 
NLP_KASHK:Finite-State Automata
NLP_KASHK:Finite-State AutomataNLP_KASHK:Finite-State Automata
NLP_KASHK:Finite-State Automata
 
Finite automata-for-lexical-analysis
Finite automata-for-lexical-analysisFinite automata-for-lexical-analysis
Finite automata-for-lexical-analysis
 
Lexical analyzer generator lex
Lexical analyzer generator lexLexical analyzer generator lex
Lexical analyzer generator lex
 
Design and Analysis of algorithms
Design and Analysis of algorithmsDesign and Analysis of algorithms
Design and Analysis of algorithms
 
Regular expression to NFA (Nondeterministic Finite Automata)
Regular expression to NFA (Nondeterministic Finite Automata)Regular expression to NFA (Nondeterministic Finite Automata)
Regular expression to NFA (Nondeterministic Finite Automata)
 
Context free grammars
Context free grammarsContext free grammars
Context free grammars
 
push down automata
 push down automata push down automata
push down automata
 
Lexical Analysis - Compiler design
Lexical Analysis - Compiler design Lexical Analysis - Compiler design
Lexical Analysis - Compiler design
 
Context free languages
Context free languagesContext free languages
Context free languages
 
1.9. minimization of dfa
1.9. minimization of dfa1.9. minimization of dfa
1.9. minimization of dfa
 
Theory of Computation Unit 1
Theory of Computation Unit 1Theory of Computation Unit 1
Theory of Computation Unit 1
 
Input-Buffering
Input-BufferingInput-Buffering
Input-Buffering
 
Context free grammar
Context free grammar Context free grammar
Context free grammar
 
Intermediate code generation in Compiler Design
Intermediate code generation in Compiler DesignIntermediate code generation in Compiler Design
Intermediate code generation in Compiler Design
 
LR PARSE.pptx
LR PARSE.pptxLR PARSE.pptx
LR PARSE.pptx
 
Topdown parsing
Topdown parsingTopdown parsing
Topdown parsing
 

Similar to 10-SLR parser practice problems-02-06-2023.pptx

Chapter Five(2)
Chapter Five(2)Chapter Five(2)
Chapter Five(2)
bolovv
 
sameermlr0parser-200701133032.pptx
sameermlr0parser-200701133032.pptxsameermlr0parser-200701133032.pptx
sameermlr0parser-200701133032.pptx
BapanKar2
 

Similar to 10-SLR parser practice problems-02-06-2023.pptx (20)

Functional Programming.pptx
Functional Programming.pptxFunctional Programming.pptx
Functional Programming.pptx
 
Chapter Five(2)
Chapter Five(2)Chapter Five(2)
Chapter Five(2)
 
Syntactic analysis in NLP
Syntactic analysis in NLPSyntactic analysis in NLP
Syntactic analysis in NLP
 
UNIT 2 (1).pptx
UNIT 2 (1).pptxUNIT 2 (1).pptx
UNIT 2 (1).pptx
 
BOTTOM UP PARSING GROUP 3.pptx
BOTTOM UP PARSING GROUP 3.pptxBOTTOM UP PARSING GROUP 3.pptx
BOTTOM UP PARSING GROUP 3.pptx
 
(Ai lisp)
(Ai lisp)(Ai lisp)
(Ai lisp)
 
Advance LISP (Artificial Intelligence)
Advance LISP (Artificial Intelligence) Advance LISP (Artificial Intelligence)
Advance LISP (Artificial Intelligence)
 
compiler design.pdf
compiler design.pdfcompiler design.pdf
compiler design.pdf
 
PARSING.ppt
PARSING.pptPARSING.ppt
PARSING.ppt
 
lalr. fo engineering student those who to
lalr. fo engineering student those who tolalr. fo engineering student those who to
lalr. fo engineering student those who to
 
COMPILER DESIGN- Syntax Analysis
COMPILER DESIGN- Syntax AnalysisCOMPILER DESIGN- Syntax Analysis
COMPILER DESIGN- Syntax Analysis
 
Linear logic (and Linear Lisp)
Linear logic (and Linear Lisp)Linear logic (and Linear Lisp)
Linear logic (and Linear Lisp)
 
sameermlr0parser-200701133032.pptx
sameermlr0parser-200701133032.pptxsameermlr0parser-200701133032.pptx
sameermlr0parser-200701133032.pptx
 
Ch4b
Ch4bCh4b
Ch4b
 
QR Factorizations and SVDs for Tall-and-skinny Matrices in MapReduce Architec...
QR Factorizations and SVDs for Tall-and-skinny Matrices in MapReduce Architec...QR Factorizations and SVDs for Tall-and-skinny Matrices in MapReduce Architec...
QR Factorizations and SVDs for Tall-and-skinny Matrices in MapReduce Architec...
 
LR(0) PARSER
LR(0) PARSERLR(0) PARSER
LR(0) PARSER
 
Lecture11 syntax analysis_7
Lecture11 syntax analysis_7Lecture11 syntax analysis_7
Lecture11 syntax analysis_7
 
Lecture 07 08 syntax analysis-4
Lecture 07 08 syntax analysis-4Lecture 07 08 syntax analysis-4
Lecture 07 08 syntax analysis-4
 
Scala as a Declarative Language
Scala as a Declarative LanguageScala as a Declarative Language
Scala as a Declarative Language
 
Lecture 15 16
Lecture 15 16Lecture 15 16
Lecture 15 16
 

More from venkatapranaykumarGa

5-Introduction to Parsing and Context Free Grammar-09-05-2023.pptx
5-Introduction to Parsing and Context Free Grammar-09-05-2023.pptx5-Introduction to Parsing and Context Free Grammar-09-05-2023.pptx
5-Introduction to Parsing and Context Free Grammar-09-05-2023.pptx
venkatapranaykumarGa
 
13-Applications of Syntax Directed Translation - Syntax Directed Translation ...
13-Applications of Syntax Directed Translation - Syntax Directed Translation ...13-Applications of Syntax Directed Translation - Syntax Directed Translation ...
13-Applications of Syntax Directed Translation - Syntax Directed Translation ...
venkatapranaykumarGa
 
12-Syntax Directed Definition – Evaluation Order-09-06-2023.ppt
12-Syntax Directed Definition – Evaluation Order-09-06-2023.ppt12-Syntax Directed Definition – Evaluation Order-09-06-2023.ppt
12-Syntax Directed Definition – Evaluation Order-09-06-2023.ppt
venkatapranaykumarGa
 
15-CAT-2 answer key discussion-04-07-2023.pdf
15-CAT-2 answer key discussion-04-07-2023.pdf15-CAT-2 answer key discussion-04-07-2023.pdf
15-CAT-2 answer key discussion-04-07-2023.pdf
venkatapranaykumarGa
 
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
venkatapranaykumarGa
 
8-Practice problems on operator precedence parser-24-05-2023.docx
8-Practice problems on operator precedence parser-24-05-2023.docx8-Practice problems on operator precedence parser-24-05-2023.docx
8-Practice problems on operator precedence parser-24-05-2023.docx
venkatapranaykumarGa
 
14-Intermediate code generation - Variants of Syntax trees - Three Address Co...
14-Intermediate code generation - Variants of Syntax trees - Three Address Co...14-Intermediate code generation - Variants of Syntax trees - Three Address Co...
14-Intermediate code generation - Variants of Syntax trees - Three Address Co...
venkatapranaykumarGa
 
4-Regular expression to Deterministic Finite Automata (Direct method)-05-05-2...
4-Regular expression to Deterministic Finite Automata (Direct method)-05-05-2...4-Regular expression to Deterministic Finite Automata (Direct method)-05-05-2...
4-Regular expression to Deterministic Finite Automata (Direct method)-05-05-2...
venkatapranaykumarGa
 
6-Practice Problems - LL(1) parser-16-05-2023.pptx
6-Practice Problems - LL(1) parser-16-05-2023.pptx6-Practice Problems - LL(1) parser-16-05-2023.pptx
6-Practice Problems - LL(1) parser-16-05-2023.pptx
venkatapranaykumarGa
 
1-Phases of compiler-26-04-2023.pptx
1-Phases of compiler-26-04-2023.pptx1-Phases of compiler-26-04-2023.pptx
1-Phases of compiler-26-04-2023.pptx
venkatapranaykumarGa
 
9-Removal of ambiguity, precedence and associativity-26-05-2023.docx
9-Removal of ambiguity, precedence and associativity-26-05-2023.docx9-Removal of ambiguity, precedence and associativity-26-05-2023.docx
9-Removal of ambiguity, precedence and associativity-26-05-2023.docx
venkatapranaykumarGa
 
7-Operator Precedence Parser-23-05-2023.pptx
7-Operator Precedence Parser-23-05-2023.pptx7-Operator Precedence Parser-23-05-2023.pptx
7-Operator Precedence Parser-23-05-2023.pptx
venkatapranaykumarGa
 
2-Design Issues, Patterns, Lexemes, Tokens-28-04-2023.docx
2-Design Issues, Patterns, Lexemes, Tokens-28-04-2023.docx2-Design Issues, Patterns, Lexemes, Tokens-28-04-2023.docx
2-Design Issues, Patterns, Lexemes, Tokens-28-04-2023.docx
venkatapranaykumarGa
 

More from venkatapranaykumarGa (14)

9-Query Processing-05-06-2023.PPT
9-Query Processing-05-06-2023.PPT9-Query Processing-05-06-2023.PPT
9-Query Processing-05-06-2023.PPT
 
5-Introduction to Parsing and Context Free Grammar-09-05-2023.pptx
5-Introduction to Parsing and Context Free Grammar-09-05-2023.pptx5-Introduction to Parsing and Context Free Grammar-09-05-2023.pptx
5-Introduction to Parsing and Context Free Grammar-09-05-2023.pptx
 
13-Applications of Syntax Directed Translation - Syntax Directed Translation ...
13-Applications of Syntax Directed Translation - Syntax Directed Translation ...13-Applications of Syntax Directed Translation - Syntax Directed Translation ...
13-Applications of Syntax Directed Translation - Syntax Directed Translation ...
 
12-Syntax Directed Definition – Evaluation Order-09-06-2023.ppt
12-Syntax Directed Definition – Evaluation Order-09-06-2023.ppt12-Syntax Directed Definition – Evaluation Order-09-06-2023.ppt
12-Syntax Directed Definition – Evaluation Order-09-06-2023.ppt
 
15-CAT-2 answer key discussion-04-07-2023.pdf
15-CAT-2 answer key discussion-04-07-2023.pdf15-CAT-2 answer key discussion-04-07-2023.pdf
15-CAT-2 answer key discussion-04-07-2023.pdf
 
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
 
8-Practice problems on operator precedence parser-24-05-2023.docx
8-Practice problems on operator precedence parser-24-05-2023.docx8-Practice problems on operator precedence parser-24-05-2023.docx
8-Practice problems on operator precedence parser-24-05-2023.docx
 
14-Intermediate code generation - Variants of Syntax trees - Three Address Co...
14-Intermediate code generation - Variants of Syntax trees - Three Address Co...14-Intermediate code generation - Variants of Syntax trees - Three Address Co...
14-Intermediate code generation - Variants of Syntax trees - Three Address Co...
 
4-Regular expression to Deterministic Finite Automata (Direct method)-05-05-2...
4-Regular expression to Deterministic Finite Automata (Direct method)-05-05-2...4-Regular expression to Deterministic Finite Automata (Direct method)-05-05-2...
4-Regular expression to Deterministic Finite Automata (Direct method)-05-05-2...
 
6-Practice Problems - LL(1) parser-16-05-2023.pptx
6-Practice Problems - LL(1) parser-16-05-2023.pptx6-Practice Problems - LL(1) parser-16-05-2023.pptx
6-Practice Problems - LL(1) parser-16-05-2023.pptx
 
1-Phases of compiler-26-04-2023.pptx
1-Phases of compiler-26-04-2023.pptx1-Phases of compiler-26-04-2023.pptx
1-Phases of compiler-26-04-2023.pptx
 
9-Removal of ambiguity, precedence and associativity-26-05-2023.docx
9-Removal of ambiguity, precedence and associativity-26-05-2023.docx9-Removal of ambiguity, precedence and associativity-26-05-2023.docx
9-Removal of ambiguity, precedence and associativity-26-05-2023.docx
 
7-Operator Precedence Parser-23-05-2023.pptx
7-Operator Precedence Parser-23-05-2023.pptx7-Operator Precedence Parser-23-05-2023.pptx
7-Operator Precedence Parser-23-05-2023.pptx
 
2-Design Issues, Patterns, Lexemes, Tokens-28-04-2023.docx
2-Design Issues, Patterns, Lexemes, Tokens-28-04-2023.docx2-Design Issues, Patterns, Lexemes, Tokens-28-04-2023.docx
2-Design Issues, Patterns, Lexemes, Tokens-28-04-2023.docx
 

Recently uploaded

The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
heathfieldcps1
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
QucHHunhnh
 
Gardella_Mateo_IntellectualProperty.pdf.
Gardella_Mateo_IntellectualProperty.pdf.Gardella_Mateo_IntellectualProperty.pdf.
Gardella_Mateo_IntellectualProperty.pdf.
MateoGardella
 
An Overview of Mutual Funds Bcom Project.pdf
An Overview of Mutual Funds Bcom Project.pdfAn Overview of Mutual Funds Bcom Project.pdf
An Overview of Mutual Funds Bcom Project.pdf
SanaAli374401
 

Recently uploaded (20)

Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
 
fourth grading exam for kindergarten in writing
fourth grading exam for kindergarten in writingfourth grading exam for kindergarten in writing
fourth grading exam for kindergarten in writing
 
Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024
 
SECOND SEMESTER TOPIC COVERAGE SY 2023-2024 Trends, Networks, and Critical Th...
SECOND SEMESTER TOPIC COVERAGE SY 2023-2024 Trends, Networks, and Critical Th...SECOND SEMESTER TOPIC COVERAGE SY 2023-2024 Trends, Networks, and Critical Th...
SECOND SEMESTER TOPIC COVERAGE SY 2023-2024 Trends, Networks, and Critical Th...
 
Measures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDMeasures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SD
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activity
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The Basics
 
Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17
 
Class 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdfClass 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdf
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdf
 
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
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
 
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
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdf
 
Gardella_Mateo_IntellectualProperty.pdf.
Gardella_Mateo_IntellectualProperty.pdf.Gardella_Mateo_IntellectualProperty.pdf.
Gardella_Mateo_IntellectualProperty.pdf.
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.
 
An Overview of Mutual Funds Bcom Project.pdf
An Overview of Mutual Funds Bcom Project.pdfAn Overview of Mutual Funds Bcom Project.pdf
An Overview of Mutual Funds Bcom Project.pdf
 

10-SLR parser practice problems-02-06-2023.pptx

  • 1. Parsing Methods Parsing Top down parsing Bottom up parsing (Shift reduce) Back tracking Parsing without backtracking (predictive Parsing) LR parsing Operator precedence LALR CLR SLR Recursive descent LL(1)
  • 2. LR parser • LR parsing is most efficient method of bottom up parsing which can be used to parse large class of context free grammar. • The technique is called LR(k) parsing: 1. The “L” is for left to right scanning of input symbol, 2. The “R” for constructing right most derivation in reverse, 3. The “k” for the number of input symbols of look ahead that are used in making parsing decision. LR parsing program INPUT OUTPUT a + b $ X Y Z $ Parsing Table Action Goto
  • 3. Parsing Methods Parsing Top down parsing Bottom up parsing (Shift reduce) Back tracking Parsing without backtracking (predictive parsing) LR parsing Operator precedence LALR CLR SLR Recursive descent LL(1)
  • 4. Computation of closure & go to function X Xb Closure(I): X X b Goto(I,X) X X b . .
  • 5. Steps to construct SLR parser 1. Construct Canonical set of LR(0) items 2. Construct SLR parsing table 3. Parse the input string
  • 6. Example: SLR(1)- simple LR S  AA A  aA | b S’.S S. AA A. aA A. b S’ S. S A . A A. aA A. b A. aA A. b A a . A A b. A. aA A. b A a . A A b. A. aA A. b A a . A A b. S AA . A aA . 𝑰𝟎 𝑰𝟏 𝑰𝟐 𝑰3 𝑰6 𝑰5 𝑰3 𝑰3 𝑰4 𝑰4 𝑰4 𝐺𝑜 𝑡𝑜 (𝐼0, 𝑆) 𝐺𝑜 𝑡𝑜 (𝐼0, 𝑏) 𝐺𝑜 𝑡𝑜 (𝐼2, 𝑏) 𝐺𝑜 𝑡𝑜 (𝐼2, 𝐴) 𝐺𝑜 𝑡𝑜 (𝐼3, 𝑎) Augmented grammar LR(0) item set
  • 7. Rules to construct SLR parsing table 1. Construct 𝐶 = { 𝐼0, 𝐼1, … … . 𝐼𝑛}, the collection of sets of LR(0) items for 𝐺’. 2. State 𝑖 is constructed from 𝐼𝑖. The parsing actions for state 𝑖 are determined as follow : a) If [ 𝐴 → 𝛼. 𝑎𝛽 ] is in 𝐼𝑖 and GOTO (𝐼𝑖 , 𝑎) = 𝐼𝑗 , then set 𝐴𝐶𝑇𝐼𝑂𝑁[𝑖, 𝑎] to “shift j”. Here a must be terminal. b) If [𝐴 → 𝛼. ] is in 𝐼𝑖, then set 𝐴𝐶𝑇𝐼𝑂𝑁[𝑖, 𝑎] to “reduce A 𝛼” for all a in 𝐹𝑂𝐿𝐿𝑂𝑊(𝐴); here A may not be S’. c) If [𝑆 → 𝑆. ] is in 𝐼𝑖, then set action [𝑖, $] to “accept”. 3. The goto transitions for state i are constructed for all non terminals A using the𝑖𝑓 𝐺𝑂𝑇𝑂( 𝐼𝑖 , 𝐴 ) = 𝐼𝑗 𝑡ℎ𝑒𝑛 𝐺𝑂𝑇𝑂 [𝑖, 𝐴] = 𝑗. 4. All entries not defined by rules 2 and 3 are made error.
  • 8. Example: SLR(1)- simple LR Action Go to Item set a b $ S A 0 S3 S4 1 2 1 Accept 2 S3 S4 5 3 S3 S4 6 4 R3 R3 R3 5 R1 6 R2 R2 R2 S’. S S. AA A. aA A. b S’ S. S A . A A. aA A. b A. aA A. b A a . A A b. A. aA A. b A a . A A b. A. aA A. b A a . A A b. S AA . A aA . 𝑰𝟎 𝑰𝟏 𝑰𝟐 𝑰3 𝑰6 𝑰5 𝑰3 𝑰3 𝑰4 𝑰4 𝑰4 𝐺𝑜 𝑡𝑜 (𝐼0, 𝑆) 𝐺𝑜 𝑡𝑜 (𝐼0, 𝑏) 𝐺𝑜 𝑡𝑜 (𝐼2, 𝑏) 𝐺𝑜 𝑡𝑜 (𝐼2, 𝐴) 𝐺𝑜 𝑡𝑜 (𝐼3, 𝑎) S  AA A  aA | b 𝐹𝑜𝑙𝑙𝑜𝑤(𝑆) = {$} 𝐹𝑜𝑙𝑙𝑜𝑤(𝐴) = {𝑎, 𝑏, $}
  • 9. Parsing Methods Parsing Top down parsing Bottom up parsing (Shift reduce) Back tracking Parsing without backtracking (predictive Parsing) LR parsing Operator precedence LALR CLR SLR Recursive descent LL(1)
  • 10. How to calculate look ahead? How to calculate look ahead? SCC C cC | d Closure(I) S’.S,$ S.CC, C.cC, C.d, S’  . S , $ A  𝜶 . X 𝜷 , 𝒂 Lookahead = First 𝜷𝒂 First $ = $ $ S  . C C , $ A  𝜶 . X 𝜷 , 𝒂 Lookahead = First 𝜷𝒂 First 𝐶$ = 𝒄, 𝒅 c|d c|d
  • 11. Example: CLR(1)- canonical LR S  AA A  aA | b S’.S,$ S.AA,$ A.aA, a|b A.b, a|b S’ S., $ S A.A,$ A.aA, $ A. b, $ A.aA ,a|b A. b, a|b Aa.A, a|b A b., a|b A. aA,$ A. b, $ A a.A,$ A b. ,$ A.aA , a|b A.b , a|b A a.A , a|b A b., a|b S AA. ,$ A aA.,a|b 𝑰𝟎 𝑰𝟏 𝑰𝟐 𝑰3 𝑰8 𝑰5 𝑰6 𝑰3 𝑰7 𝑰4 𝑰4 𝐺𝑜 𝑡𝑜 (𝐼0, 𝑆) 𝐺𝑜 𝑡𝑜 (𝐼0, 𝑏) 𝐺𝑜 𝑡𝑜 (𝐼2, 𝑏) 𝐺𝑜 𝑡𝑜 (𝐼2, 𝐴) 𝐺𝑜 𝑡𝑜 (𝐼3, 𝑎) Augmented grammar A aA.,$ 𝑰9 A. aA,$ A. b, $ A a.A,$ A b. ,S 𝑰6 𝑰7 LR(1) item set
  • 12. Example: CLR(1)- canonical LR S  AA A  aA | b S’.S,$ S.AA,$ A.aA, a|b A.b, a|b S’ S., $ S A.A,$ A.aA, $ A. b, $ A.aA ,a|b A. b, a|b Aa.A, a|b A b., a|b A. aA,$ A. b, $ A a.A,$ A b. ,S A.aA , a|b A.b , a|b A a.A , a|b A b., a|b S AA. ,$ A aA.,a|b 𝑰𝟎 𝑰𝟏 𝑰𝟐 𝑰3 𝑰8 𝑰5 𝑰6 𝑰3 𝑰7 𝑰4 𝑰4 𝐺𝑜 𝑡𝑜 (𝐼0, 𝑆) 𝐺𝑜 𝑡𝑜 (𝐼0, 𝑏) 𝐺𝑜 𝑡𝑜 (𝐼2, 𝑏) 𝐺𝑜 𝑡𝑜 (𝐼2, 𝐴) 𝐺𝑜 𝑡𝑜 (𝐼3, 𝑎) A aA.,$ 𝑰9 Item set Action Go to a b $ S A 0 S3 S4 1 2 1 Accept 2 S6 S7 5 3 S3 S4 8 4 R3 R3 5 R1 6 S6 S7 9 7 R3 8 R2 R2 9 R2 A. aA,$ A. b, $ A a.A,$ A b. ,S 𝑰6 𝑰7
  • 13. Parsing Methods Parsing Top down parsing Bottom up parsing (Shift reduce) Back tracking Parsing without backtracking (predictive Parsing) LR parsing Operator precedence LALR CLR SLR Recursive descent LL(1)
  • 14. A aA.,a|b Example: LALR(1)- look ahead LR S  AA A  aA | b S’.S,$ S.AA,$ A.aA, a|b A.b, a|b S’ S., $ S A.A,$ A.aA, $ A. b, $ A.aA ,a|b A. b, a|b Aa.A, a|b A b., a|b A. aA,$ A. b, $ A a.A,$ A b. ,$ A.aA , a|b A.b , a|b A a.A , a|b A b., a|b S AA. ,$ 𝑰𝟎 𝑰𝟏 𝑰𝟐 𝑰3 𝑰8 𝑰5 𝑰6 𝑰3 𝑰7 𝑰4 𝑰4 𝐺𝑜 𝑡𝑜 (𝐼0, 𝑆) 𝐺𝑜 𝑡𝑜 (𝐼0, 𝑏) 𝐺𝑜 𝑡𝑜 (𝐼2, 𝑏) 𝐺𝑜 𝑡𝑜 (𝐼2, 𝐴) 𝐺𝑜 𝑡𝑜 (𝐼3, 𝑎) A aA.,$ 𝑰9 A. aA,$ A. b, $ A a.A,$ A b. ,$ 𝑰6 𝑰7 CLR 𝑰36 A.aA , a|b|$ A. b, a|b|$ Aa.A, a|b|$ A b., a|b|$ 𝑰47 A aA.,a|b|$ 𝑰89
  • 15. Example: LALR(1)- look ahead LR Item set Action Go to a b $ S A 0 S3 S4 1 2 1 Accept 2 S6 S7 5 3 S3 S4 8 4 R3 R3 5 R1 6 S6 S7 9 7 R3 8 R2 R2 9 R2 Item set Action Go to a b $ S A 0 S36 S47 1 2 1 Accept 2 S36 S47 5 36 S36 S47 89 47 R3 R3 R3 5 R1 89 R2 R2 R2 CLR Parsing Table LALR Parsing Table