1 / 14

Top Down Parser

Top Down Parser. - Mandakinee Singh ( 11CS10026). Parser. What is parsing? Discovering the derivation of a string: If one exists. Harder than generating strings. Two major approaches Top-down parsing Bottom-up parsing. Top Down Parser.

dudley
Download Presentation

Top Down Parser

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Top Down Parser -Mandakinee Singh (11CS10026)

  2. Parser • What is parsing? • Discovering the derivation of a string: If one exists. • Harder than generating strings. • Two major approaches • Top-down parsing • Bottom-up parsing

  3. Top Down Parser • A parser is top-down if it discovers a parse tree top to bottom. • A top-down parse corresponds to a preorder traversal(preorder expansion) of the parse tree. • A leftmost derivation is applied at each derivation step. • 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.

  4. Grammar • Top Down Parser –LL(1) Grammar • LL(1) parsers • Left-to-right input • Leftmost derivation • 1 symbol of look-ahead Preorder Expansion: The Leftmost non terminal production will occur first. Grammars that this can handle are called LL(1) grammars

  5. Top Down Parser • Start with the root of the parse tree • Root of the tree: node labeled with the start symbol. • Algorithm: • Repeat until the fringe of the parse tree matches input string. • Declare a pointer which will represent the current position of the parser in string. • Start scanning character by character from left to right from the parse tree and match it with input string .

  6. Algorithm • If the scanned symbol is: • Terminal: Increase the pointer by one. • Non-Terminal: Go for a production. Add a child node for each symbol of chosen production. • If a terminal symbol is added that doesn’t match, backtrack. • Find the next node to be expanded (a non-terminal) • Repeat The process. • Done when: • Leaves of parse tree match input string (success) • All productions exhausted in backtracking (failure)

  7. Example • Grammar E E+T(rule 1) | E-T(2) | T(3) T T*F(4) | T/F (5)| F(6) F number(7) | Id(8) Input String:x-2*y

  8. Example • Problem: • Can’t match next terminal • We guessed wrong at step 2 E  x - 2 * y  x - 2 * y 1 E + T  x – 2 * y 3 T+ T T + T  x – 2 * y 6 F+ T x  – 2 * y 8 <Id> + T x  – 2 * y - <id,x> + T T F x

  9. Backtracking Go for next production.  x - 2 * y  x - 2 * y 1E+ T  x – 2 * y 3 T+ T Undo all these productions  x – 2 * y 6 F+ T x  – 2 * y 8 <Id> + T x  – 2 * y ? <Id,x> + T

  10. Retrying with a new production • Problem: • More input to read • Another cause of backtracking E  x - 2 * y  x - 2 * y 2 E - T E - T  x – 2 * y 3 T- T  x – 2 * y 6 F- T x  – 2 * y 8 <Id> - T T F x –  2 * y -<Id,x> - T x –  2 * y 6 <Id,x> - F x – 2  * y F 2 7<Id,x> - <num> x

  11. T * F F y 2 Successful parse All terminals matches- we are done. E  x - 2 * y  x - 2 * y 2 E- T  x – 2 * y 3 T - T E - T  x – 2 * y 6 For - T x  – 2 * y 8 <id> - T x –  2 * y - <id,x> - T T x –  2 * y 4 <id,x> - T * F x –  2 * y 6 <id,x> - F * F F x – 2  * y 7 <id,x> - <num> * F x – 2 *  y - <id,x> - <num,2> * F x – 2 * y  8 <id,x> - <num,2> * <id> x

  12. Left Recursion • If we see it carefully then there is one more possibility • Problem: Termination • Wrong choice leads to infinite expansion (More importantly: without consuming any input!) • May not be as obvious as this • Our grammar is left recursive  x - 2 * y  x - 2 * y 2 E + T  x – 2 * y 2 E + T + T  x – 2 * y 2 E + T + T + T  x – 2 * y 2 E + T + T + T + T

  13. Left Recursion • Formally, A grammar is left recursiveif a non-terminal A such that A →A a |b (for some set of symbolsa ) A→AAa A→AAAa ……………… A→AAAAAAAAAa A→bAAAAAA……AAAAAAAa • How to remove it: A →b A’ A’→aA’|e

  14. The End • Up Next: Predictive Parser

More Related