heloo any body
i try to wrote a slr parser with c,but don't correctly work,my code is :

#include<stdio.h>
#include<ctype.h>
#include<conio.h>
#include<stdlib.h>
#include<string.h>
#include<iostream.h>

#define epsilon '^'

char prod[20][20],T[20],NT[20],c[10][10],foll[10][10],fir[10][10];
int tt,tnt,tp,a;
int follow[20][20],first[20][20];
void first_of(char);
int count(int j);
void rhs(int j);
void read_tnt();


void read_tnt()
{
 cout<<"For SLR parser: ";
 cout<<"\nEnter number of terminals: ";
 cin>>tt;
 cout<<"\nEnter terminals: ";
 for(int i=0;i<tt;i++)
  T[i]=getche();
 getch();
 cout<<"\nEnter number of Non-terminals: ";
 cin>>tnt;
 cout<<"\nEnter Non-terminals: ";
 for(i=0;i<tnt;i++)
  NT[i]=getche();
 getch();
}

void read_prod()
{
 int j;
 char x=0;
 cout<<"\n\nEnter number of productions: ";
 cin>>tp;
 cout<<"\n Enter productions: ";
 for(int i=0;i<tp;i++)
 {
  j=x=0;
  while(x!='\r')
  {
   prod[i][j]=x=getche();
   j++;
  }
  cout<<"\n";
 }
 getch();
}

int nt_no(char n)
{
 for(int i=0;i<tnt;i++)
 if(NT[i]==n)
   return(i);
 return(-1);
}

int t_no(char t)
{
 for(int i=0;i<tt;i++)
 if(T[i]==t)
  return(i);
 if(t=='$')
  return(tt);
 return(-1);
}

int terminal(char x)
{
 for(int i=0;i<tt;i++)
 if(T[i]==x)
   return(1);
 return(0);
}

int nonterminal(char x)
{
 for(int i=0;i<tnt;i++)
 if(NT[i]==x)
  return(1);
 return(0);
}

int in_rhs(char *s,char x)
{
 for(int i=0;i<=strlen(s);i++)
 if(*(s+i)==x)
  return(i);
 return(-1);
}

void find_first()
{
 for(int i=0;i<tnt;i++)
  first_of(NT[i]);
}

void first_of(char n)
{
 int t1,t2,p1,cnt=0,i,j;
 char x;
 static int over[20];
 p1=t_no(epsilon);
 if(terminal(n))
   return;
 t1=nt_no(n);
 if(over[t1])
  return;
 over[t1]=1;
 for(i=0;i<tp;i++)
 {
  t1=nt_no(prod[i][0]);
  if(prod[i][0]==n)
  {
   int k=0;
   cnt=count(1);
   rhs(i);
   while(k<cnt)
   {
    x=c[i][k];
    if(terminal(x))
    {
     t2=t_no(x);
     first[t1][t2]=1;
     break;
    }
    else
    {
     t2=nt_no(x);
     first_of(x);
     for(int j=0;j<tt;j++)
      if(p1!=j && first[t2][j])
	first[t1][j]=1;
      if(p1!=-1 && first[t2][p1])
	k++;
      else
	break;
     }
   }
   if(p1!=-1 && k>=cnt)
     first[t1][p1]=1;
  }
 }
}

void follow_of(char n)
{
 int f,t1,t2,p1,t,cnt=0;
 char x,beta;
 static int over[20];
 p1=t_no(epsilon);
 t1=nt_no(n);
 if(over[t1])
  return;
 over[t1]=1;
 if(NT[0]==n)
  follow[nt_no(NT[0])][tt]=1;
 for(int i=0;i<tp;i++)
 {
  rhs(i);
  cnt=count(i);
  t=in_rhs(c[i],n);
  if(t==-1)
   continue;
  for(int k=t+1;k<=cnt;k++)
  {
   rhs(i);
   beta=c[i][k];
   if(terminal(beta))
   {
    t2=t_no(beta);
    follow[t1][t2]=1;
    break;
   }
   int bno;
   for(int j=0;j<tt;j++)
   {
    bno=nt_no(beta);
    if((first[bno][j]) && (j!=p1))
      follow[t1][j]=1;
   }
   if((p1!=-1) && (first[bno][p1]==1))
     continue;
   else if((t==(cnt-1)||(k>=cnt)))
   {
    follow_of(prod[i][0]);
    t1=nt_no(prod[i][0]);
    for(int l=0;l<=tt+1;l++)
    if(follow[t][l])
      follow[t1][l]=1;
   }
  }
 }
}


int count(int j)
{
 int c1=0;
 for(int q=3;prod[j][q]!='\r';q++)
  c1++;
 return(c1);
}

void rhs(int j)
{
 int a,h=0;
 a=j;
 for(int q=3;prod[j][q]!='\r';q++)
 {
  c[a][h]=prod[j][q];
  h++;
 }
}

void find_follow()
{
 for(int i=0;i<tnt;i++)
  follow_of(NT[i]);
}

