Tuesday, November 6, 2007

S R Parser

Program : S R Parser



#include<stdio.h>

#include<process.h>

#include<conio.h>

#include<string.h>


void main() //main function

{

int i,j,len;

char str[20],sta[20];

int k;

clrscr();

printf("\nEnter the string:\t");

gets(str);

len=strlen(str); //get string length

j=0;



for(i=0;i<len;i++)

{

printf("\n%s",sta);



if(str[i]=='a')

{

sta[j]='E';

j++; //increment stack pointer

printf("\n%s",sta);


if((sta[j-2]=='+')||(sta[j-2]=='-')||(sta[j-2]=='*')||(sta[j-2]=='/'))

{

if(sta[j-3]=='E')

{

sta[j-2]=' ';

sta[j-1]=' ';

j=j-2; //decrement stack pointer by 2

}


else

{

if(j>2)

{

printf("\nSTRING NOT ACCEPTED");

getch();

exit(0);

}

}

}


else if(sta[j-2]=='E')

{

printf("\nSTRING NOT ACCEPTED");

getch();

exit(0);

}


else

{

if(j>2)

{

printf("\nSTRING NOT ACCEPTED");

getch();

exit(0);


}

}


} //end of if(id)


else if(str[i]=='+')

{

sta[j]='+';

j++; //increment stack pointer


}


else if(str[i]=='-')

{

sta[j]='-';

j++; //increment stack pointer


}


else if(str[i]=='/')

{

sta[j]='/';

j++; //increment stack pointer


}


else if(str[i]=='*')

{

sta[j]='*';

j++; //increment stack pointer


}

} //end of for loop


printf("\nE\nSTRING ACCEPTED");

getch();

} //end of main function

0 comments: