SlideShare uma empresa Scribd logo
1 de 20
Top-Down Parsing
Structure of Compiler
Syntax Analysis
• Scanner and parser
– Scanner looks at the lower level part of the programming language
• Only the language for the tokens
– Parser looks at the higher lever part of the programming language
• E.g., if statement, functions
• The tokens are abstracted
• Parser = Syntax analyzer
– Input: sequence of tokens from lexical analysis
– Output: a parse tree of the program
• E.g., AST
– Process:
• Try to derive from a starting symbol to the input string (How?)
• Build the parse tree following the derivation
Top-Down Parsing
• Top down parsing
– Recursive descent parsing
– Making Recursive descent a Predictive parsing
algorithm
• Left recursion elimination
• Left factoring
– Predictive parsing
• First set and Follow set
• Parse table construction
• Parsing procedure
– LL grammars and languages
• LL(1) grammar
Predictive parsing
A predictive parser is an efficient way of implementing recursive-descent
parsing by handling the stack of activation records explicitly.
First set and Follow set
The construction of a predictive parser is aided by two functions associated with
a grammar G. These functions, FIRST and FOLLOW, allow us to fill in the proper entries of a
predictive parsing table for G, if such a parsing table for G exist.
First :
If a is any string of grammar symbols, let FIRST(a) be the set of terminals that
begin the strings derived from a. If a ⇒ ε then e is also in FIRST(a).
Follow :
Define FOLLOW(A), for nonterminal A, to be the set of terminals a that can
appear immediately to the right of A in some sentential form, that is, the set of terminals a
such that there exists a derivation of the form S ⇒ αAa β for some a and b. Note that there
may, at some time during the derivation, have been symbols between A and a, but if so,
they derived ε and disappeared. If A can be the rightmost symbol in some sentential form,
then $, representing the input right endmarker, is in FOLLOW(A).
First
Rules for Compute First set:
1. If X is terminal, then FIRST (X) is {X}.
2. If X is nonterminal and X  aα is a production, then add a to
FIRST (X). If X  ε is a production, then add ε to FIRST (X).
3. If XY1Y2…..Yk is a production, then for all i such that all of
Y1,Y2…..,Yi-1 are nonterminals and FIRST (Yj) contains ε for
j=1,2,…,i-1 (i.e. Y1Y2…. Yi-1 ⇒ ε), add every non-ε symbol in
FIRST (Yi) to FIRST(X). If ε is in FIRST (Yj) for all j=1, 2… k, then
add ε to FIRST(X).
Follow
Rules for Compute Follow set:
1. $ is in FOLLOW(S), where S is the starting symbol.
2. If there is a production A  αBβ, β ≠ ε, then everything is in
FIRST (Β) except for ε is placed in FOLLOW (B).
3. If there is a production A  αB, or a production A  αBβ,
where FIRST (Β) contains ε (i.e. β ⇒ ε), then everything in
FOLLOW (A) is in FOLLOW (B).
Example
Grammar
E TE’
E’ +TE’ |
T FT’
T’ *FT’ |
F (E) | id | num
Note : This grammar is non left-recursive type so,
we get Both FIRST and FOLLOW sets
First
Grammar
E TE’
E’ +TE’ |
T FT’
T’ *FT’ |
F (E) | id | num
First set
FIRST (E) = { } FIRST (+) = { + } FIRST (id) = { id }
FIRST(E') = { } FIRST (*) = { * } FIRST (num) = { num }
FIRST(T) = { } FIRST (‘(‘) = { ( }
FIRST(T') = { } FIRST (‘)’) = { ) }
FIRST( F ) = { } FIRST ( ) = { }
The First thing we do is Add terminal
itself in its FIRST set by applying 1st
rule of FIRST set
First
Grammar
E TE’
E’ +TE’ |
T FT’
T’ *FT’ |
F (E) | id | num
First set
FIRST (E) = { } FIRST (+) = { + } FIRST (id) = { id }
FIRST(E') = { + , ε } FIRST (*) = { * } FIRST (num) = { num }
FIRST(T) = { } FIRST (‘(‘) = { ( }
FIRST(T') = { * , ε } FIRST (‘)’) = { ) }
FIRST( F ) = { ( , id , num } FIRST ( ) = { }
Next, we apply rule 2 i.e. for every
production X  aα add a to First (X)
Rule 2 also says that for every
production X  ε add ε to First (X)
First
Grammar
E TE’
E’ +TE’ |
T FT’
T’ *FT’ |
F (E) | id | num
First set
FIRST (E) = { ( , id , num } FIRST (+) = { + } FIRST (id) = { id }
FIRST(E') = { + , ε } FIRST (*) = { * } FIRST (num) = { num }
FIRST(T) = { ( , id , num } FIRST (‘(‘) = { ( }
FIRST(T') = { * , ε } FIRST (‘)’) = { ) }
FIRST( F ) = { ( , id , num } FIRST ( ) = { }
For Production T FT’ 3rd rule says that
everything in FIRST (F) is into FIRST (T).
(since production is like XY1Y2…Yk and
FIRST (Y1) not contain ε )
Similarly For Production E TE’ 3rd rule says
that everything in FIRST (F) is into FIRST (T).
Follow
Grammar
E TE’
E’ +TE’ |
T FT’
T’ *FT’ |
F (E) | id | num
The First thing we do is Add $ to the
start Symbol nonterminal “E”
Follow set
FOLLOW(E) = { $ }
FOLLOW(E') = { }
FOLLOW(T) = { }
FOLLOW(T') = { }
FOLLOW( F ) = { }
Follow
Grammar
E TE’
E’ +TE’ |
T FT’
T’ *FT’ |
F (E) | id | num
Follow set
FOLLOW(E) = { $ , ) }
FOLLOW(E') = { }
FOLLOW(T) = { + }
FOLLOW(T') = { }
FOLLOW( F ) = { * }
Next we get this productions on which
we apply rule 2we apply rule 2 to E' →+TE' This says that
everything in First(E') except for ε should be
in Follow(T)Similarly we can apply this rule 2 to other
two productions T' → *FT‘ and F → (E)
which give Follow(F) & Follow(E)
respectively
Follow
Grammar
E TE’
E’ +TE’ |
T FT’
T’ *FT’ |
F (E) | id | num
Follow set
FOLLOW(E) = { $ , ) }
FOLLOW(E') = { $ , ) }
FOLLOW(T) = { + , $ , ) }
FOLLOW(T') = { + , $ , ) }
FOLLOW(F) = { * , + , $ , ) }
Now applying 3rd rule on this
production we can say everything
in FOLLOW (E) is into FOLLOW (E’).
(since here β = ε )
Now applying 3rd rule on this
production we can say everything
in FOLLOW (E’) is into FOLLOW (T).
(since here β ⇒ ε )
Same here everything in
FOLLOW (T) is into FOLLOW (T’).
(since here β = ε )
Same here everything in
FOLLOW (T’) is into FOLLOW (F).
(since here β ⇒ ε )
Parse table
 Construct a parse table M[N, T {$}]
 Non-terminals in the rows and terminals in the columns
 For each production A
 For each terminal a First( )
add A to M[A, a]
 Meaning: When at A and seeing input a, A should be used
 If First( ) then for each terminal a Follow(A)
add A to M[A, a]
 Meaning: When at A and seeing input a, A should be used
• In order to continue expansion to
• X AC A B B b | C cc
 If First( ) and $ Follow(A)
add A to M[A, $]
 Same as the above
Construct a Parse Table
Grammar
E TE’
E’ +TE’ |
T FT’
T’ *FT’ |
F (E) | id | num
First(*) = {*}
First(F) = {(, id, num}
First(T’) = {*, }
First(T) {(, id, num}
First(E’) = {+, }
First(E) {(, id, num}
Follow(E) = {$, )}
Follow(E’) = {$, )}
Follow(T) = {$, ), +}
Follow(T) = {$, ), +}
Follow(T’) = {$, ), +}
Follow(F) = {*, $, ), +}
E TE’: First(TE’) = {(, id, num}E’ +TE’: First(+TE’) = {+}E’ : Follow(E’) = {$,)}T FT’: First(FT’) = {(, id, num}T’ *FT’: First(*FT’) = {*}T’ : Follow(T’) = {$, ), +}
id num * + ( ) $
E E TE’ E TE’ E TE’
E’ E’ +TE’ E’ E’
T T FT’ T FT’ T FT’
T’ T’ *FT’ T’ T’ T’
F F id F num F (E)
 Now we can have a predictive parsing mechanism
 Use a stack to keep track of the expanded form
 Initialization
 Put starting symbol S and $ into the stack
 Add $ to the end of the input string
 $ is for the recognition of the termination configuration
 If ‘a’ is at the top of the stack and ‘a’ is the next input symbol then
 Simply pop ‘a’ from stack and advance on the input string
 If ‘A’ is on top of the stack and ‘a’ is the next input symbol then
 Assume that M [A, a] = A
 Replace A by in the stack
 Termination
 When only $ in the stack and in the input string
 If ‘A’ is on top of the stack and ‘a’ is the next input but
 M[A, a] = empty
 Error
Parsing procedure
id num * + ( ) $
E E TE’ E TE’ E TE’
E’ E’ +TE’ E’ E’
T T FT’ T FT’ T FT’
T’ T’ *FT’ T’ T’ T’
F F id F num F (E)
Stack Input Action
E $ id + num * id $ E TE’
T E’ $ id + num * id $ T FT’
F T’ E’ $ id + num * id $ F id
T’ E’ $ + num * id $ T’
E’ $ + num * id $ E’ +TE’
T E’ $ num * id $ T FT’
F T’ E’ $ num * id $ F num
T’ E’ $ * id $ T’ *FT’
F T’ E’ $ id $ F id
T’ E’ $ $ T’
E’ $ $ E’
$ $ Accept
ERROR
Example of Predictive Parsing:
id + num * id
Top down parsing(sid) (1)

Mais conteúdo relacionado

Mais procurados

Parsing in Compiler Design
Parsing in Compiler DesignParsing in Compiler Design
Parsing in Compiler DesignAkhil Kaushik
 
Language Model (N-Gram).pptx
Language Model (N-Gram).pptxLanguage Model (N-Gram).pptx
Language Model (N-Gram).pptxHeneWijaya
 
Operator Precedence Grammar
Operator Precedence GrammarOperator Precedence Grammar
Operator Precedence GrammarHarisonFekadu
 
Context free langauges
Context free langaugesContext free langauges
Context free langaugessudhir sharma
 
Regular Expression to Finite Automata
Regular Expression to Finite AutomataRegular Expression to Finite Automata
Regular Expression to Finite AutomataArchana Gopinath
 
Minimization of DFA.pptx
Minimization of DFA.pptxMinimization of DFA.pptx
Minimization of DFA.pptxSadagopanS
 
Syntax Analysis in Compiler Design
Syntax Analysis in Compiler Design Syntax Analysis in Compiler Design
Syntax Analysis in Compiler Design MAHASREEM
 
Syntactic analysis in NLP
Syntactic analysis in NLPSyntactic analysis in NLP
Syntactic analysis in NLPkartikaVashisht
 
A Role of Lexical Analyzer
A Role of Lexical AnalyzerA Role of Lexical Analyzer
A Role of Lexical AnalyzerArchana Gopinath
 
Finite automata(For college Seminars)
Finite automata(For college Seminars)Finite automata(For college Seminars)
Finite automata(For college Seminars)Naman Joshi
 
Regular expressions and languages pdf
Regular expressions and languages pdfRegular expressions and languages pdf
Regular expressions and languages pdfDilouar Hossain
 
Lecture 02 lexical analysis
Lecture 02 lexical analysisLecture 02 lexical analysis
Lecture 02 lexical analysisIffat Anjum
 
Automata
AutomataAutomata
AutomataGaditek
 

Mais procurados (20)

Parsing in Compiler Design
Parsing in Compiler DesignParsing in Compiler Design
Parsing in Compiler Design
 
Parsing LL(1), SLR, LR(1)
Parsing LL(1), SLR, LR(1)Parsing LL(1), SLR, LR(1)
Parsing LL(1), SLR, LR(1)
 
Regular Expressions
Regular ExpressionsRegular Expressions
Regular Expressions
 
Language Model (N-Gram).pptx
Language Model (N-Gram).pptxLanguage Model (N-Gram).pptx
Language Model (N-Gram).pptx
 
Operator Precedence Grammar
Operator Precedence GrammarOperator Precedence Grammar
Operator Precedence Grammar
 
Context free langauges
Context free langaugesContext free langauges
Context free langauges
 
Regular Expression to Finite Automata
Regular Expression to Finite AutomataRegular Expression to Finite Automata
Regular Expression to Finite Automata
 
Minimization of DFA.pptx
Minimization of DFA.pptxMinimization of DFA.pptx
Minimization of DFA.pptx
 
Syntax Analysis in Compiler Design
Syntax Analysis in Compiler Design Syntax Analysis in Compiler Design
Syntax Analysis in Compiler Design
 
Syntactic analysis in NLP
Syntactic analysis in NLPSyntactic analysis in NLP
Syntactic analysis in NLP
 
A Role of Lexical Analyzer
A Role of Lexical AnalyzerA Role of Lexical Analyzer
A Role of Lexical Analyzer
 
Top down parsing
Top down parsingTop down parsing
Top down parsing
 
LL(1) parsing
LL(1) parsingLL(1) parsing
LL(1) parsing
 
Chapter 5 Syntax Directed Translation
Chapter 5   Syntax Directed TranslationChapter 5   Syntax Directed Translation
Chapter 5 Syntax Directed Translation
 
Finite automata(For college Seminars)
Finite automata(For college Seminars)Finite automata(For college Seminars)
Finite automata(For college Seminars)
 
Regular expressions and languages pdf
Regular expressions and languages pdfRegular expressions and languages pdf
Regular expressions and languages pdf
 
Finite automata
Finite automataFinite automata
Finite automata
 
Lecture 02 lexical analysis
Lecture 02 lexical analysisLecture 02 lexical analysis
Lecture 02 lexical analysis
 
Automata
AutomataAutomata
Automata
 
Recursion
RecursionRecursion
Recursion
 

Semelhante a Top down parsing(sid) (1)

Top down and botttom up 2 LATEST.
Top down     and botttom up 2 LATEST.Top down     and botttom up 2 LATEST.
Top down and botttom up 2 LATEST.Gerwin Ocsena
 
Top down and botttom up Parsing
Top down     and botttom up ParsingTop down     and botttom up Parsing
Top down and botttom up ParsingGerwin Ocsena
 
CS17604_TOP Parser Compiler Design Techniques
CS17604_TOP Parser Compiler Design TechniquesCS17604_TOP Parser Compiler Design Techniques
CS17604_TOP Parser Compiler Design Techniquesd72994185
 
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.pptxvenkatapranaykumarGa
 
ALF 5 - Parser Top-Down (2018)
ALF 5 - Parser Top-Down (2018)ALF 5 - Parser Top-Down (2018)
ALF 5 - Parser Top-Down (2018)Alexandru Radovici
 
Ch4_topdownparser_ngfjgh_ngjfhgfffdddf.PPT
Ch4_topdownparser_ngfjgh_ngjfhgfffdddf.PPTCh4_topdownparser_ngfjgh_ngjfhgfffdddf.PPT
Ch4_topdownparser_ngfjgh_ngjfhgfffdddf.PPTFutureTechnologies3
 
Ll(1) Parser in Compilers
Ll(1) Parser in CompilersLl(1) Parser in Compilers
Ll(1) Parser in CompilersMahbubur Rahman
 
compiler-lecture-6nn-14112022-110738am.ppt
compiler-lecture-6nn-14112022-110738am.pptcompiler-lecture-6nn-14112022-110738am.ppt
compiler-lecture-6nn-14112022-110738am.pptSheikhMuhammadSaad3
 
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 manualNitesh Dubey
 
Stacks,queues,linked-list
Stacks,queues,linked-listStacks,queues,linked-list
Stacks,queues,linked-listpinakspatel
 

Semelhante a Top down parsing(sid) (1) (20)

Top down and botttom up 2 LATEST.
Top down     and botttom up 2 LATEST.Top down     and botttom up 2 LATEST.
Top down and botttom up 2 LATEST.
 
Top down and botttom up Parsing
Top down     and botttom up ParsingTop down     and botttom up Parsing
Top down and botttom up Parsing
 
First and follow set
First and follow setFirst and follow set
First and follow set
 
CS17604_TOP Parser Compiler Design Techniques
CS17604_TOP Parser Compiler Design TechniquesCS17604_TOP Parser Compiler Design Techniques
CS17604_TOP Parser Compiler Design Techniques
 
First() and Follow()
First() and Follow()First() and Follow()
First() and Follow()
 
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
 
ALF 5 - Parser Top-Down (2018)
ALF 5 - Parser Top-Down (2018)ALF 5 - Parser Top-Down (2018)
ALF 5 - Parser Top-Down (2018)
 
ALF 5 - Parser Top-Down
ALF 5 - Parser Top-DownALF 5 - Parser Top-Down
ALF 5 - Parser Top-Down
 
Ch8b
Ch8bCh8b
Ch8b
 
Cs419 lec9 constructing parsing table ll1
Cs419 lec9   constructing parsing table ll1Cs419 lec9   constructing parsing table ll1
Cs419 lec9 constructing parsing table ll1
 
LL(1) Parsers
LL(1) ParsersLL(1) Parsers
LL(1) Parsers
 
Presentation1.pdf
Presentation1.pdfPresentation1.pdf
Presentation1.pdf
 
Ch4_topdownparser_ngfjgh_ngjfhgfffdddf.PPT
Ch4_topdownparser_ngfjgh_ngjfhgfffdddf.PPTCh4_topdownparser_ngfjgh_ngjfhgfffdddf.PPT
Ch4_topdownparser_ngfjgh_ngjfhgfffdddf.PPT
 
Ch5b
Ch5bCh5b
Ch5b
 
Ll(1) Parser in Compilers
Ll(1) Parser in CompilersLl(1) Parser in Compilers
Ll(1) Parser in Compilers
 
Left factor put
Left factor putLeft factor put
Left factor put
 
compiler-lecture-6nn-14112022-110738am.ppt
compiler-lecture-6nn-14112022-110738am.pptcompiler-lecture-6nn-14112022-110738am.ppt
compiler-lecture-6nn-14112022-110738am.ppt
 
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
 
Ch8a
Ch8aCh8a
Ch8a
 
Stacks,queues,linked-list
Stacks,queues,linked-listStacks,queues,linked-list
Stacks,queues,linked-list
 

Último

Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...pradhanghanshyam7136
 
Towards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptxTowards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptxJisc
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.pptRamjanShidvankar
 
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptxMaritesTamaniVerdade
 
Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Jisc
 
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17  How to Extend Models Using Mixin ClassesMixin Classes in Odoo 17  How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17 How to Extend Models Using Mixin ClassesCeline George
 
Graduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - EnglishGraduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - Englishneillewis46
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxAreebaZafar22
 
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...Nguyen Thanh Tu Collection
 
How to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSHow to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSCeline George
 
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.christianmathematics
 
Fostering Friendships - Enhancing Social Bonds in the Classroom
Fostering Friendships - Enhancing Social Bonds  in the ClassroomFostering Friendships - Enhancing Social Bonds  in the Classroom
Fostering Friendships - Enhancing Social Bonds in the ClassroomPooky Knightsmith
 
Micro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfMicro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfPoh-Sun Goh
 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxRamakrishna Reddy Bijjam
 
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...christianmathematics
 
How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17Celine George
 
Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...Association for Project Management
 
On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsMebane Rash
 
Sociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning ExhibitSociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning Exhibitjbellavia9
 
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdfUGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdfNirmal Dwivedi
 

Último (20)

Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
 
Towards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptxTowards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptx
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.ppt
 
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
 
Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)
 
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17  How to Extend Models Using Mixin ClassesMixin Classes in Odoo 17  How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
 
Graduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - EnglishGraduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - English
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptx
 
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
 
How to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSHow to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POS
 
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.
 
Fostering Friendships - Enhancing Social Bonds in the Classroom
Fostering Friendships - Enhancing Social Bonds  in the ClassroomFostering Friendships - Enhancing Social Bonds  in the Classroom
Fostering Friendships - Enhancing Social Bonds in the Classroom
 
Micro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfMicro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdf
 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docx
 
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...
 
How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17
 
Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...
 
On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan Fellows
 
Sociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning ExhibitSociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning Exhibit
 
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdfUGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
 

Top down parsing(sid) (1)

  • 3. Syntax Analysis • Scanner and parser – Scanner looks at the lower level part of the programming language • Only the language for the tokens – Parser looks at the higher lever part of the programming language • E.g., if statement, functions • The tokens are abstracted • Parser = Syntax analyzer – Input: sequence of tokens from lexical analysis – Output: a parse tree of the program • E.g., AST – Process: • Try to derive from a starting symbol to the input string (How?) • Build the parse tree following the derivation
  • 4. Top-Down Parsing • Top down parsing – Recursive descent parsing – Making Recursive descent a Predictive parsing algorithm • Left recursion elimination • Left factoring – Predictive parsing • First set and Follow set • Parse table construction • Parsing procedure – LL grammars and languages • LL(1) grammar
  • 5. Predictive parsing A predictive parser is an efficient way of implementing recursive-descent parsing by handling the stack of activation records explicitly.
  • 6. First set and Follow set The construction of a predictive parser is aided by two functions associated with a grammar G. These functions, FIRST and FOLLOW, allow us to fill in the proper entries of a predictive parsing table for G, if such a parsing table for G exist. First : If a is any string of grammar symbols, let FIRST(a) be the set of terminals that begin the strings derived from a. If a ⇒ ε then e is also in FIRST(a). Follow : Define FOLLOW(A), for nonterminal A, to be the set of terminals a that can appear immediately to the right of A in some sentential form, that is, the set of terminals a such that there exists a derivation of the form S ⇒ αAa β for some a and b. Note that there may, at some time during the derivation, have been symbols between A and a, but if so, they derived ε and disappeared. If A can be the rightmost symbol in some sentential form, then $, representing the input right endmarker, is in FOLLOW(A).
  • 7. First Rules for Compute First set: 1. If X is terminal, then FIRST (X) is {X}. 2. If X is nonterminal and X  aα is a production, then add a to FIRST (X). If X  ε is a production, then add ε to FIRST (X). 3. If XY1Y2…..Yk is a production, then for all i such that all of Y1,Y2…..,Yi-1 are nonterminals and FIRST (Yj) contains ε for j=1,2,…,i-1 (i.e. Y1Y2…. Yi-1 ⇒ ε), add every non-ε symbol in FIRST (Yi) to FIRST(X). If ε is in FIRST (Yj) for all j=1, 2… k, then add ε to FIRST(X).
  • 8. Follow Rules for Compute Follow set: 1. $ is in FOLLOW(S), where S is the starting symbol. 2. If there is a production A  αBβ, β ≠ ε, then everything is in FIRST (Β) except for ε is placed in FOLLOW (B). 3. If there is a production A  αB, or a production A  αBβ, where FIRST (Β) contains ε (i.e. β ⇒ ε), then everything in FOLLOW (A) is in FOLLOW (B).
  • 9. Example Grammar E TE’ E’ +TE’ | T FT’ T’ *FT’ | F (E) | id | num Note : This grammar is non left-recursive type so, we get Both FIRST and FOLLOW sets
  • 10. First Grammar E TE’ E’ +TE’ | T FT’ T’ *FT’ | F (E) | id | num First set FIRST (E) = { } FIRST (+) = { + } FIRST (id) = { id } FIRST(E') = { } FIRST (*) = { * } FIRST (num) = { num } FIRST(T) = { } FIRST (‘(‘) = { ( } FIRST(T') = { } FIRST (‘)’) = { ) } FIRST( F ) = { } FIRST ( ) = { } The First thing we do is Add terminal itself in its FIRST set by applying 1st rule of FIRST set
  • 11. First Grammar E TE’ E’ +TE’ | T FT’ T’ *FT’ | F (E) | id | num First set FIRST (E) = { } FIRST (+) = { + } FIRST (id) = { id } FIRST(E') = { + , ε } FIRST (*) = { * } FIRST (num) = { num } FIRST(T) = { } FIRST (‘(‘) = { ( } FIRST(T') = { * , ε } FIRST (‘)’) = { ) } FIRST( F ) = { ( , id , num } FIRST ( ) = { } Next, we apply rule 2 i.e. for every production X  aα add a to First (X) Rule 2 also says that for every production X  ε add ε to First (X)
  • 12. First Grammar E TE’ E’ +TE’ | T FT’ T’ *FT’ | F (E) | id | num First set FIRST (E) = { ( , id , num } FIRST (+) = { + } FIRST (id) = { id } FIRST(E') = { + , ε } FIRST (*) = { * } FIRST (num) = { num } FIRST(T) = { ( , id , num } FIRST (‘(‘) = { ( } FIRST(T') = { * , ε } FIRST (‘)’) = { ) } FIRST( F ) = { ( , id , num } FIRST ( ) = { } For Production T FT’ 3rd rule says that everything in FIRST (F) is into FIRST (T). (since production is like XY1Y2…Yk and FIRST (Y1) not contain ε ) Similarly For Production E TE’ 3rd rule says that everything in FIRST (F) is into FIRST (T).
  • 13. Follow Grammar E TE’ E’ +TE’ | T FT’ T’ *FT’ | F (E) | id | num The First thing we do is Add $ to the start Symbol nonterminal “E” Follow set FOLLOW(E) = { $ } FOLLOW(E') = { } FOLLOW(T) = { } FOLLOW(T') = { } FOLLOW( F ) = { }
  • 14. Follow Grammar E TE’ E’ +TE’ | T FT’ T’ *FT’ | F (E) | id | num Follow set FOLLOW(E) = { $ , ) } FOLLOW(E') = { } FOLLOW(T) = { + } FOLLOW(T') = { } FOLLOW( F ) = { * } Next we get this productions on which we apply rule 2we apply rule 2 to E' →+TE' This says that everything in First(E') except for ε should be in Follow(T)Similarly we can apply this rule 2 to other two productions T' → *FT‘ and F → (E) which give Follow(F) & Follow(E) respectively
  • 15. Follow Grammar E TE’ E’ +TE’ | T FT’ T’ *FT’ | F (E) | id | num Follow set FOLLOW(E) = { $ , ) } FOLLOW(E') = { $ , ) } FOLLOW(T) = { + , $ , ) } FOLLOW(T') = { + , $ , ) } FOLLOW(F) = { * , + , $ , ) } Now applying 3rd rule on this production we can say everything in FOLLOW (E) is into FOLLOW (E’). (since here β = ε ) Now applying 3rd rule on this production we can say everything in FOLLOW (E’) is into FOLLOW (T). (since here β ⇒ ε ) Same here everything in FOLLOW (T) is into FOLLOW (T’). (since here β = ε ) Same here everything in FOLLOW (T’) is into FOLLOW (F). (since here β ⇒ ε )
  • 16. Parse table  Construct a parse table M[N, T {$}]  Non-terminals in the rows and terminals in the columns  For each production A  For each terminal a First( ) add A to M[A, a]  Meaning: When at A and seeing input a, A should be used  If First( ) then for each terminal a Follow(A) add A to M[A, a]  Meaning: When at A and seeing input a, A should be used • In order to continue expansion to • X AC A B B b | C cc  If First( ) and $ Follow(A) add A to M[A, $]  Same as the above
  • 17. Construct a Parse Table Grammar E TE’ E’ +TE’ | T FT’ T’ *FT’ | F (E) | id | num First(*) = {*} First(F) = {(, id, num} First(T’) = {*, } First(T) {(, id, num} First(E’) = {+, } First(E) {(, id, num} Follow(E) = {$, )} Follow(E’) = {$, )} Follow(T) = {$, ), +} Follow(T) = {$, ), +} Follow(T’) = {$, ), +} Follow(F) = {*, $, ), +} E TE’: First(TE’) = {(, id, num}E’ +TE’: First(+TE’) = {+}E’ : Follow(E’) = {$,)}T FT’: First(FT’) = {(, id, num}T’ *FT’: First(*FT’) = {*}T’ : Follow(T’) = {$, ), +} id num * + ( ) $ E E TE’ E TE’ E TE’ E’ E’ +TE’ E’ E’ T T FT’ T FT’ T FT’ T’ T’ *FT’ T’ T’ T’ F F id F num F (E)
  • 18.  Now we can have a predictive parsing mechanism  Use a stack to keep track of the expanded form  Initialization  Put starting symbol S and $ into the stack  Add $ to the end of the input string  $ is for the recognition of the termination configuration  If ‘a’ is at the top of the stack and ‘a’ is the next input symbol then  Simply pop ‘a’ from stack and advance on the input string  If ‘A’ is on top of the stack and ‘a’ is the next input symbol then  Assume that M [A, a] = A  Replace A by in the stack  Termination  When only $ in the stack and in the input string  If ‘A’ is on top of the stack and ‘a’ is the next input but  M[A, a] = empty  Error Parsing procedure
  • 19. id num * + ( ) $ E E TE’ E TE’ E TE’ E’ E’ +TE’ E’ E’ T T FT’ T FT’ T FT’ T’ T’ *FT’ T’ T’ T’ F F id F num F (E) Stack Input Action E $ id + num * id $ E TE’ T E’ $ id + num * id $ T FT’ F T’ E’ $ id + num * id $ F id T’ E’ $ + num * id $ T’ E’ $ + num * id $ E’ +TE’ T E’ $ num * id $ T FT’ F T’ E’ $ num * id $ F num T’ E’ $ * id $ T’ *FT’ F T’ E’ $ id $ F id T’ E’ $ $ T’ E’ $ $ E’ $ $ Accept ERROR Example of Predictive Parsing: id + num * id