You are on page 1of 3

Ex.

No:
Date:

RECURSIVE DESCENT PARSING USING C

PROBLEM DESCRIPTION:
Write a C program to perform recursive descent or predictive parsing for the given string using
the given recursive descent parser table.
THEORY :
A parser that uses collection of recursive procedures for parsing the given input string is called
recursive descent parser. In RD parser, the CFG is used to build the recursive routines. The
R.H.S of the production rule is directly converted to a program.
For each non terminal a separate procedure is written and body of the procedure is RHS of the
corresponding non terminal.
ELIMINATION OF LEFT RECURSION:
A Grammer is left recursive if it has a non terminal A such that there is a derivation
A=>A for some string . Recursive parsing cannot handle left recursive grammers, so a
transformation that eliminates left recursion is needed.
Any left recursive production of the form A-> | could be replaced by the non leftrecursive productions without changing a set of strings derivable from A. Thus eliminating left
recursion in the given grammar, we obtain
A->A
A-> A |
To FIND FIRST(X):
To compute FIRST(X) for all grammer symbols X apply the following rules until no more
terminals or can be added to any FIRST set .
If X is terminal ,then FIRST(X) is {X}
If X-> is a production then add to FIRST (X)
If X is a non terminal and X->Y1,Y2,Y3YK is a production then place a in FIRST(X) if for
some I ,a is in FIRST(Y) and is in all of FIRST(Y1) .,FIRST(YI-1); that is Y1YI-1=> .If
is in FIRST(Yj) for all j=1,2,k,then add to FIRST(X)
To FIND FOLLOW(X):
RULES TO COMPUTE FOLLOW SET
1) If S is the start symbol, then add $ to the FOLLOW(S).
2) If there is a production rule A-->B then everything in FIRST() except for
is placed in FOLLOW(B).
3) If there is a production A-->B, or a production A-->B whereFIRST() contains
then everything in FOLLOW(A) is in FOLLOW(B).

TO BUILD PREDICTIVE PARSER TABLE:


For every production A in the grammar:
1. If

can derive a string starting with a (i.e., for all a in FIRST( ) ,

Table [A, a] = A
2. If can derive the empty string, , then, for all b that can follow a string derived from A
(i.e., for all b in FOLLOW (A) ,
Table [A,b] = A
Test Case: <by the student>
Program: <By the Student written once the program gets successfully executed in the lab>
Output: <By the Student>
Result: <By the Student>
Review Questions:
1. What is meant by recursive descent parser?
2. List down the difficulties with top down parsing.
3. Define LL (1) grammar.