To Implement Predictive Parsing in C

praneeth_gunda 1 Tallied Votes 26K Views Share

This Program implements the Predictive Parsing Of the grammar
E->E+T/T
F->F*T/F
F->id(Identifier)

//To Implement Predictive Parsing
#include<string.h>
#include<conio.h>
char a[10];
int top=-1,i;
void error(){
printf("Syntax Error");
}
void push(char k[]) //Pushes The Set Of Characters on to the Stack
{
  for(i=0;k[i]!='\0';i++)
  {
    if(top<9)
    a[++top]=k[i];
  }
}
char TOS()        //Returns TOP of the Stack
{
  return a[top];
}
void pop()       //Pops 1 element from the Stack
{
  if(top>=0)
    a[top--]='\0';
}
void display()  //Displays Elements Of Stack
{
  for(i=0;i<=top;i++)
    printf("%c",a[i]);
}
void display1(char p[],int m) //Displays The Present Input String
{
  int l;
  printf("\t");
  for(l=m;p[l]!='\0';l++)
    printf("%c",p[l]);
}
char* stack(){
return a;
}
int main()
{
  char ip[20],r[20],st,an;
  int ir,ic,j=0,k;
  char t[5][6][10]={"$","$","TH","$","TH","$",
		   "+TH","$","e","e","$","e",
		   "$","$","FU","$","FU","$",
		   "e","*FU","e","e","$","e",
		   "$","$","(E)","$","i","$"};
  clrscr();
  printf("\nEnter any String(Append with $)");
  gets(ip);
  printf("Stack\tInput\tOutput\n\n");
  push("$E");
  display();
  printf("\t%s\n",ip);
  for(j=0;ip[j]!='\0';)
  {
  if(TOS()==an)
      {
	pop();
	display();
	display1(ip,j+1);
	printf("\tPOP\n");
	j++;
      }
    an=ip[j];
    st=TOS();
      if(st=='E')ir=0;
      else if(st=='H')ir=1;
      else if(st=='T')ir=2;
      else if(st=='U')ir=3;
      else if(st=='F')ir=4;
      else {
	    error();
	    break;
	    }
      if(an=='+')ic=0;
      else if(an=='*')ic=1;
      else if(an=='(')ic=2;
      else if(an==')')ic=3;
      else if((an>='a'&&an<='z')||(an>='A'&&an<='Z')){ic=4;an='i';}
      else if(an=='$')ic=5;
      strcpy(r,strrev(t[ir][ic]));
      strrev(t[ir][ic]);
      pop();
      push(r);
      if(TOS()=='e')
      {
	pop();
	display();
	display1(ip,j);
	printf("\t%c->%c\n",st,238);
      }
      else{
      display();
      display1(ip,j);
      printf("\t%c->%s\n",st,t[ir][ic]);
      }
      if(TOS()=='$'&&an=='$')
      break;
      if(TOS()=='$'){
	error();
	break;
	}
      }
      k=strcmp(stack(),"$");
      if(k==0 && i==strlen(ip))
    printf("\n Given String is accepted");
    else
    printf("\n Given String is not accepted");
  return 0;
}
hariharanb 0 Newbie Poster

For which input will this program work?

commented: every input which except yours +0
ayushigangwal17 0 Newbie Poster

what should be the input?
can plz rply fast

viziworld 0 Newbie Poster

hey this program is not correct this is just blow your head.

suraiyaparveen 0 Newbie Poster

i want the exact program to eliminate ambiguity,left recursion, left factoring and also for construction of transition diagram

Schol-R-LEA 1,446 Commie Mutant Traitor Featured Poster

suraiyaparveen: You won't get it, for two reasons. First, no such program exists - those are all things that need to be done manually, not programs to be written. Second, even if it were feasible to do those things programmatically, we wouldn't give it to you without some evidence that you had put in a good faith effort to solve the problems on your own first. Daniweb is not a free homework cheating service, and no one here is going to do your work for you.

Also, don't go reviving long-dead threads. Thread necromancy is considered extremely rude on pretty much every message board on the web, and is just one more reason for most of the people here to ignore you or worse.

rubberman commented: Ah! Digital necromancy! Time to raise the long dead! +0
Istiaq 0 Newbie Poster

Would you please tell us the input procedure of this program? How the input is given?

adarsh mitra commented: no +0
Rahul_65 0 Newbie Poster

Above program needs a fix , As per my anayl.
[fix]Replace line 108 of above program with if(k==0) :

                       if(k==0 && i==strlen(ip)) to if(k==0)

Any I/P will work which can be generated by above grammer for example try
I/P : 1. a+bc$ (accepted) 2. a+bc (Not Accepted) 3. a+b*$ (Not Accepted)

sushma25 0 Newbie Poster

input can be anything like a+b$ or a*b$ etc

yesim 0 Newbie Poster

would you tell us input?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.