Monday, October 17

Recursive Descent Parser using C program


Updated on 09/02/15

Hello reader,

Here is the updated post considering your valuable suggestions. check it out.


The grammar on which we are going to do recursive descent parsing is:

E -> E+T | T
T -> T*F | F
F -> (E) | id
  RD parser will verify whether the syntax of the input stream is correct by checking each character  from left to right. A basic operation necessary is reading characters from the input stream and matching then with terminals from the grammar that describes the syntax of the input.

 The given grammar can accept all arithmetic equations involving +, * and ().
eg:
 a+(a*a)  a+a*a , (a), a , a+a+a*a+a.... etc are accepted
a++a, a***a, +a, a*, ((a . . . etc are rejected.

Solution:
First we have to avoid left recursion

E -> TE'
E' -> +TE' | ε
T -> FT'
T' -> *FT' | ε
F -> (E) | id

After eliminating Left recursion, we have to simply move from one character to next by checking whether it follow the grammar. In this program, Îµ is indicated as $.


recursive.c

#include<stdio.h>
#include<string.h>

#include<ctype.h>
 
char input[10];
int i,error;
void E();
void T();
void Eprime();
void Tprime();
void F();
 
          main()
          {

i=0;
error=0;
                printf("Enter an arithmetic expression   :  "); // Eg: a+a*a
                gets(input);
                E();
                if(strlen(input)==i&&error==0)

                        printf("\nAccepted..!!!\n");
                else printf("\nRejected..!!!\n");
                          }
         
         
 
void E()
{
     T();
     Eprime();
}

void Eprime()
{
     if(input[i]=='+')
     {
     i++;
     T();
     Eprime();
     }
     }
void T()
{
     F();
     Tprime();
}
void Tprime()
{
     if(input[i]=='*')
     {
                      i++;
                      F();
                      Tprime();
                      }
                      }
     void F()
     {
          if(
isalnum(input[i]))i++;
          else if(input[i]=='(')
          {
          i++;
          E();
          if(input[i]==')')
          i++;


          else error=1;
            }
        
          else error=1;
          }
           


Output:
 a+(a*a)  a+a*a , (a), a , a+a+a*a+a.... etc are  accepted
++a, a***a, +a, a*, ((a . . . etc are rejected.


40 comments :

v!p!n said... Best Blogger Tips [Reply to comment] Best Blogger Templates

sample output
a+(a*a)

Accepted!!

Uchiha said... Best Blogger Tips [Reply to comment] Best Blogger Templates

Accepts (((a
that cannot be generated by the grammar

Anonymous said... Best Blogger Tips [Reply to comment] Best Blogger Templates

void F()
{
if(input[i]=='a')i++;
else if(input[i]=='(')
{
i++;
E();
if(input[i]==')')
i++;
else
error=1;
}

else error=1;
}

Bhargav said... Best Blogger Tips [Reply to comment] Best Blogger Templates

it showing that rejected

Anonymous said... Best Blogger Tips [Reply to comment] Best Blogger Templates

if i want to enter the string not only 'a' but "a" to "z" and "A" to "Z"

Anonymous said... Best Blogger Tips [Reply to comment] Best Blogger Templates

#include
#include
char input[10];
int i=0,error=0;
void E();
void T();
void Eprime();
void Tprime();
void F();
main()
{
printf("Enter an arithmetic expression : ");
gets(input);
E();
if(strlen(input)==i&&error==0)printf("\nAccepted..!!!");
else printf("\nRejected..!!!");
}



void E()
{
T();
Eprime();
}
void Eprime()
{
if(input[i]=='+')
{
i++;
T();
Eprime();
}
}
void T()
{
F();
Tprime();
}
void Tprime()
{
if(input[i]=='*')
{
i++;
F();
Tprime();
}
}
void F()
{
if(input[i]=='a'&&input[i]=='z'||input[i]=='A'&&input[i]=='Z')i++;
else if(input[i]=='(')
{
i++;
E();
if(input[i]==')')
i++;
}

else error=1;
}
this program can be used for any string which is consisting of small letters as well as capital letter.....

Unknown said... Best Blogger Tips [Reply to comment] Best Blogger Templates

//this code is for all the characters
void F()
{
if((input[i]>64&&input[i]<91)||input[i]>96&&input[i]<123)i++;
else if(input[i]=='(')
{
i++;
E();
if(input[i]==')')
i++;
}

Anonymous said... Best Blogger Tips [Reply to comment] Best Blogger Templates

will this string be accepted or rejected:a+a*a

Unknown said... Best Blogger Tips [Reply to comment] Best Blogger Templates

@rohit crackulous
How can the amendment to this code so that if we have introduced outputs the output figures

Unknown said... Best Blogger Tips [Reply to comment] Best Blogger Templates

plz give complete information of output

Anonymous said... Best Blogger Tips [Reply to comment] Best Blogger Templates

hi

Anonymous said... Best Blogger Tips [Reply to comment] Best Blogger Templates

its not working for any input string.....there something wrong with code .

v!p!n said... Best Blogger Tips [Reply to comment] Best Blogger Templates

@Uchiha Hello Uchiha.. Thank you for spotting the bug... It is fixed now..!!

v!p!n said... Best Blogger Tips [Reply to comment] Best Blogger Templates

Hello @sachin patil ,
The Saple input output test cases has been included in updated post.. Check it out

v!p!n said... Best Blogger Tips [Reply to comment] Best Blogger Templates

Hello @Anonymous , can you please comment for which input string you have tried

v!p!n said... Best Blogger Tips [Reply to comment] Best Blogger Templates

Hello @Anonymous, You can use isalnum(your_character) to check whether it is an alphanumeric. your suggestion has been included in the updated code.

Anonymous said... Best Blogger Tips [Reply to comment] Best Blogger Templates

excellent

v!p!n said... Best Blogger Tips [Reply to comment] Best Blogger Templates

Thank youAnonymous for your feedback

Anonymous said... Best Blogger Tips [Reply to comment] Best Blogger Templates

fucking nerds

Anonymous said... Best Blogger Tips [Reply to comment] Best Blogger Templates

#include
#include

void E(),E1(),T(),T1(),F();

int ip=0;
static char s[10];

int main()
{
char k;
int l;
ip=0;
printf("Enter the string:\n");
scanf("%s",s);
printf("The string is: %s",s);
E();
if(s[ip]=='$')
printf("\nString is accepted.\nThe length of the string is %d\n",strlen(s)-1);
else
printf("\nString not accepted.\n");
return 0;
}

void E()
{
T();
E1();
return;
}

void E1()
{
if(s[ip]=='+')
{
ip++;
T();
E1();
}
return;
}

void T()
{
F();
T1();
return;
}

void T1()
{
if(s[ip]=='*')
{
ip++;
F();
T1();
}
return;
}

void F()
{
if(s[ip]=='(')
{
ip++;
E();
if(s[ip]==')')
{
ip++;
}
}
else if(s[ip]=='i')
ip++;
else
printf("\nId expected ");
return;
}

Anonymous said... Best Blogger Tips [Reply to comment] Best Blogger Templates

khb

Anonymous said... Best Blogger Tips [Reply to comment] Best Blogger Templates

it rejects a+a string!!

v!p!n said... Best Blogger Tips [Reply to comment] Best Blogger Templates

@Anonymous
I tried..
It is accepting a+a

Anonymous said... Best Blogger Tips [Reply to comment] Best Blogger Templates

@Anonymous
vanam

Unknown said... Best Blogger Tips [Reply to comment] Best Blogger Templates

Hello sir. This coding style makes me puke. I am currently on my mobile and it is very hard to read (even on my laptop).

I don't understand how hardcoding every 'rule' solves the problem.

Unknown said... Best Blogger Tips [Reply to comment] Best Blogger Templates

plz upload correct code asap a++ is not working

Unknown said... Best Blogger Tips [Reply to comment] Best Blogger Templates

kay rao code ach chalat nahe shatta

Unknown said... Best Blogger Tips [Reply to comment] Best Blogger Templates

tender nako marat jao re....changla code asal tarach upload karat ja....lavda pana nako karu

Anonymous said... Best Blogger Tips [Reply to comment] Best Blogger Templates

@abhishek deshpande
ho na rao kya chuta code takla .......bhau sapdala tar kar send ....yetha....aai ghal vipin cha blog che

Anonymous said... Best Blogger Tips [Reply to comment] Best Blogger Templates

You vipin Mother f****r you have no right to play with student future ...I used your code in practical exam and i failed....

Unknown said... Best Blogger Tips [Reply to comment] Best Blogger Templates

After Running That Code I am getting This warning
b7.c: In function ‘main’:
b7.c:16:1: warning: ‘gets’ is deprecated (declared at /usr/include/stdio.h:638) [-Wdeprecated-declarations]
gets(input);
^
/tmp/ccD9kUtA.o: In function `main':
b7.c:(.text+0x19): warning: the `gets' function is dangerous and should not be used.

Unknown said... Best Blogger Tips [Reply to comment] Best Blogger Templates
This comment has been removed by the author.
Unknown said... Best Blogger Tips [Reply to comment] Best Blogger Templates

if i input a + a Rejected, i know its because using gets(input); if im using scanf("%c",input); or scanf("%s",input); and i input a a is Accpeted,
how if i do:
1. input a + a (Accepted)
2. input a a (Rejected)

can you help me?

Unknown said... Best Blogger Tips [Reply to comment] Best Blogger Templates

how to check whitespace of this function:

void F()
{
if(isalnum(input[i]))
{
i++;
if(input[i] == ' ' && isalnum(input[i+1]))
{
error = 1;
}
}
else if(input[i]=='(')
{
i++;
E();
if(input[i]==')')
{
i++;
}
else
{
error = 1;
}
}
else
{
error=1;
}

nikhila said... Best Blogger Tips [Reply to comment] Best Blogger Templates

no it did not accept the string you mentioned it gave me the correct output

Polisetty Sai Teja said... Best Blogger Tips [Reply to comment] Best Blogger Templates

this is predictive parser not recursive descent parser

Anonymous said... Best Blogger Tips [Reply to comment] Best Blogger Templates

lol there is a way indded ou gotta make a them for yourself in editor

Anonymous said... Best Blogger Tips [Reply to comment] Best Blogger Templates

@Bhargav

Dont know and dont care

Anonymous said... Best Blogger Tips [Reply to comment] Best Blogger Templates

@Uchihamandan

Anonymous said... Best Blogger Tips [Reply to comment] Best Blogger Templates
This comment has been removed by a blog administrator.

Post a Comment