SOURCE CODE:
#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<ctype.h>
int
point(char *sort,char ch)
{
char *position;
position=strchr(sort,ch);
return(position-sort);
}
void
main()
{
char OPM[10][10],prod[10][5],ter[20],nter[20],str[20],hand[10],temp[20];
char symbol,ch1,ch;
int i,j,k,cnter,cter,p=4,mx,my,m=0;
clrscr();
printf("\nEnter non
terminals:");
scanf("%s",nter);
printf("\nEnter terminals:");
scanf("%s",ter);
cter=strlen(ter);
printf("\nEnter no of productions:
");
scanf("%d",&p);
printf("Enter Productions:-\n");
for(i=0;i<p;i++)
scanf("%s",prod[i]);
printf("\nEnter The OPM
Matrix:-\n");
for(i=0;i<cter;i++)
scanf("\n%s",OPM[i]);
printf("\nEnter string For Parsing:-");
scanf("%s",str);
int l=0,r=0;
i=0;
while(strlen(str)>3)
{
ch=str[i];
symbol=' ';
if(ch=='#' && i==0)
symbol='<';
else if(ch=='#' &&
i==(strlen(str)-1))
symbol='>';
else
{
if(!isupper(ch))
{
mx=point(ter,ch);
ch1=str[i+1];
if(!isupper(ch1))
{
r=i+1;
my=point(ter,ch1);
symbol=OPM[mx][my];
}
else
{
ch1=str[i+2];
if(isupper(ch1))
{
printf("\n\nString
is not valid");
break;
}
else
{
r=i+2;
if(ch1=='#')
symbol='>';
else
{
my=point(ter,ch1);
symbol=OPM[mx][my];
}
}
}
}
}
if(symbol=='<')
{
i++;
l=i;
}
else if(symbol=='>')
{
printf("\n");
if(r-l==1)
{
for(k=0;k<p;k++)
if(str[l]==prod[k][2])
str[l]=prod[k][0];
i=0;
printf("\n%s",str);
getch();
}
else if((r-l)==3)
{
ch=str[l+1];
for(k=0;k<p;k++)
{
if(ch==prod[k][3]
|| (isupper(ch) && isupper(prod[k][3])))
{
str[l++]=prod[k][0];
i=0;
for(m=0;str[r]!='\0';m++)
{
temp[m]=str[r];
r++;
}
temp[m]='\0';
str[l]='\0';
str[l+1]='\0';
strcat(str,temp);
printf("\n%s",str);
}
}
}
}
else
i++;
}
getch();
}
Output:
Enter non terminals:ETF
Enter terminals:+*()i
Enter no of productions: 6
Enter Productions:-
E=E+T E=T T=T*F T=F F=(E)
F=i
Enter The OPM Matrix:-
><<><
>><><
<<<=<
>>0>0
>>0>0
Enter string For Parsing:-#i+i*(i+i)#
#F+i*(i+i)#
#F+F*(i+i)#
#F+F*(F+i)#
#F+F*(F+F)#
#F+F*(E)#
#F+F*F#
#F+T#
#E#
No comments:
Post a Comment
If you have any doubts please let me know