110 likes | 328 Views
20-8-2013. Mohit Senapaty. Top Down parser. There are two types of Top-Down Parser Recursive Descent Parser Predictive Parser
E N D
20-8-2013 MohitSenapaty
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.
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
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.
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
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).
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.
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
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
Example (contd.) • First(F) = {(, id} • First(T) = {є, id} • First(E) = {є, id} • First(T’) = {є, *} • First(E’) = {є, +}
END OF SLIDE MohitSenapaty 11CS10029