160 likes | 420 Views
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.
E N D
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 • 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.
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
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 .
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)
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
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
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
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
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
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
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
The End • Up Next: Predictive Parser