0% found this document useful (0 votes)
9 views15 pages

Top Down

Uploaded by

kondareddy
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)
9 views15 pages

Top Down

Uploaded by

kondareddy
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/ 15

Top-down parsing:

Recursive descent parsing :


• It is a common form of top-down parsing.
• It is called recursive as it uses recursive procedures to process the
input.
• Recursive descent parsing suffers from backtracking.

Backtracking :
• It means, if one derivation of a production fails, the syntax
analyzer restarts the process using different rules of same
production.
• This technique may process the input string more than once to
determine the right production.
102
104
Example:
S → if E then S else S
| while E do S
| print
E→ true | False | id
The input token string is:
If id then while true do print else
print.

105
107
108
Backtracking:
• Top- down parsers start from the root node (start symbol) and
match the input string against the production rules to replace
them (if matched).
• To understand this, take the following example of CFG
S → rXd | rZd
X → oa | ea
Z→ ai

109
For an input string: read,
A top-down parser, will behave like this:
• It will start with S from the production rules and will match its yield
to the left-most letter of the input, i.e. ‘r’.
• The very production of S (S → rXd) matches with it. So the top-
down parser advances to the next input letter (i.e. ‘e’).
• The parser tries to expand non-terminal ‘X’ and checks
its production from the left (X → oa).
• It does not match with the next input symbol. So the top-
down parser backtracks to obtain the next production rule
of X, (X → ea).
• Now the parser matches all the input letters in an ordered manner.
The string is accepted.
110
111
112
Recursive-descent parsing:
Recursive descent parser is a top-down parser.
• It requires backtracking to find the correct production to be applied.
• The parsing program consists of a set of procedures, one for each
non-terminal.
• Process begins with the procedure for start symbol.
• Start symbol is placed at the root node and on encountering each
non- terminal, the procedure concerned is called to expand the non-
terminal with its corresponding production.
• Procedure is called recursively until all non-terminals are expanded.
• Successful completion occurs when the scan over entire input
string is done. ie., all terminals in the sentence are derived by
parse tree
113
Algorithm for Recursive-descent parsing:
void A()
{
choose an A-production, A ----> X1 X2 X3...Xk;
for (i = 1 to k)
if (Xi is a non-terminal)
call procedure Xi ();
else if (Xi equals the current input
symbol a) advance the input
to the next symbol;
else error;
}
115
Example for recursive decent parsing:
• A left-recursive grammar can cause a recursive-descent
parser to go into an infinite loop.
• Hence, elimination of left-recursion must be done
before parsing.
• Consider the grammar for arithmetic expressions
E → E+T | T
T → T*F | F
F → (E) | id

116
After eliminating the left-recursion the grammar
becomes,
E → TE’
E’ → +TE’ |Ɛ
T → FT’
T’ → *FT’ | Ɛ
F → (E) | id
Now we can write the procedure for grammar as
follows:

117
Recursive procedure: Recursive procedure:
Procedure E() EPRIME( );
begin end Procedure
T( ); T( ) begin
EPRIME( ); F( );
End TPRIME();
Procedure EPRIME( ) end
begin Procedure TPRIME( )
If input symbol=’+’ then begin
ADVANCE( ); If input_symbol=’*’
T( ); then

118
Recursive procedure: Recursive procedure:
ADVANCE( ); E( );
F( ); else if input-symbol=’)’ then
TPRIME( ); ADVANCE( );
end Procedure end
F( ) begin
else
If input-symbol=’id’ then
ERROR( );
ADVANCE( );
else if input-symbol=’(‘ then
ADVANCE( );

120

You might also like