Unit 2 - Session 6
Unit 2 - Session 6
Unit 2 - Session 6
COMPILER DESIGN
UNIT 2
SESSION 6
Topics that will be covered in this Session
• Top-down parsing can also be viewed as finding a leftmost derivation for an input string
• At each step of a top-down parsing, the key problem is that of determining the production to be
applied for a nonterminal, say A.
• Once an A-production is chosen, the rest of the parsing process consists of matching the
terminal symbols in the production body with the input string
Top Down Parsing - Example
• Consider the grammar
Example
id + id * id
Top Down Parsing – Cont..
• Recursive-descent parsing is a general form of top-down parsing
• Predictive parsing chooses the correct A-production by looking ahead at the input a fixed
number of symbols
• Typically, we may look only at one (that is, the next input symbol)
• In the previous example, at the first E′ node, the production E′ → + T E′, at the second E′ node,
the production E′ → ε is chosen
• A predictive parser can choose between E′ productions by looking at the next input symbol
• The class of grammars for which we can construct predictive parsers looking k symbols ahead
in the input is sometimes called the LL(k) grammar
RECURSIVE DESCENT
PARSING & BACKTRACKING
Recursive-Descent Parsing
• Recursive-descent parsing is a general form of top-down parsing, that may involve
backtracking, i.e., making repeated scans of the input
• When we cannot choose a unique A-production, we must try each of several productions in
some order
• Only if there are no more A-productions to try, we declare that an input error has been found
NOTE:
• A left-recursive grammar can cause a recursive-descent parser, even one with backtracking, to
go into an infinite loop. That is, when we try to expand a nonterminal A, we may eventually
find ourselves again trying to expand A without having consumed any input
Recursive-Descent Parsing - Example
• Consider the grammar
• To construct a parse tree top-down for the input string w = cad, begin with a tree consisting of
a single node labeled S, and the input pointer pointing to c, the first symbol of w
• S has only one production, so we use it to expand S and obtain the tree
• Consider the next leaf labeled A. Now we expand A using the first alternative A → a b
Recursive-Descent Parsing - Example
• We have a match for the second input symbol, a, so we advance the input pointer to d, the third
input symbol, and compare d against the next leaf, labeled b
• Since b does not match d, we report failure and go back to A to see whether there is another
alternative for A that has not been tried, but that might produce a match
• Go back to A and reset the input pointer to pos 2. The 2nd alternative for A produces the tree
• The leaf a matches the second symbol of w and the leaf d matches the third
symbol. Since we have produced a parse tree for w, we halt and announce
successful completion of parsing
Recursive-Descent Parsing – Example 2
• Consider the grammar
S→(L)|a
L → ( L ) L′ | a L′
L′ → , S L′ | ε