Parsing Part II Topdown parsing leftrecursion removal Parsing
![Parsing — Part II (Top-down parsing, left-recursion removal) Parsing — Part II (Top-down parsing, left-recursion removal)](https://slidetodoc.com/presentation_image/2e7fd2a2ead6b6d8d4f5c6bf71845d7e/image-1.jpg)
![Parsing Techniques Top-down parsers • • Start at the root of the parse tree Parsing Techniques Top-down parsers • • Start at the root of the parse tree](https://slidetodoc.com/presentation_image/2e7fd2a2ead6b6d8d4f5c6bf71845d7e/image-2.jpg)
![Top-down Parsing A top-down parser starts with the root of the parse tree The Top-down Parsing A top-down parser starts with the root of the parse tree The](https://slidetodoc.com/presentation_image/2e7fd2a2ead6b6d8d4f5c6bf71845d7e/image-3.jpg)
![Remember the expression grammar? Version with precedence derived last lecture And the input x Remember the expression grammar? Version with precedence derived last lecture And the input x](https://slidetodoc.com/presentation_image/2e7fd2a2ead6b6d8d4f5c6bf71845d7e/image-4.jpg)
![Example Let’s try x – 2 * y : Leftmost derivation, choose productions in Example Let’s try x – 2 * y : Leftmost derivation, choose productions in](https://slidetodoc.com/presentation_image/2e7fd2a2ead6b6d8d4f5c6bf71845d7e/image-5.jpg)
![Example Let’s try x – 2 * y : This worked well, except that Example Let’s try x – 2 * y : This worked well, except that](https://slidetodoc.com/presentation_image/2e7fd2a2ead6b6d8d4f5c6bf71845d7e/image-6.jpg)
![Example Continuing with x – 2 * y : Example Continuing with x – 2 * y :](https://slidetodoc.com/presentation_image/2e7fd2a2ead6b6d8d4f5c6bf71845d7e/image-7.jpg)
![Example Continuing with x – 2 * y : Now, we need to expand Example Continuing with x – 2 * y : Now, we need to expand](https://slidetodoc.com/presentation_image/2e7fd2a2ead6b6d8d4f5c6bf71845d7e/image-8.jpg)
![Example Trying to match the “ 2” in x – 2 * y : Example Trying to match the “ 2” in x – 2 * y :](https://slidetodoc.com/presentation_image/2e7fd2a2ead6b6d8d4f5c6bf71845d7e/image-9.jpg)
![Example Trying to match the “ 2” in x – 2 * y : Example Trying to match the “ 2” in x – 2 * y :](https://slidetodoc.com/presentation_image/2e7fd2a2ead6b6d8d4f5c6bf71845d7e/image-10.jpg)
![Example Trying again with “ 2” in x – 2 * y : This Example Trying again with “ 2” in x – 2 * y : This](https://slidetodoc.com/presentation_image/2e7fd2a2ead6b6d8d4f5c6bf71845d7e/image-11.jpg)
![Another possible parse Other choices for expansion are possible This doesn’t terminate consuming no Another possible parse Other choices for expansion are possible This doesn’t terminate consuming no](https://slidetodoc.com/presentation_image/2e7fd2a2ead6b6d8d4f5c6bf71845d7e/image-12.jpg)
![Left Recursion Top-down parsers cannot handle left-recursive grammars Formally, A grammar is left recursive Left Recursion Top-down parsers cannot handle left-recursive grammars Formally, A grammar is left recursive](https://slidetodoc.com/presentation_image/2e7fd2a2ead6b6d8d4f5c6bf71845d7e/image-13.jpg)
![Eliminating Left Recursion To remove left recursion, we can transform the grammar Consider a Eliminating Left Recursion To remove left recursion, we can transform the grammar Consider a](https://slidetodoc.com/presentation_image/2e7fd2a2ead6b6d8d4f5c6bf71845d7e/image-14.jpg)
![Eliminating Left Recursion The expression grammar contains two cases of left recursion Applying the Eliminating Left Recursion The expression grammar contains two cases of left recursion Applying the](https://slidetodoc.com/presentation_image/2e7fd2a2ead6b6d8d4f5c6bf71845d7e/image-15.jpg)
![Eliminating Left Recursion Substituting them back into the grammar yields • This grammar is Eliminating Left Recursion Substituting them back into the grammar yields • This grammar is](https://slidetodoc.com/presentation_image/2e7fd2a2ead6b6d8d4f5c6bf71845d7e/image-16.jpg)
![Eliminating Left Recursion The transformation eliminates immediate left recursion What about more general, indirect Eliminating Left Recursion The transformation eliminates immediate left recursion What about more general, indirect](https://slidetodoc.com/presentation_image/2e7fd2a2ead6b6d8d4f5c6bf71845d7e/image-17.jpg)
![Eliminating Left Recursion How does this algorithm work? 1. Impose arbitrary order on the Eliminating Left Recursion How does this algorithm work? 1. Impose arbitrary order on the](https://slidetodoc.com/presentation_image/2e7fd2a2ead6b6d8d4f5c6bf71845d7e/image-18.jpg)
![Example • Order of symbols: G, E, T G E E +T E E Example • Order of symbols: G, E, T G E E +T E E](https://slidetodoc.com/presentation_image/2e7fd2a2ead6b6d8d4f5c6bf71845d7e/image-19.jpg)
![Example • Order of symbols: G, E, T 1. Ai = G G E Example • Order of symbols: G, E, T 1. Ai = G G E](https://slidetodoc.com/presentation_image/2e7fd2a2ead6b6d8d4f5c6bf71845d7e/image-20.jpg)
![Example • Order of symbols: G, E, T 1. Ai = G 2. Ai Example • Order of symbols: G, E, T 1. Ai = G 2. Ai](https://slidetodoc.com/presentation_image/2e7fd2a2ead6b6d8d4f5c6bf71845d7e/image-21.jpg)
![Example • Order of symbols: G, E, T 1. Ai = G 2. Ai Example • Order of symbols: G, E, T 1. Ai = G 2. Ai](https://slidetodoc.com/presentation_image/2e7fd2a2ead6b6d8d4f5c6bf71845d7e/image-22.jpg)
![Example • Order of symbols: G, E, T 1. Ai = G 2. Ai Example • Order of symbols: G, E, T 1. Ai = G 2. Ai](https://slidetodoc.com/presentation_image/2e7fd2a2ead6b6d8d4f5c6bf71845d7e/image-23.jpg)
- Slides: 23
![Parsing Part II Topdown parsing leftrecursion removal Parsing — Part II (Top-down parsing, left-recursion removal)](https://slidetodoc.com/presentation_image/2e7fd2a2ead6b6d8d4f5c6bf71845d7e/image-1.jpg)
Parsing — Part II (Top-down parsing, left-recursion removal)
![Parsing Techniques Topdown parsers Start at the root of the parse tree Parsing Techniques Top-down parsers • • Start at the root of the parse tree](https://slidetodoc.com/presentation_image/2e7fd2a2ead6b6d8d4f5c6bf71845d7e/image-2.jpg)
Parsing Techniques Top-down parsers • • Start at the root of the parse tree and grow toward leaves Pick a production & try to match the input Bad “pick” may need to backtrack Some grammars are backtrack-free Bottom-up parsers • • (LL(1), recursive descent) (predictive parsing) (LR(1), operator precedence) Start at the leaves and grow toward root As input is consumed, encode possibilities in an internal state Start in a state valid for legal first tokens Bottom-up parsers handle a large class of grammars
![Topdown Parsing A topdown parser starts with the root of the parse tree The Top-down Parsing A top-down parser starts with the root of the parse tree The](https://slidetodoc.com/presentation_image/2e7fd2a2ead6b6d8d4f5c6bf71845d7e/image-3.jpg)
Top-down Parsing A top-down parser starts with the root of the parse tree The root node is labeled with the goal symbol of the grammar Top-down parsing algorithm: Construct the root node of the parse tree Repeat until the fringe of the parse tree matches the input string At a node labeled A, select a production with A on its lhs and, for each symbol on its rhs, construct the appropriate child When a terminal symbol is added to the fringe and it doesn’t match the fringe, backtrack Find the next node to be expanded (label NT) • The key is picking the right production in step 1 That choice should be guided by the input string
![Remember the expression grammar Version with precedence derived last lecture And the input x Remember the expression grammar? Version with precedence derived last lecture And the input x](https://slidetodoc.com/presentation_image/2e7fd2a2ead6b6d8d4f5c6bf71845d7e/image-4.jpg)
Remember the expression grammar? Version with precedence derived last lecture And the input x – 2 * y
![Example Lets try x 2 y Leftmost derivation choose productions in Example Let’s try x – 2 * y : Leftmost derivation, choose productions in](https://slidetodoc.com/presentation_image/2e7fd2a2ead6b6d8d4f5c6bf71845d7e/image-5.jpg)
Example Let’s try x – 2 * y : Leftmost derivation, choose productions in an order that exposes problems
![Example Lets try x 2 y This worked well except that Example Let’s try x – 2 * y : This worked well, except that](https://slidetodoc.com/presentation_image/2e7fd2a2ead6b6d8d4f5c6bf71845d7e/image-6.jpg)
Example Let’s try x – 2 * y : This worked well, except that “–” doesn’t match “+” The parser must backtrack to here
![Example Continuing with x 2 y Example Continuing with x – 2 * y :](https://slidetodoc.com/presentation_image/2e7fd2a2ead6b6d8d4f5c6bf71845d7e/image-7.jpg)
Example Continuing with x – 2 * y :
![Example Continuing with x 2 y Now we need to expand Example Continuing with x – 2 * y : Now, we need to expand](https://slidetodoc.com/presentation_image/2e7fd2a2ead6b6d8d4f5c6bf71845d7e/image-8.jpg)
Example Continuing with x – 2 * y : Now, we need to expand Term - the last NT on the fringe
![Example Trying to match the 2 in x 2 y Example Trying to match the “ 2” in x – 2 * y :](https://slidetodoc.com/presentation_image/2e7fd2a2ead6b6d8d4f5c6bf71845d7e/image-9.jpg)
Example Trying to match the “ 2” in x – 2 * y :
![Example Trying to match the 2 in x 2 y Example Trying to match the “ 2” in x – 2 * y :](https://slidetodoc.com/presentation_image/2e7fd2a2ead6b6d8d4f5c6bf71845d7e/image-10.jpg)
Example Trying to match the “ 2” in x – 2 * y : Where are we? • “ 2” matches “ 2” • We have more input, but no NTs left to expand • The expansion terminated too soon Need to backtrack
![Example Trying again with 2 in x 2 y This Example Trying again with “ 2” in x – 2 * y : This](https://slidetodoc.com/presentation_image/2e7fd2a2ead6b6d8d4f5c6bf71845d7e/image-11.jpg)
Example Trying again with “ 2” in x – 2 * y : This time, we matched & consumed all the input Success!
![Another possible parse Other choices for expansion are possible This doesnt terminate consuming no Another possible parse Other choices for expansion are possible This doesn’t terminate consuming no](https://slidetodoc.com/presentation_image/2e7fd2a2ead6b6d8d4f5c6bf71845d7e/image-12.jpg)
Another possible parse Other choices for expansion are possible This doesn’t terminate consuming no input ! (obviously) • Wrong choice of expansion leads to non-termination • Non-termination is a bad property for a parser to have • Parser must make the right choice
![Left Recursion Topdown parsers cannot handle leftrecursive grammars Formally A grammar is left recursive Left Recursion Top-down parsers cannot handle left-recursive grammars Formally, A grammar is left recursive](https://slidetodoc.com/presentation_image/2e7fd2a2ead6b6d8d4f5c6bf71845d7e/image-13.jpg)
Left Recursion Top-down parsers cannot handle left-recursive grammars Formally, A grammar is left recursive if A NT such that a derivation A + A , for some string (NT T )+ Our expression grammar is left recursive • This can lead to non-termination in a top-down parser • For a top-down parser, any recursion must be right recursion • We would like to convert the left recursion to right recursion Non-termination is a bad property in any part of a compiler
![Eliminating Left Recursion To remove left recursion we can transform the grammar Consider a Eliminating Left Recursion To remove left recursion, we can transform the grammar Consider a](https://slidetodoc.com/presentation_image/2e7fd2a2ead6b6d8d4f5c6bf71845d7e/image-14.jpg)
Eliminating Left Recursion To remove left recursion, we can transform the grammar Consider a grammar fragment of the form Fee | where neither nor start with Fee We can rewrite this as Fee Fie Fie | where Fie is a new non-terminal This accepts the same language, but uses only right recursion
![Eliminating Left Recursion The expression grammar contains two cases of left recursion Applying the Eliminating Left Recursion The expression grammar contains two cases of left recursion Applying the](https://slidetodoc.com/presentation_image/2e7fd2a2ead6b6d8d4f5c6bf71845d7e/image-15.jpg)
Eliminating Left Recursion The expression grammar contains two cases of left recursion Applying the transformation yields These fragments use only right recursion They retain the original left associativity
![Eliminating Left Recursion Substituting them back into the grammar yields This grammar is Eliminating Left Recursion Substituting them back into the grammar yields • This grammar is](https://slidetodoc.com/presentation_image/2e7fd2a2ead6b6d8d4f5c6bf71845d7e/image-16.jpg)
Eliminating Left Recursion Substituting them back into the grammar yields • This grammar is correct, if somewhat non-intuitive. • It is left associative, as was the original • A top-down parser will terminate using it. • A top-down parser may need to backtrack with it.
![Eliminating Left Recursion The transformation eliminates immediate left recursion What about more general indirect Eliminating Left Recursion The transformation eliminates immediate left recursion What about more general, indirect](https://slidetodoc.com/presentation_image/2e7fd2a2ead6b6d8d4f5c6bf71845d7e/image-17.jpg)
Eliminating Left Recursion The transformation eliminates immediate left recursion What about more general, indirect left recursion ? The general algorithm: arrange the NTs into some order A 1, A 2, …, An for i 1 to n for s 1 to i – 1 replace each production Ai As with Ai 1 2 k , where As 1 2 k are all the current productions for As eliminate any immediate left recursion on Ai using the direct transformation This assumes that the initial grammar has no cycles (Ai + Ai ), and no epsilon productions And back
![Eliminating Left Recursion How does this algorithm work 1 Impose arbitrary order on the Eliminating Left Recursion How does this algorithm work? 1. Impose arbitrary order on the](https://slidetodoc.com/presentation_image/2e7fd2a2ead6b6d8d4f5c6bf71845d7e/image-18.jpg)
Eliminating Left Recursion How does this algorithm work? 1. Impose arbitrary order on the non-terminals 2. Outer loop cycles through NT in order 3. Inner loop ensures that a production expanding Ai has no non -terminal As in its rhs, for s < i 4. Last step in outer loop converts any direct recursion on Ai to right recursion using the transformation showed earlier 5. New non-terminals are added at the end of the order & have no left recursion At the start of the ith outer loop iteration For all k < i, no production that expands Ak contains a non-terminal As in its rhs, for s < k
![Example Order of symbols G E T G E E T E E Example • Order of symbols: G, E, T G E E +T E E](https://slidetodoc.com/presentation_image/2e7fd2a2ead6b6d8d4f5c6bf71845d7e/image-19.jpg)
Example • Order of symbols: G, E, T G E E +T E E T T ~T E T id
![Example Order of symbols G E T 1 Ai G G E Example • Order of symbols: G, E, T 1. Ai = G G E](https://slidetodoc.com/presentation_image/2e7fd2a2ead6b6d8d4f5c6bf71845d7e/image-20.jpg)
Example • Order of symbols: G, E, T 1. Ai = G G E E +T E E T T ~T E T id
![Example Order of symbols G E T 1 Ai G 2 Ai Example • Order of symbols: G, E, T 1. Ai = G 2. Ai](https://slidetodoc.com/presentation_image/2e7fd2a2ead6b6d8d4f5c6bf71845d7e/image-21.jpg)
Example • Order of symbols: G, E, T 1. Ai = G 2. Ai = E G E E +T E E T T ~T E T id E T E' E' + T E' E' T T E~ T id
![Example Order of symbols G E T 1 Ai G 2 Ai Example • Order of symbols: G, E, T 1. Ai = G 2. Ai](https://slidetodoc.com/presentation_image/2e7fd2a2ead6b6d8d4f5c6bf71845d7e/image-22.jpg)
Example • Order of symbols: G, E, T 1. Ai = G 2. Ai = E 3. Ai = T, As = E G E E +T E E T T ~T E T id E T E' E' + T E' E' E' T T E~ T T T E' ~ T id Go to Algorithm
![Example Order of symbols G E T 1 Ai G 2 Ai Example • Order of symbols: G, E, T 1. Ai = G 2. Ai](https://slidetodoc.com/presentation_image/2e7fd2a2ead6b6d8d4f5c6bf71845d7e/image-23.jpg)
Example • Order of symbols: G, E, T 1. Ai = G 2. Ai = E 3. Ai = T, As = E 4. Ai = T G E E +T E E T T ~T E T id E T E' E' + T E' E' T T E~ T id T T T E' ~ T id T' T' E' ~ T T' T'
Top-down processing vs bottom-up processing
For top down parsing left recursion removal is
For top-down parsing left recursion removal is
Left recursion
Cvpr paper list
Parts of the back bar
Part to part ratio definition
The phase of the moon you see depends on ______.
Part part whole
Minitab adalah
Part by part technical description example
Part part whole addition
Advantages of bottom up parsing
Visual studio regular expression
Parsing adalah
Parsing and translation in query processing
Left corner parser
Predictive parsing
Parsing
Parsing algorithms in nlp
Semantic parsing
Probabilistic parsing
Panic mode error recovery in predictive parsing
Lr parsing table example