/* C program to eliminate the productions that do not produce terminal in a grammar */
#include<stdio.h>
int check(char ,int );
int flag=0;
char a[20][20];
int main()
{
int i=0,j=0,k,n,p;
char c;
FILE *fp;
//clrscr();
fp=fopen("input.txt","r");
while(fscanf(fp,"%c",&c)!=EOF)
{
if(c==|)
{
a[i][j]=
;
j=0,i++;
a[i][j]=a[i-1][j++];
a[i][j++]=-;
a[i][j++]=>;
}
else if(c!=
)
a[i][j++]=c;
else
{
a[i][j]=c;
i++;
j=0;
}}
n=i;
j=i=0;
fclose(fp);
printf("
Start symbol is S
Capital latters are variables
Small latters are terminals
");
printf("
:::::::::::----INPUT-----::::::::::::
");
while(i<n)
{
if(a[i][j]!=
)
printf("%c",a[i][j++]);
else
{
printf("%c",a[i][j]);
i++;
j=0;
}}
i=0;
j=3;
while(i<n)
{
if(a[i][j]!=
)
{
if(a[i][j]==S)
j++;
if(a[i][j]>=a&&a[i][j]<=z)
j++;
if(a[i][j]>=A&&a[i][j]<=Z)
{
check(a[i][j],n);
if(flag==1)
{
a[i][0]= ;
}
j++;
} //ifA
} //if/n
else
{
i++;
j=3;
}
} //while//
i=j=0;
printf("
:::::::::::----OUTPUT-----::::::::::::
");
while(i<n)
{
if(a[i][j]== )
{
i++;
j=0;
}
else if(a[i][j]!=
)
printf("%c",a[i][j++]);
else
{
printf("%c",a[i][j]);
i++;
j=0;
}}
//getch();
}
int check(char c,int n)
{
//char b;
int i=0,p,j=3;
for(i=0;i<n;i++)
if(a[i][0]==c)
{ flag=0;
break;
}
else
flag=1;
while(a[i][j]!=
&&flag!=1)
{
if(a[i][j]==S||a[i][j]==c)
j++;
if(a[i][j]>=a&&a[i][j]<=z)
j++;
if(a[i][j]>=A&&a[i][j]<=Z)
{
check(a[i][j],n);
j++;
}
}
}
INPUT FILE (input.txt):
S->ASa|ASB|ASC|AB
A->a
B->b
C->D
For more c programs related to Automata, See the Automata label(Above).
Please Share and comment to improve this blog. Thanks to all.. :)
#include<stdio.h>
int check(char ,int );
int flag=0;
char a[20][20];
int main()
{
int i=0,j=0,k,n,p;
char c;
FILE *fp;
//clrscr();
fp=fopen("input.txt","r");
while(fscanf(fp,"%c",&c)!=EOF)
{
if(c==|)
{
a[i][j]=
;
j=0,i++;
a[i][j]=a[i-1][j++];
a[i][j++]=-;
a[i][j++]=>;
}
else if(c!=
)
a[i][j++]=c;
else
{
a[i][j]=c;
i++;
j=0;
}}
n=i;
j=i=0;
fclose(fp);
printf("
Start symbol is S
Capital latters are variables
Small latters are terminals
");
printf("
:::::::::::----INPUT-----::::::::::::
");
while(i<n)
{
if(a[i][j]!=
)
printf("%c",a[i][j++]);
else
{
printf("%c",a[i][j]);
i++;
j=0;
}}
i=0;
j=3;
while(i<n)
{
if(a[i][j]!=
)
{
if(a[i][j]==S)
j++;
if(a[i][j]>=a&&a[i][j]<=z)
j++;
if(a[i][j]>=A&&a[i][j]<=Z)
{
check(a[i][j],n);
if(flag==1)
{
a[i][0]= ;
}
j++;
} //ifA
} //if/n
else
{
i++;
j=3;
}
} //while//
i=j=0;
printf("
:::::::::::----OUTPUT-----::::::::::::
");
while(i<n)
{
if(a[i][j]== )
{
i++;
j=0;
}
else if(a[i][j]!=
)
printf("%c",a[i][j++]);
else
{
printf("%c",a[i][j]);
i++;
j=0;
}}
//getch();
}
int check(char c,int n)
{
//char b;
int i=0,p,j=3;
for(i=0;i<n;i++)
if(a[i][0]==c)
{ flag=0;
break;
}
else
flag=1;
while(a[i][j]!=
&&flag!=1)
{
if(a[i][j]==S||a[i][j]==c)
j++;
if(a[i][j]>=a&&a[i][j]<=z)
j++;
if(a[i][j]>=A&&a[i][j]<=Z)
{
check(a[i][j],n);
j++;
}
}
}
INPUT FILE (input.txt):
S->ASa|ASB|ASC|AB
A->a
B->b
C->D
For more c programs related to Automata, See the Automata label(Above).
Please Share and comment to improve this blog. Thanks to all.. :)