The document discusses predictive parsing, which constructs a top-down parser that does not backtrack. It involves left recursion elimination, left factoring, and ensuring LL(1) compatibility of the grammar. Predictive parsing relies on information about the first symbols that can be generated by each production to unambiguously determine the production selected at each step without backtracking.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0 ratings0% found this document useful (0 votes)
39 views14 pages
Understanding Predictive Parsing
The document discusses predictive parsing, which constructs a top-down parser that does not backtrack. It involves left recursion elimination, left factoring, and ensuring LL(1) compatibility of the grammar. Predictive parsing relies on information about the first symbols that can be generated by each production to unambiguously determine the production selected at each step without backtracking.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14
Understanding Predictive Parsing
Preparing to Construct the Predictive Parsing
Table Left Recursion Elimination Left Factoring LL(1) compatibility Predictive Parsing The goal of predictive parsing is to construct a top-down parser that does not backtrack The lookahead symbol (token) unambiguously determines the production selected for each non-terminal Vandana, CSIS, BITS Pilani January 24, 2013 2 Predictive Parsing and lookahead symbol The lookahead symbol is scanned from left to right The left recursive grammar always produces the same nonterminal and cannot be used to build predictive parsers The grammar rules are redefined These rules eliminate most common causes for backtracking
Vandana, CSIS, BITS Pilani January 24, 2013 3 Predictive Parsing Predictive parsing relies on information about what first symbols can be generated by the right side of a production We try collecting all terminals that could be generated first by all productions for a non terminal The decision making is based on the next lookahead and the selection of the rule is made based on the entries in the parsing table
Vandana, CSIS, BITS Pilani January 24, 2013 4 Predictive Parsing Let a rule be A where (alpha) is the sequence of terminal and non terminal variables at the RHS of the rule
Define FIRST() as the set of terminals (tokens) that appear as the first symbols of one or more strings generated from Follow-set of A, written as Follow(A), is defined as the set of terminals a such that there is a string of symbols Aa that can be derived from the start symbol.
Vandana, CSIS, BITS Pilani January 24, 2013 5 Example Consider the grammar S ASB SaA | h A cA | dB B eA | fB |
Vandana, CSIS, BITS Pilani
Verify the Input: cacdfed FIRST(S)= {a,h,c,d} FIRST(A)={c,d} FIRST(B)={e,f, } January 24, 2013 6 Example: construct FIRST sets Consider the grammar S ABSB SaA | h A cA | dB | B eA | fB
January 24, 2013 7 Example: construct FOLLOW sets Consider the grammar S ABS$ SaA | h A cA | dB | B eA | fB
Vandana, CSIS, BITS Pilani
FOLLOW(S)= {$} FOLLOW(A) = FIRST(B) FOLLOW(B)= FIRST(S) Parse the string
ecfh January 24, 2013 8 Predictive Parsing The grammar rules are redefined with following aspects 1. Eliminate left recursion, and 2. Perform left factoring. example : Compute FIRST(statement) <Statement> IF <condition> THEN <Statement> <Statement> IF <condition> THEN <Statement> ELSE <Statement>
Vandana, CSIS, BITS Pilani January 24, 2013 9 Class assignment Parse the following input to verify whether the grammar is ambiguous if E1 then S2 else if E2 then S2 else S3 Vandana, CSIS, BITS Pilani January 24, 2013 10 Left factored grammar Can be left factored as <Statement> IF <condition>THEN <Statement> <Elsepart> <Elsepart> | ELSE <Statement > Vandana, CSIS, BITS Pilani January 24, 2013 11 Parser The parser consists of an input buffer, a string from the grammar a stack on which to store the terminals and non- terminals from the grammar yet to be parsed a parsing table which tells it what (if any) grammar rule to apply given the symbols on top of its stack and the next input token Vandana, CSIS, BITS Pilani January 24, 2013 12 Parser The parser applies the rule found in the table by matching the top-most symbol on the stack (row) with the current symbol in the input stream (column).
Vandana, CSIS, BITS Pilani January 24, 2013 13 Sample Parsing table Input : ( 1 + 1 ) Parsing table for this grammar ( ) 1 + $ S 2 - 1 - - F - - 3 - - Grammar 1. S F 2. S ( S + F ) 3. F 1