Top Down Parsing Algorithms Lecture 9 Topics Covered
- Slides: 42
Top Down Parsing Algorithms Lecture 9
Topics Covered in Lecture 8
n Concept about top down parsing n Using derivations to find a parse tree for any input n Concept about back tracking n When to use back tracking while making a parse tree. 3
Recursive Descent Parsing A general form of top-down parsing where backtracking may be involved. n Involves repeated attempts to get the correct output n brute-force type of parsing n outdated, just because there are much better methods of parsing which we will be discussing later n 4
How RDP Works ? Learn by Example n Consider the grammar: S -> c. Ad | bd n A -> ab | a n n Input string is “cad”. 5
How RDP Works ? Learn by Example n Consider the grammar: n n S S -> c. Ad | bd A -> ab | a c n A d Input string is “cad”. c Matched ! ! 6
How RDP Works ? Learn by Example n Consider the grammar: n n S S -> c. Ad | bd A -> ab | a c n A d Input string is “cad”. a b a Matched ! ! 7
How RDP Works ? Learn by Example n Consider the grammar: n n S S -> c. Ad | bd A -> ab | a c n A d Input string is “cad”. a b b and d do not Match ! ! 8
How RDP Works ? Learn by Example n Consider the grammar: n n n S S -> c. Ad | bd A -> ab | a c A d Input string is “cad”. Backtrack and try alternative production 9
How RDP Works ? Learn by Example n Consider the grammar: n n S S -> c. Ad | bd A -> ab | a c n A d Input string is “cad”. a a Matched ! ! 10
How RDP Works ? Learn by Example n Consider the grammar: n n n S S -> c. Ad | bd A -> ab | a c A d Input string is “cad”. a d Matched ! ! 11
Problem with RDP n n E E+T |E–T |T Input string 2+5 -9 E + E + E T T T Infinite loop E + T 12
Hence, we can conclude n A left recursive grammar shall cause a recursive descent parser to go in an infinite loop n Also, finding the right option while exhausting all choices of productions may consume un necessary time n Therefore, we need to improve the algorithm 13
Predictive Parsing n A special case of recursive descent parsing that needs no back tracking n Obtained as a result of eliminating left recursion from a grammar and left factoring it. n The proper production is predicted on the basis of its first symbol. 14
Transition Diagram n Purpose: n Transition Diagram is used as a plan for predictive parser One diagram for each non – terminal n Edges are labeled by tokens or non terminals n Transition on a terminal n n n Take transition if the token is the next input symbol Transition on non – terminal n Call of procedure for the non - terminal 15
How to construct a TD for predictive parser n Step 1 : n Eliminate left – recursion from the grammar n Step 2 : n Left factor the grammar n Step 3: n For each non – terminal create an initial and final state n For each production create a path from an initial state to the final state. 16
Learn by Example n Original Grammar n n n E→ E + T | T T→ T * F | F F→ ( E ) | id n Transformed Grammar n n n E TE ’ E’ + TE ’ |Є T FT ’ T’ * FT ’ |Є F (E) | id 17
Learn by Example n Grammar T E: E TE’ n E’ + TE’ |Є E’ 0 1 2 + T E’ n n E’: 3 4 5 6 Є T FT’ T’ * FT’ |Є F (E) | id 18
Learn by Example n Grammar n n E TE’ E’ + TE’ |Є T FT’ n T’ * FT’ |Є F T: n n T’: 7 10 T’ 8 * 9 F 11 T’ 12 13 Є F (E) | id 19
Learn by Example n Grammar n n n E TE’ E’ + TE’ |Є T FT’ T’ * FT’ |Є E ( F: 14 15 ) 16 17 id F (E) | id 20
Finally n Grammar E TE’ n E’ + TE’ |Є n T FT’ n T’ * FT’ |Є n F (E) | id n T E: E’: 0 3 E’ 1 + 2 T E’ 4 5 6 Є F T: T’: 7 10 T’ 8 * 9 F 11 T’ 12 13 Є E ( F: 14 15 ) 16 17 id 21
Lets Simplify and Optimize our Transition Diagrams
Optimizing Transition Diagram – Learn by Example n Grammar E TE’ n E’ +TE’ |Є n T FT’ n T’ * FT’ |Є n F (E) | id n T E: E’: 0 3 E’ 1 + 2 T 4 E’ 5 6 Є Lets Optimize E ! 23
Optimizing Transition Diagram – Learn by Example n Grammar E’: T + 3 4 E’ 5 6 Є E TE’ n E’ + TE’ |Є n T FT’ n T’ * FT’ |Є n F (E) | id Є n E’: + 3 T 4 5 Є 6 T E’: 3 + Є 4 6 24
Optimizing Transition Diagram – Learn by Example n Grammar E TE’ n E’ + TE’ |Є n T FT’ n T’ * FT’ |Є n F (E) | id T 0 E: E’ 1 2 T n + 3 E’: 4 Є 6 T E: 0 T 3 + Є 4 6 25
Optimizing Transition Diagram – Learn by Example T n Grammar 0 E: E TE’ n E’ + TE’ |Є n T FT’ n T’ * FT’ |Є n F (E) | id T + 3 n 4 Є 6 + E: 0 T 3 Є 6 26
Optimizing Transition Diagram – Learn by Doing n Grammar E TE’ n E’ + TE’ |Є n T FT’ n T’ * FT’ |Є n F (E) | id n T: T’: 7 10 8 * 9 F 11 T’ 12 13 Є See if you can optimize T ! 27
Optimizing Transition Diagram – Learn by *Doing. F T’ n Grammar E TE’ n E’ + TE’ |Є n T FT’ n T’ * FT’ |Є n F (E) | id T’: 10 11 12 13 Є Є n T’: 10 * F 11 Є 12 13 F T’: 10 * Є 11 13 28
Optimizing Transition Diagram – Learn by Doing n Grammar T’ F 7 T: 8 9 F E TE’ n E’ + TE’ |Є n T FT’ n T’ * FT’ |Є n F (E) | id n T’: 10 * Є 11 13 F T: 7 F 10 * Є 11 13 29
Optimizing Transition Diagram – Learn by Doing F n Grammar T: E TE’ n E’ + TE’ |Є n T FT’ n T’ * FT’ |Є n F (E) | id 7 F 10 n * Є 11 13 * T: 7 F 10 Є 13 30
Optimizing Transition Diagram – Learn by Doing n Grammar E TE’ n E’ + TE’ |Є n T FT’ n T’ * FT’ |Є n F (E) | id n E ( F: 14 15 ) 16 17 id See if you can optimize F ! Can not be further optimized!!! 31
Optimized Transition Diagrams + n Grammar E TE’ n E’ + TE’ |Є n T FT’ n T’ * FT’ |Є n F (E) | id E: 0 T 3 Є * n T: 7 F 10 Є 14 13 E ( F: 6 15 ) 16 17 id 32
Why should we do optimizing at all ? n Because !!! n An implementation of un - optimized transition diagram for a predictive parser is 20 % slower than that of a optimized transition diagram !!! 33
Learn by Doing Grammar Expr + Term | Expr - Term | Term n n Set of terminals n n Set of Non terminals n Term * num | Term / num | num n { +, -, *, /, Int } { Start, Expr, Term } Draw optimized transition diagrams for implementing a predictive parser for this grammar !!! 34
Let’s Revise !!!
Recursive Descent Parsing A general form of top-down parsing where backtracking may be involved. n Involves repeated attempts to get the correct output n brute-force type of parsing n outdated, just because there are much better methods of parsing which we will be discussing later n 36
n Why does Recursive Descent Parser fail at particular conditions ? n Solution : We need a predictive parser 37
Predictive Parsing n A special case of recursive descent parsing that needs no back tracking n Obtained as a result of eliminating left recursion from a grammar and left factoring it. n The proper production is predicted on the basis of its first symbol. 38
Transition Diagram as a plan for Predictive Parsing
How to construct a TD for predictive parser n Step 1 : n Eliminate left – recursion from the grammar n Step 2 : n Left factor the grammar n Step 3: n For each non – terminal create an initial and final state n For each production create a path from an initial state to the final state. 40
n Need for optimizing Transition Diagrams n Know the reason why !! 41
Homework : The language for which you have built your lexical analyzer must have some rules for construction of valid statements. n Explain the rules in terms of productions (e. g. the example done for TD was a rule for arithmetic expressions) n Make Transition Diagrams and optimize them n This will become the first step towards building your parser !! n 42
- Advantages of bottom up parsing
- Error recovery in predictive parsing
- Top-down parser
- For top down parsing left recursion removal is
- Top down parsing algorithm
- Top down parsing vs bottom up
- For top down parsing left recursion removal is
- Parsing in nlp
- Eliminating left recursion
- Parsing algorithms in nlp
- What topics will be covered in this unit
- Echinoderms spiny skin
- Analysis of algorithms lecture notes
- Introduction to algorithms lecture notes
- 01:640:244 lecture notes - lecture 15: plat, idah, farad
- Fungsi dari top-down adalah
- Top-down theory
- Top down data warehouse
- Top down processing
- Top down networking
- Top down control
- Top down design example
- Conclusão de um trabalho
- Bottom up profiling
- Recursive descent parser
- Top down adalah
- Bottom up regulation
- Contoh it audit
- Top down versus bottom up psychology
- Unity top down shooter tutorial
- Top down konstruktion
- Modular design programming
- Top-down approach psychology
- Top down forecasting
- Bottom up reading approach
- Dxx18
- Call graph based integration in software testing
- Prosedur audit bottom-up dan audit top-down
- Bago bumasa habang bumabasa pagkatapos bumasa
- Top down and bottom up approach
- These are techniques or activities that contribute directly
- Top down design examples
- Top down design creo training