What is Recursive Descent Parser?



Recursive Descent Parser uses the technique of Top-Down Parsing without backtracking. It can be defined as a Parser that uses the various recursive procedure to process the input string with no backtracking. It can be simply performed using a Recursive language. The first symbol of the string of R.H.S of production will uniquely determine the correct alternative to choose.

The major approach of recursive-descent parsing is to relate each non-terminal with a procedure. The objective of each procedure is to read a sequence of input characters that can be produced by the corresponding non-terminal, and return a pointer to the root of the parse tree for the non-terminal. The structure of the procedure is prescribed by the productions for the equivalent non-terminal.

The recursive procedures can be simply to write and adequately effective if written in a language that executes the procedure call effectively. There is a procedure for each non-terminal in the grammar. It can consider a global variable lookahead, holding the current input token and a procedure match (Expected Token) is the action of recognizing the next token in the parsing process and advancing the input stream pointer, such that lookahead points to the next token to be parsed. Match () is effectively a call to the lexical analyzer to get the next token.

For example, input stream is a + b$.

lookahead == a

match()

lookahead == +

match ()

lookahead == b

……………………….

……………………….

In this manner, parsing can be done.

Example − In the following Grammar, first symbol, i.e., if, while & begin uniquely determine, which of the alternative to choose.

As Statement starting with if will be a conditional statement & statement starting with while will be an Iterative Statement.

                                                Stmt → If condition then Stmt else Stmt

                                                             | While condition do Stmt

                                                             | begin Stmt end.

Example − Write down the algorithm using Recursive procedures to implement the following Grammar.

E → TE′

E′ → +TE′

T → FT′

T′ →∗ FT′|ε

F → (E)|id

One of major drawback or recursive-descent parsing is that it can be implemented only for those languages which support recursive procedure calls and it suffers from the problem of left-recursion.


Advertisements