void show_follow()
{
 int b=0;
 a=0;
 cout<<"\n\n Follow Table For Grammar: \n";
 for(int i=0;i<tnt;i++)
 {
  b=0;
  cout<<"\n FOLLOW ("<<NT[i]<<" )= { ";
  for(int j=0;j<tt+1;j++)
   if(follow[i][j] && j!=tt)
   {
    foll[a][b]=T[j];
    b++;
    cout<<T[j]<<" ";
   }
   else
    if(j==tt)
    {
     foll[a][b]='$';
     b++;
     cout<<'$';
    }
    a++;
    cout<<" } ";
   }
  getch();
}


void show_first()
{
 int b=0;
 a=0;
 cout<<"\n\n First Table For Grammar: \n";
 for(int i=0;i<tnt;i++)
 {
  b=0;
  cout<<"\n FIRST ("<<NT[i]<<" )= { ";
  for(int j=0;j<tt+1;j++)
   if(first[i][j] && j!=tt)
   {
    fir[a][b]=T[j];
    b++;
    cout<<T[j]<<" ";
   }
    a++;
    cout<<" } ";
   }
  getch();
}



void mainf(void)
{
 clrscr();
 read_tnt();
 read_prod();
 find_first();
 find_follow();
 show_follow();
 show_first();
}

int S=0,i=0,j=0,state[20];
char TNT[15];

struct node
{
 int pno,dpos;
};
struct t
{
 char s;
 int n;
};
struct t1
{
 struct t lr[10];
 int gr[5];
};
struct t1 action[15];
struct node closure[10][10];
int g[15][10];
int l;

void sclosure(int,int);
int  added(int);
int  t_into(char);
void print_table(int);
void parser(void);
int  find_index(char);
int  t_ino(char);
void pop(void);

void push(char,int);
void find_closure(int,int);
void SLR(void);

void main()
{
 clrscr();
 mainf();
 getch();
 for(int i=0;i<tnt;i++)
  TNT[i]=NT[i];
 for(int j=0;j<tt;j++)
 {
  TNT[i]=T[j];
  i++;
 }
 strcat(T,"$");
 i=j=0;
 SLR();
 //print_table(S);
 getch();
}

void SLR()
{
 int clno,no=0,x,y,z,len,cnt=-1,d=0;
 closure[i][j].pno=0;
 closure[i][j++].dpos=3;
 find_closure(no,3);
 sclosure(i,j);
 state[i]=j;
 S=0;
 do
 {
  cnt++;
  z=state[cnt];
  for(int k=0;k<tnt+tt;k++)
  {
   i++;
   j=0;d=0;
   for(int l=0;l<z;l++)
   {
    x=closure[cnt][1].pno;
    y=closure[cnt][1].dpos;
    if(prod[x][y]==TNT[k])
    {
     d=1;
     closure[i][j].pno=x;
     closure[i][j++].dpos=++y;
     if((y<strlen(prod[x])) && (isupper(prod[x][y])))
       find_closure(x,y);
    }
   }
   if(d==0)
   {
    i--;
    continue;
   }
   sclosure(i,j);
   state[i]=j;
   clno=added(i-1);
   if(clno==-1)
    clno=i;
   if(isupper(TNT[k]))
    action[cnt].gr[k]=clno;
   else
   {
    action[cnt].lr[k-tnt].s='S';
    action[cnt].lr[k-tnt].n=clno;
   }
   if(added(i-1)!=-1)
    i--;
   else
   {
    S++;
    for(l=0;l<state[i];l++)
    {
     if(closure[i][1].pno==0)
     {
      action[i].lr[tt].s='A';
      continue;
     }
     len=(strlen(prod[closure[i][l].pno])-1);
     if(len==closure[i][l].dpos)
     {
      char v=prod[closure[i][l].pno][0];
      int u=nt_no(v);
      for(x=0;x<strlen(foll[u]);x++)
      {
       int w=t_ino(foll[u][x]);
       action[i].lr[w].s='R';
       action[i].lr[w].n=closure[i][l].pno;
      }
     }
    }
   }
  }
 }
 while(cnt!=S);
}

