0% found this document useful (0 votes)
2 views23 pages

CSE2002 Session21 TopDownParsingSession2

The document covers top-down parsing techniques in compiler design, focusing on left recursion and left factoring. It explains how to eliminate left recursion from grammars and provides examples of left factoring to prepare grammars for predictive parsing. Additionally, it introduces recursive-descent parsing and poses review questions to reinforce understanding of the concepts presented.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views23 pages

CSE2002 Session21 TopDownParsingSession2

The document covers top-down parsing techniques in compiler design, focusing on left recursion and left factoring. It explains how to eliminate left recursion from grammars and provides examples of left factoring to prepare grammars for predictive parsing. Additionally, it introduces recursive-descent parsing and poses review questions to reinforce understanding of the concepts presented.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 23

Theory of Computation &

Compiler Design

Top Down Parsing


Dr Prakash P
VIT Chennai
Roadmap

 Recap
 Left Recursion
 Left Factoring
 Top Down Parsing
 Recursive Descent Parsing

Dr Prakash P VIT Chennai


Recap
 Grammar
 Context Free Grammar
 Derivation(s)
 Sentence
 Sentential Form
 Derivation Tree
 Ambiguous Grammar
 Role of Parser
 Types of Parsing

Dr Prakash P VIT Chennai


Left Recursion
A grammar is left recursive if it has a nonterminal A such that
there is a derivation A + Aα for some string α.

Example EE+T|T
TT*F|F
F  ( E ) | id

the form 𝐴 → 𝐴𝛼
 Direct/Immediate left recursion: There is a production of

 Indirect left recursion: First symbol on the right-hand side


of a rule can derive the symbol on the left

Top-down parsing methods cannot handle left-recursive


grammars, so a transformation is needed to eliminate left
recursion.

Dr Prakash P VIT Chennai


Remove Left Recursion
Rule:

** Without changing the strings derivable from A

Dr Prakash P VIT Chennai


Remove Left Recursion – Example 1
EE+T|T
TT*F|F
F  ( E ) | id S(L)/a
L L , S / S

Dr Prakash P VIT Chennai


Indirect Left Recursion
Left recursion hidden due to many productions.
 There is a left recursion
For example:
 S  Aa | b A Ac | Sd | Є because
S Aa Sda

In such cases, left recursion is removed systematically


 Starting from the first rule and replacing all the occurrences of the first
non terminal symbol
 Removing left recursion from the modified grammar

After the first step (substitute S by its rhs in the rules) the grammar
becomes
S Aa | b A  Ac | Aad | bd | Є
After the second step (removal of left recursion) the grammar
becomes S  Aa | b
A bdA' | A'
A'  cA' | adA' | Є
Dr Prakash P VIT Chennai
Indirect Left Recursion
 A → BaB  An equivalent
 B → Cb | ɛ immediately left-
 C → Ab | Ac recursive grammar,

Dr Prakash P VIT Chennai


Algorithm to eliminate left recursion
 INPUT: Grammar G with no cycles or epsilon-
productions.
 OUTPUT: An equivalent grammar with no left recursion.

Dr Prakash P VIT Chennai


Left Factoring
 Left factoring is a grammar transformation that is useful for
producing a grammar suitable for predictive, or top-down, parsing.
 When the choice between two alternative A-productions is not
clear,
 Rewrite the productions to defer the decision until enough of the input
has been seen that we can make the right choice.

Left factoring is the process of extracting and isolating common prefixes


in a set of productions

Dr Prakash P VIT Chennai


Left Factoring – Example
 S  i E t S | i E tSeS | a  A → aAB / aBc / aAc
 Eb

• S  i E tSS’ | a • A → aA’
• S’  e S | ɛ • A’ → AB / Bc / Ac
• Eb

• A → aA’
• A’ → AD / Bc
• D→B/c

Dr Prakash P VIT Chennai


Algorithm : Left factoring a
grammar
 INPUT: Grammar G.
 OUTPUT: An equivalent left-factored grammar.

Dr Prakash P VIT Chennai


 Recursive-descent parsing is a form of top-down parsing
Recursive-Descent
Parsing

that may require backtracking.


 Consists of a set of procedures, one for each nonterminal

Dr Prakash P VIT Chennai


Grammar: E --> i E'
E' --> + i E' | Ɛ
E()
RDP Example
{
if (l == 'i') {
match('i');
int main() E'();
{ }
// E is a start symbol. }
E();

// if lookahead = $, it represents the end


of the string
// Here l is lookahead. E'()
if (l == '$') {
printf("Parsing Successful"); if (l == '+') {
} match('+'); match(char t)
match('i'); {
E'(); if (l == t) {
} l = getchar();
else }
return (); else
} printf("Error");
}
Dr Prakash P VIT Chennai
Recursive-Descent
Parsing
The start symbol is S;
The only terminal symbols are a and b.

 For example, consider the grammar S


 aAB|bB A aA|b B  b

Dr Prakash P VIT Chennai


Recursive-Descent Parsing
Example

Scanner has function getToken() and global variable


token Dr Prakash P VIT Chennai
Review Question -1
Consider the following E -> E - T | T
T -> T + F | F
expression grammar G. F -> (E) | id

Which of the following grammars are not left recursive,


but equivalent to G?

A)
C)
E -> E - T | T
E -> TX
T -> T + F | F X -> -TX | ε
F -> (E) | id T -> FY
Y -> +FY | ε
B) F -> (E) | id
E -> TE'
D)
E' -> -TE' | ε
E -> TX | (TX)
T -> T + F | F X -> -TX | +TX | ε
F -> (E) | id T -> id

Dr Prakash P VIT Chennai


Review Question -1
Consider the following E -> E - T | T
T -> T + F | F
expression grammar G. F -> (E) | id

Which of the following grammars are not left recursive,


but equivalent to G?

A)
C)
E -> E - T | T
E -> TX
T -> T + F | F X -> -TX | ε
F -> (E) | id T -> FY
Y -> +FY | ε
B) F -> (E) | id
E -> TE'
D)
E' -> -TE' | ε
E -> TX | (TX)
T -> T + F | F X -> -TX | +TX | ε
F -> (E) | id T -> id

Dr Prakash P VIT Chennai


Review Question -2
 Which of the following derivations does a
top-down parser use while parsing an input
string?
a) Leftmost derivation
b) Leftmost derivation in reverse
c) Rightmost derivation
d) Rightmost derivation in reverse

Dr Prakash P VIT Chennai


Review Question -2
 Which of the following derivations does a
top-down parser use while parsing an input
string?
a) Leftmost derivation
b) Leftmost derivation in reverse
c) Rightmost derivation
d) Rightmost derivation in reverse

Dr Prakash P VIT Chennai


References

 Principles of Compiler Design, Alferd V. Aho


and Jeffery D. Ullman, Addison Wesley,2006.

 Modern Compiler Implementation in Java, 2nd


ed., Andrew W. Appel Cambrdige University
Press, 2012.

Dr Prakash P VIT Chennai


Next…

 Limitations with Recursive-Descent


Parsing
 FIRST and FOLLOW
 LL(1) Parser

Dr Prakash P VIT Chennai


Thanks

Dr Prakash P VIT Chennai

You might also like