1 / 11

20-8-2013

20-8-2013. Mohit Senapaty. Top Down parser. There are two types of Top-Down Parser Recursive Descent Parser Predictive Parser

anila
Download Presentation

20-8-2013

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. 20-8-2013 MohitSenapaty

  2. Top Down parser • There are two types of Top-Down Parser • Recursive Descent Parser • Predictive Parser Recursive Descent Parser involves back-tracking as we had learnt in the previous class. But Predictive Parser involves Prediction of the Production of the Grammar involved.

  3. Predictive Parser: • Applicable for Following types of Grammar: • Non Ambiguous Grammar: A Predictive parser cannot be made for ambiguous grammar. • Non Left Recursive Grammar: Left Recursive Grammar are not suitable for this Parser. • LL(1) Grammar

  4. LL(1) Grammar • The first L in LL(1) refers to the fact that the input is processed from left to right. • The second L refers to the fact that LL(1) parsing determines a leftmost derivation for the input string. • The 1 in parentheses implies that LL(1) parsing uses only one symbol of input to predict the next grammar rule that should be used.

  5. Designing a Predictive Parser • We need to make a parse table which has top row as the set of terminals or input symbols. • The left-most column should have the set of all Non-Terminals. • The table should be filled with the set of all Productions corresponding to the Non-Terminals and the input symbol

  6. Predictive Parser table • We use the parsing table to decide which decision should be made if a given nonterminal N is at the top of the parsing stack, based on the current input symbol T. • To construct the table we need two types of sets constructed from the grammar. • First Sets: For a sentential form w the set First(w) consists of any terminal at the left end of w or at the left end of any sentential form derived from w, including ٨ if w is ٨ or w derives ٨. • Follow Sets: If A is a nonterminal, then Follow(A) is the set of terminals to the right of A in sentential forms derived from the start symbol S, where we include $ in Follow(S).

  7. First() and Follow() Rules • If X is terminal, then a є First(X) • If X is Non-terminal, X derives epsilon production, then (є) є First(X) • If X is Non-terminal and X -> Y1Y2..Yi, where Yi may be Terminal or Non-Terminal, then, First(X) = First(Yi), I = 1, 2,…, n. If Yi is not a epsilon production.

  8. Example • Grammar: E -> E + E| E*E | (E) | id • Generate: id + id * id • The above grammar is ambiguous as the Production can be derived by using two parse trees. • New Grammar: E-> E + T | T • T -> T * F | F • F -> (E) | id

  9. Example (contd.) • The derived Grammar is Left Recursive. So we need to convert it into non-left recusrsive. • E -> TE’ • E’ -> +TE’ | epsilon • T -> FT’ • T’ -> *FT’| epsilon • F -> (E) | id

  10. Example (contd.) • First(F) = {(, id} • First(T) = {є, id} • First(E) = {є, id} • First(T’) = {є, *} • First(E’) = {є, +}

  11. END OF SLIDE MohitSenapaty 11CS10029

More Related