void print_table(int states)
{
 int lin=5;
 cout<<"\n\n Parser Table: \n";
 for(int i=0;i<tt;i++)
  cout<<"\t"<<T[i];
  cout<<"\t$";
 for(i=0;i<tnt;i++)
   cout<<"\t"<<NT[i];
  cout<<"\n____________________________________________________________________\n";
  for(i=0;i<=states;i++)
  {
   gotoxy(l,lin);
   cout<<"I"<<i<<"\t";
   for(int j=0;j<=tt;j++)
   {
    if(action[i].lr[j].s!='\x0')
    {
     if(action[i].lr[j].s=='A')
     {
      cout<<"Acc";
      continue;
     }
     cout<<action[i].lr[j].s;
     cout<<action[i].lr[j].n;
     cout<<"\t";
    }
    else
     cout<<"\t";
   }
   for(j=0;j<tnt;j++)
    if(action[i].gr[j])
    {
     cout<<action[i].gr[j];
     cout<<"\t";
    }
    else
     cout<<"\t";
    lin++;
    cout<<"\n";
   }
   cout<<"\n_________________________________________________________________";
 }


 void sclosure(int clno,int prodno)
 {
  struct node temp;
  for(int i=0;i<prodno-1;i++)
  {
   for(int j=i+1;j<prodno;j++)
   {
    if(closure[clno][i].pno>closure[clno][j].pno)
    {
     temp=closure[clno][i];
     closure[clno][i]=closure[clno][j];
     closure[clno][j]=temp;
    }
   }
  }
  for(i=0;i<prodno-1;i++)
  {
   for(j=i+1;j<prodno;j++)
   {
    if((closure[clno][i].dpos>closure[clno][j].dpos) &&
       (closure[clno][i].pno==closure[clno][j].pno))
    {
     temp=closure[clno][i];
     closure[clno][i]=closure[clno][j];
     closure[clno][j]=temp;
    }
   }
  }
 }

 int added(int n)
 {
  int d=1;
  for(int k=0;k<=n;k++)
  {
   if(state[k]==state[n+1])
   {
    d=0;
    for(int j=0;j<state[k];j++)
    {
     if((closure[k][j].pno!=closure[n+1][j].pno) ||
	(closure[k][j].dpos!=closure[n+1][j].dpos))
       break;
     else
       d++;
    }
    if(d==state[k])
      return(k);
   }
  }
  return(-1);
 }

 void find_closure(int no,int dp)
 {
  int k;
  char temp[5];
  if(isupper(prod[no][dp]))
  {
   for(k=0;k<tp;k++)
   {
    if(prod[k][0]==prod[no][dp])
    {
     closure[i][j].pno=k;
     closure[i][j++].dpos=3;
     if(isupper(prod[k][3])&&
       (prod[k][3]!=prod[k][0]))
       find_closure(k,3);
    }
   }
  }
  return;
 }

 int t_ino(char t)
 {
  for(int i=0;i<=tt;i++)
   if(T[i]==t)
    return(i);
  return(-1);
 }

please help me ?
tell me why code is not work correctly ?
by this input :
Enter number of terminals: 5
Enter terminals:+*()i
Enter number of non-terminals:3
Enter non-terminals:ETF
Enter number of productions:6
Enter productions:
E->E+T
E->T
T->T*F
T->F
F->(E)
F->i

please help me ?
i dont enough time to correct it
can u answer my problem ?

Welcome,

Your source code must be surrounded by bb code tags. You must read How to post source code using bb code tags?

>please help me ? i dont enough time to correct it. can u answer my problem ?

Where did you spend it?

>please help me ?
If you post using code tags.

>tell me why code is not work correctly ?
Analyzing code without code tags goes far beyond my skills...
If you'd followed the steps described here while developing your program, your program should work fine now, but apparently you didn't :(

this is not a C program because it includes iostream.h, which is an old obsolete file.

please help me ?
where i give my answer ?
if you can't answer me , please give me a slr parser project with c ?
i realy needed it,i must write a slr parser today
please help meeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee

no,this is a C language program,i test it,this code is run,but don't work correctly,i don't know why work incorrectly!confused

That is a c++ program and contains millions (well, maybe not quite that many) errors. You need to get yourself a good modern compiler that does not use iostream.h but <iostream> (no .h extension), such as Code::Blocks or VC++ 2008 Express, both are free.

I'm not about to do your work for you, like debugging that program. That's your job, not mine. All you have to do if fix the errors one at a time, then recompile to see what errors are left to fix.

no,this is a C language program,i test it,this code is run,but don't work correctly,i don't know why work incorrectly!confused

no, this is C++. Not C. Which is probably a lot of your problem: you're confusing two related, but different, languages.

please help me ?
where i give my answer ?
if you can't answer me , please give me a slr parser project with c ?
i realy needed it,i must write a slr parser today
please help meeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee

how sad.

you're not going to do well around here.

no,this is a C language program,i test it,this code is run,but don't work correctly,i don't know why work incorrectly!confused

No you didn't because it will not compile. And C programs do not contain calls to cout, which is a c++ class from iostream. So you really have no clue what you are doing, do you? And you apparently (obviously) didn't write the code you posted but copied it from somewhere else.

ok,allright,i realy confused,yes,this code is C++,thats now you don't help me ?
is this right ???????????????????????????????????????????????????
i have a problem,i don't think that my friends ( you and another members) don't help me

no, its the way you ask for help

you whine and beg, but you don't ask a specific question. you just post a big slop of mixed up code and demand that someone fix it for you.

well, you need to start sorting through the compilation errors and fixing the obvious problems, then asking SPECIFIC questions about the rest of the problems that are still confusing you.

ask a SPECIFIC question, using normal language, like a normal adult and not a toddler. in other words, do not ask a question like:

>>somebody please help meeeeeeeeeeeeeeeeeeeeeeeeeee

that just is annoying and makes nobody want to help you. and anyhow, no one is going to just hand you code for your project. that's against the forum rules.

anyhow, your deadline is coming up quick. you should quit grabassing and get to work.

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.