Date post: | 21-Jul-2016 |
Category: |
Documents |
Upload: | preethinarmi |
View: | 221 times |
Download: | 1 times |
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
Vandana, CSIS, BITS Pilani
FIRST(S)= {a,h,c,d,e,f}
FIRST(A)={c,d, ε}
FIRST(B)={e,f,} Parse strings
1. cf
2. ae
3. eac
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
14 Vandana, CSIS, BITS Pilani January 24, 2013