Write A Program To Check Whether The String Belongs To The Grammar or Not
Write A Program To Check Whether The String Belongs To The Grammar or Not
#include<stdio.h>
#include<conio.h>
#include<string.h>
voidmain(){
charstring[50];
intflag,count=o;
clrscr();
gets(string);
if(string[0]=='a'){
flag=0;
for(count=1;string[count-1]!='\0';count++){
if(string[count]=='b'){
flag=1;
continue;
}elseif((flag==1)&&(string[count]=='a')){
break;
}elseif(string[count]=='a')
continue;elseif(flag==1)&&(string[count]='\0')){
printf("String accepted…..!!!!");
1
break;
}else{
getch();
Output-
2
2. Write a program to find leading terminals
#include<iostream.h>
#include<stdio.h>
#include<conio.h>
#include<string.h>
int n,d,f,xn,xt,top=-1;
char X,Y;
int a[20][20];
char terminal[20],nonterm[20];
struct production
{
char l;
char r[10];
};
struct stack
{
char nt;
char t;
};
void main()
{
clrscr();
3
{
cin>>terminal[k];
}
for(int p=0;p<f;p++)
{
for(int q=0;q<d;q++)
{
a[p][q]=0;
}
}
for(i=0;i<=n-1;i++)
{
for(int j=0;j<d;j++)
{
if(prod[i].r[0]==terminal[j])
install(prod[i].l,prod[i].r[0]);
else if(prod[i].r[1]==terminal[j])
install(prod[i].l,prod[i].r[1]);
}
}
4
while(top>-1)
{
pop();
for(int c=0;c<=n-1;c++)
{
if(prod[c].r[0]==X)
install(prod[c].l,Y);
}
}
//Output
cout<<"\n\n----------------------------------------------------------------";
cout<<"\n leading elements are:- " ;
cout<<"\n\n-----------------------------------------------------------------";
cout<<endl<<" ";
for(int w=0;w<d;w++)
cout<<" "<<terminal[w];
cout<<endl;
for(p=0;p<f;p++)
{
cout<<nonterm[p]<<" ";
for(int q=0;q<d;q++)
{
cout<<a[p][q]<<" ";
}
cout<<endl;
}
getch();
}
5
st[top].nt=x;
st[top].t=y;
a[xn][xt]=1;
}
}
void pop()
{
X=st[top].nt;
Y=st[top].t;
top--;
}
int pon(char u)
{
for(int x=0;x<f;x++)
{
if(u==nonterm[x])
return x;
}
}
int pot(char v)
{
for(int x=0;x<d;x++)
{
if(v==terminal[x])
return x;
}
}
int checkstatus(int xn,intxt)
{
if(a[xn][xt]==1)
return 0;
else
return 1;
Output-
6
7
3. Write a program to find the trailing terminals
#include<iostream.h>
#include<stdio.h>
#include<conio.h>
#include<string.h>
int n,d,f,xn,xt,top=-1;
char X,Y;
int a[20][20];
char terminal[20],nonterm[20];
struct production
{
char l;
char r[10];
int rear;
};
struct stack
{
char nt;
char t;
};
struct stack st[20];
struct production prod[20];
void main()
{
clrscr();
8
{
cin>>terminal[k];
}
for(int p=0;p<f;p++)
{
for(int q=0;q<d;q++)
{
a[p][q]=0;
}
}
for(i=0;i<=n-1;i++)
{
for(int j=0;j<d;j++)
{
if(prod[i].r[prod[i].rear-1]==terminal[j])
install(prod[i].l,prod[i].r[prod[i].rear-1]);
else if(prod[i].r[prod[i].rear-2]==terminal[j])
install(prod[i].l,prod[i].r[prod[i].rear-2]);
}
9
}
while(top>-1)
{
pop();
for(int c=0;c<=n-1;c++)
{
if(prod[c].r[prod[c].rear-1]==X)
install(prod[c].l,Y);
}
}
cout<<"\n\n****************************************************";
cout<<"\n TRAILING ELEMENTS OF GIVEN PRODUCTION ";
cout<<"\n****************************************************** ";
cout<<endl<<" ";
for(int w=0;w<d;w++)
cout<<" "<<terminal[w];
cout<<endl;
for(p=0;p<f;p++)
{
cout<<nonterm[p]<<" ";
for(int q=0;q<d;q++)
{
cout<<a[p][q]<<" ";
}
cout<<endl;
}
cout<<endl<<endl;
for(i=0;i<f;i++)
{
cout<<"Leading("<<nonterm[i]<<")="<<" "<<"{";
for(int j=0;j<d;j++)
{
if(a[i][j]==1)
cout<<terminal[j]<<",";
}
cout<<"}"<<endl;
}
getch();
}
10
void install(char x,char y)
{
int g;
xn=pon(x);
xt=pot(y);
g=checkstatus(xn,xt);
if(g==0)
return;
else if(g==1)
{
top++;
st[top].nt=x;
st[top].t=y;
a[xn][xt]=1;
}
}
void pop()
{
X=st[top].nt;
Y=st[top].t;
top--;
}
int pon(char u)
{
for(int x=0;x<f;x++)
{
if(u==nonterm[x])
return x;
}
}
int pot(char v)
{
for(int x=0;x<d;x++)
{
if(v==terminal[x])
return x;
}
}
11
if(a[xn][xt]==1)
return 0;
else
return 1;
}
12
Output-
13
4. Write a program to compute FIRST of non-terminals
#include<stdio.h>
#include<conio.h>
char array[10][20],temp[10];
int c,n;
void fun(int,int[]);
int fun2(int i,intj,int p[],int );
void main()
{
int p[2],i,j;
printf("Enter the no. of productions :");
scanf("%d",&n);
printf("Enter the productions :\n");
for(i=0;i<n;i++)
scanf("%s",array[i]);
for(i=0;i<n;i++)
{
c=-1,p[0]=-1,p[1]=-1;
fun(i,p);
printf("First(%c) : [ ",array[i][0]);
for(j=0;j<=c;j++)
printf("%c,",temp[j]);
printf("\b ].\n");
getch();
}
}
int fun2(int i,intj,int p[],int key)
{
int k;
if(!key)
{
for(k=0;k<n;k++)
if(array[i][j]==array[k][0])
break;
p[0]=i;p[1]=j+1;
fun(k,p);
return 0;
}
else
{
for(k=0;k<=c;k++)
{
if(array[i][j]==temp[k])
break;
}
14
if(k>c)return 1;
else return 0;
}
}
void fun(int i,int p[])
{
int j,k,key;
for(j=2;array[i][j] != NULL; j++)
{
if(array[i][j-1]=='/')
{
if(array[i][j]>= 'A' && array[i][j]<='Z')
{
key=0;
fun2(i,j,p,key);
}
else
{
key = 1;
if(fun2(i,j,p,key))
temp[++c] = array[i][j];
if(array[i][j]== '@'&& p[0]!=-1) //taking '@' as null symbol
{
if(array[p[0]][p[1]]>='A' && array[p[0]][p[1]] <='Z')
{
key=0;
fun2(p[0],p[1],p,key);
}
else
if(array[p[0]][p[1]] != '/'&& array[p[0]][p[1]]!=NULL)
{
if(fun2(p[0],p[1],p,key))
temp[++c]=array[p[0]][p[1]];
}
}
}
}
}
}
15
Output-
16
5. Write a program to compute the follow of non-terminal
#include<stdio.h>
#include<conio.h>
#define max 10
#define MAX 15
void ffun(int,int);
void fun(int,int[]);
void follow(int i);
char array[max][MAX],temp[max][MAX];
int c,n,t;
int fun2(int i,intj,int p[],int key)
{
int k;
if(!key){
for(k=0;k<n;k++)
if(array[i][j]==array[k][0])
break;
p[0]=i;p[1]=j+1;
fun(k,p);
return 0;
}
else{
for(k=0;k<=c;k++){
if(array[i][j]==temp[t][k])
break;
}
if(k>c)return 1;
else return 0;
}
}
18
for(cc=1;temp[tt][cc]!=NULL;cc++)
{
if(temp[tt][cc]=='@')null=1;
else fol[l][++f]=temp[tt][cc];
}
}
else fol[l][++f]=array[j][ii];
}
if(null)follow(j);
}
int main()
{
int p[2],i,j;
//clrscr();
printf("Enter the no. of productions :");
scanf("%d",&n);
printf("Enter the productions :\n");
for(i=0;i<n;i++)
scanf("%s",array[i]);
for(i=0,t=0;i<n;i++,t++){
c=0,p[0]=-1,p[1]=-1;
temp[t][0]=array[i][0];
fun(i,p);
temp[t][++c]=NULL;
printf("First(%c) : [ ",temp[t][0]);
for(j=1;j<c;j++)
printf("%c,",temp[t][j]);
printf("\b ].\n");
}
/* Follow Finding */
for(i=0,l=0;i<n;i++,l++)
{
f=-1;ff0=-1;
fol[l][++f]=array[i][0];
follow(i);
fol[l][++f]=NULL;
}
for(i=0;i<n;i++)
{
printf("\nFollow[%c] : [ ",fol[i][0]);
for(j=1;fol[i][j]!=NULL;j++)
printf("%c,",fol[i][j]);
printf("\b ]");
19
}
getch();
return 0;
}
20
Output-
21
{
cout<<"Enter Production "<<i+1<<" : ";
cin>>temp;
sizes[i] = temp.size();
ip+=temp;
if(i!=n-1)
ip += "|";
}
cout<<"Production Rule : "<<ip<<endl;
char x = ip[3];
for(int i=0,k=3;i<n;i++)
{
if(x == ip[k])
{
if(ip[k+1] == '|')
{
op1 += "#";
ip.insert(k+1,1,ip[0]);
ip.insert(k+2,1,'\'');
k+=4;
}
else
{
op1 += "|" + ip.substr(k+1,sizes[i]-1);
ip.erase(k-1,sizes[i]+1);
}
}
else
{
while(ip[k++]!='|');
}
}
char y = op1[6];
for(int i=0,k=6;i<n-1;i++)
{
if(y == op1[k])
{
if(op1[k+1] == '|')
{
op2 += "#";
op1.insert(k+1,1,op1[0]);
op1.insert(k+2,2,'\'');
k+=5;
}
22
else
{
temp.clear();
for(int s=k+1;s<op1.length();s++)
temp.push_back(op1[s]);
op2 += "|" + temp;
op1.erase(k-1,temp.length()+2);
} }}
op2.erase(op2.size()-1);
cout<<"After Left Factoring : "<<endl;
cout<<ip<<endl;
cout<<op1<<endl;
cout<<op2<<endl;
return 0;
Output-
Enter the Parent Non-Terminal : L
Enter the number of productions : 4
Enter Production 1 :i
Enter Production 2 :iL
Enter Production 3 : (L)
Enter Production 4 :iL+L
Production Rule : L->i|iL|(L)|iL+L
After Left Factoring :
L->iL'|(L)
L'->#|LL''
L''->#|+L
23
7. To Show All Operations of a stack
(Implementing Stack using Arrays)
24
int isFull(struct Stack* stack)
{
return stack->top == stack->capacity - 1;
}
25
{
if (isEmpty(stack))
return INT_MIN;
return stack->array[stack->top];
}
push(stack, 10);
push(stack, 20);
push(stack, 30);
return 0;
}
26
Output :
27
8. To show various operations read write and modify in a
text file.
#include <stdio.h>
int main()
{
FILE *fp;
fp = fopen ("data.txt", "w");
}
FILE *fp;
fp = fopen ("data.txt", "r");
fclose (fp);
Writing to a File
#include <stdio.h>
int main()
{
int i;
FILE * fptr;
char fn[50];
28
char str[] = "Guru99 Rocks\n"; fptr = fopen("fputc_test.txt", "w");
#include <stdio.h>
int main() {
FILE * file_pointer;
char buffer[30], c;
fclose(file_pointer);
return 0;
}
30
9. program to check whether a grammar is operator
precedent
#include<stdlib.h>
#include<stdio.h>
#include<string.h>
void main()
{
char grm[20][20], c;
31
for (i = 0; i < n; i++) {
c = grm[i][2];
while (c != '\0') {
flag = 1;
else {
flag = 0;
f();
}
if (c == '$') {
flag = 0;
f();
}
c = grm[i][++j];
}
}
if (flag == 1)
printf("Operator grammar"); }
32
OUTPUT
33