6.26.2010

OPERATOR PRESEDANCE

#include
#include
#include
#include
char stack[30];
int top=-1;
void main()
{
clrscr();
fstream filep;
char str[30],terminal[30],a,table[30][30];
int i=0;
cout<<"Enter input string: ";
cin>>str;
filep.open("prece-re.txt",ios::in);
filep>>terminal;
while(filep.eof()!=1)
{
filep>>table[i++];
}
filep.close();
char stop,in,*ptr;
int p,q;
i=0;
top++;
stack[top]='$';
while(1)
{
stop=stack[top];
in=str[i];
if(stop=='$' && in=='$')
{
cout<<"\nstring accepted";
break;
}
else
{
ptr=strchr(terminal,stop);
p=ptr-terminal;
ptr=strchr(terminal,in);
q=ptr-terminal;
if(table[p][q]=='<' || table[p][q]=='=')
{
top++;
stack[top]=table[p][q];
top++;
stack[top]=in;
i++;
}
else if(table[p][q]=='>')
{
while(stack[top]!='<' && top!=-1)
{
top--;
}
if(top==-1)
{
cout<<"\n\nError..." ;
break;
}
top--;
}
else
{
cout<<"\n\nError...";
break;
}
}
}
getch();
return;
}

No comments: