Recursive Descent Parsing

The following c program parser the given string for the given grammar::

E->E+T

E->T

T->T*F

T->F

F->(E)

F->i


Before writing writing recursive descent parser first eliminate left recursion and left factoring.

E->TEplus

Eplus->+TEplus / ∈ (epsilon)

T ->FTplus

Tplus->*FTplus / ∈ (epsilon)

F-> (E)

F-> i


#include<stdio.h>
#include<string.h>
char string[' '];
int len;
int pos=0;
int E();
int Eplus();
int T();
int Tplus();
int F();
int main()
{
printf("Enter the string:: ");
scanf("%s",&string);
len=strlen(string);
if(E()==0) printf("\nNot Accepted");
else if(pos<len) {printf("\nNot Accepted");}
else printf("\nAccepted");
printf("\n");
return 0;
}
int E()
{
if(T()==0) return 0;
if(Eplus()==0) return 0;
return 1;
}
int Eplus()
{
if(string[pos]=='+')
{ pos++;
if(T()==0) return 0;
if(Eplus()==0) return 0;
}
return 1;
}
int T()
{
if(F()==0) return 0;
if(Tplus()==0) return 0;
return 1;
}
int Tplus()
{
if(string[pos]=='*')
{ pos++;
if(F()==0) return 0;
if(Tplus()==0) return 0;
}
return 1;
}
int F()
{
if(string[pos]=='('){
pos++;
if(E()==0) return 0;
if(string[pos]==')') pos++;
else return 0;
}
else if(string[pos]=='i'){
pos++;}
else {return 0;}
return 1;
}

Don’t copy the code directly from this page it may contain HTML meta content.

Just download this text file(code)>>>>Download<<<<

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s