Download presentation
Presentation is loading. Please wait.
Published byMarilyn Berry Modified over 9 years ago
1
-Mandakinee Singh (11CS10026)
2
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
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
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
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
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
Grammar E E+T(rule 1) | E-T(2) | T(3) TT*F(4) | T/F (5)| F(6) Fnumber(7) | Id(8) Input String:x-2*y
8
Problem: ◦ Can’t match next terminal ◦ We guessed wrong at step 2 RuleSentential formInput string - E E T x + T F T 1 E + T x - 2 * y 3 T + T x – 2 * y 6 F + T x – 2 * y 8 + T x – 2 * y - + T x – 2 * y x - 2 * y
9
Go for next production. RuleSentential formInput string - E 1 E + T x - 2 * y 3 T + T x – 2 * y 6 F + T x – 2 * y 8 + T x – 2 * y ? + T x – 2 * y x - 2 * y Undo all these productions
10
Problem: ◦ More input to read ◦ Another cause of backtracking RuleSentential formInput string - E E E x - T F T 2 E - T x - 2 * y 3 T - T x – 2 * y 6 F - T x – 2 * y 8 - T x – 2 * y - - T x – 2 * y x - 2 * y 6 - F x – 2 * y 7 - x – 2 * y F 2
11
All terminals matches- we are done. RuleSentential formInput string - E E E x - T F T 2 E - T x - 2 * y 3 T - T x – 2 * y 6 For - T x – 2 * y 8 - T x – 2 * y - - T x – 2 * y x - 2 * y 4 - T * F x – 2 * y 6 - F * F x – 2 * y 2 7 - * F x – 2 * y F - - * F x – 2 * y 8 - * x – 2 * y T * F y
12
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 RuleSentential formInput string - E 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 x – 2 * y x - 2 * y
13
Formally, A grammar is left recursive if a non-terminal A such that A → A | b (for some set of symbols ) A → AA A → AAA ……………… A → AAAAAAAAA A → bAAAAAA……AAAAAAA How to remove it: A → b A’ A’ → A’|
14
Up Next: Predictive Parser
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.