0% found this document useful (0 votes)
4K views

Write A Program To Check Whether The String Belongs To The Grammar or Not

The document contains 5 programs related to context free grammar operations: 1. A program to check if a string belongs to a given grammar. 2. A program to find the leading terminals of a grammar. 3. A program to find the trailing terminals of a grammar. 4. A program to compute the FIRST set of nonterminals in a grammar. 5. A program to compute the FOLLOW set of nonterminals in a grammar.

Uploaded by

Jitender Garg
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4K views

Write A Program To Check Whether The String Belongs To The Grammar or Not

The document contains 5 programs related to context free grammar operations: 1. A program to check if a string belongs to a given grammar. 2. A program to find the leading terminals of a grammar. 3. A program to find the trailing terminals of a grammar. 4. A program to compute the FIRST set of nonterminals in a grammar. 5. A program to compute the FOLLOW set of nonterminals in a grammar.

Uploaded by

Jitender Garg
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 33

1.

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();

printf("The grammar is: S->aS, S->Sb, S->ab\n");

printf("Enter the string to be checked:\n");

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')){

printf("The string does not belong to the


specified grammar");

break;

}elseif(string[count]=='a')

continue;elseif(flag==1)&&(string[count]='\0')){

printf("String accepted…..!!!!");

1
break;

}else{

printf("String not accepted");

getch();

Output-

2
2. Write a program to find leading terminals

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

void install(char x,char y);


void pop();
int pon(char u);
int pot(char v);
int checkstatus(int a,int b);

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;
};

struct stack st[20];


struct production prod[20];

void main()
{

clrscr();

cout<<"Provide the number of terminals: ";


cin>>d;
cout<<"Provide the terminal symbols for your production: ";
for(int k=0;k<d;k++)

3
{
cin>>terminal[k];
}

cout<<"\n Provide the number of non-terminals: ";


cin>>f;
cout<<"Provide the non-terminal symbols for your production:";
for(k=0;k<f;k++)
{
cin>>nonterm[k];
}

cout<< "\nHow many productions you want to Provide?? ";


cin>>n;
for(int i=0;i<=n-1;i++)
{
cout<<"Provide the "<< i+1<<" production: ";
cin>>prod[i].l;
cout<<"->";
cin>>prod[i].r;
}

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();
}

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++;

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>

void install(char x,char y);


void pop();
int pon(char u);
int pot(char v);
int checkstatus(int a,int b);

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();

cout<<"Enter the number of terminals: ";


cin>>d;
cout<<"Enter the terminal symbols for your production: ";
for(int k=0;k<d;k++)

8
{
cin>>terminal[k];
}

//Input of non-terminal symbols

cout<<"\nEnter the number of non-terminals: ";


cin>>f;
cout<<"Enter the non-terminal symbols for your production: ";
for(k=0;k<f;k++)
{
cin>>nonterm[k];
}

cout<< "\nEnter the number of productions: ";


cin>>n;
for(int i=0;i<=n-1;i++)
{
cout<<"Enter the "<< i+1<<" production: ";
cin>>prod[i].l;
cout<<"->";
cin>>prod[i].r;
prod[i].rear=strlen(prod[i].r);
}

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;
}
}

int checkstatus(int xn,intxt)


{

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;
}
}

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{
17
key=1;
if(fun2(i,j,p,key))
temp[t][++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[t][++c]=array[p[0]][p[1]];
}
}
}
}
}
}

char fol[max][MAX],ff[max];int f,l,ff0;


void follow(int i)
{
int j,k;
for(j=0;j<=ff0;j++)
if(array[i][0]==ff[j])
return;
if(j>ff0)ff[++ff0]=array[i][0];
if(i==0)fol[l][++f]='$';
for(j=0;j<n;j++)
for(k=2;array[j][k]!=NULL;k++)
if(array[j][k]==array[i][0])
ffun(j,k);
}

void ffun(int j,int k)


{
int ii,null=0,tt,cc;
if(array[j][k+1]=='/'||array[j][k+1]==NULL)
null=1;
for(ii=k+1;array[j][ii]!='/'&&array[j][ii]!=NULL;ii++){
if(array[j][ii]<='Z'&&array[j][ii]>='A')
{
for(tt=0;tt<n;tt++)
if(temp[tt][0]==array[j][ii])break;

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-

6. Write a program to remove left factoring


#include<iostream>
#include<string>
using namespace std;
int main()
{ string ip,op1,op2,temp;
int sizes[10] = {};
char c;
int n,j,l;
cout<<"Enter the Parent Non-Terminal : ";
cin>>c;
ip.push_back(c);
op1 += ip + "\'->";
op2 += ip + "\'\'->";;
ip += "->";
cout<<"Enter the number of productions : ";
cin>>n;
for(int i=0;i<n;i++)

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)

// C program for array implementation of stack


#include <limits.h>
#include <stdio.h>
#include <stdlib.h>

// A structure to represent a stack


struct Stack {
int top;
unsigned capacity;
int* array;
};

// function to create a stack of given capacity. It initializes size of


// stack as 0
struct Stack* createStack(unsigned capacity)
{
struct Stack* stack = (struct Stack*)malloc(sizeof(struct Stack));
stack->capacity = capacity;
stack->top = -1;
stack->array = (int*)malloc(stack->capacity * sizeof(int));
return stack;
}

// Stack is full when top is equal to the last index

24
int isFull(struct Stack* stack)
{
return stack->top == stack->capacity - 1;
}

// Stack is empty when top is equal to -1


int isEmpty(struct Stack* stack)
{
return stack->top == -1;
}

// Function to add an item to stack. It increases top by 1


void push(struct Stack* stack, int item)
{
if (isFull(stack))
return;
stack->array[++stack->top] = item;
printf("%d pushed to stack\n", item);
}

// Function to remove an item from stack. It decreases top by 1


int pop(struct Stack* stack)
{
if (isEmpty(stack))
return INT_MIN;
return stack->array[stack->top--];
}

// Function to return the top from stack without removing it


int peek(struct Stack* stack)

25
{
if (isEmpty(stack))
return INT_MIN;
return stack->array[stack->top];
}

// Driver program to test above functions


int main()
{
struct Stack* stack = createStack(100);

push(stack, 10);
push(stack, 20);
push(stack, 30);

printf("%d popped from stack\n", pop(stack));

return 0;
}

26
Output :

27
8. To show various operations read write and modify in a
text file.

How to Create a File

#include <stdio.h>
int main()
{
FILE *fp;
fp = fopen ("data.txt", "w");
}

How to Close a file

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");

// "w" defines "writing mode"

for (i = 0; str[i] != '\n'; i++)


{
/* write to file using fputc() function */
fputc(str[i], fptr);
}
fclose(fptr);
return 0;
}

Reading data from a File

#include <stdio.h>
int main() {
FILE * file_pointer;
char buffer[30], c;

file_pointer = fopen("fprintf_test.txt", "r");


printf("----read a line----\n");
fgets(buffer, 50, file_pointer);
printf("%s\n", buffer);

printf("----read and parse data----\n");


file_pointer = fopen("fprintf_test.txt", "r"); //reset the pointer
char str1[10], str2[2], str3[20], str4[2];
fscanf(file_pointer, "%s %s %s %s", str1, str2, str3, str4);
printf("Read String1 |%s|\n", str1);
29
printf("Read String2 |%s|\n", str2);
printf("Read String3 |%s|\n", str3);
printf("Read String4 |%s|\n", str4);

printf("----read the entire file----\n");

file_pointer = fopen("fprintf_test.txt", "r"); //reset the pointer


while ((c = getc(file_pointer)) != EOF) printf("%c", 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>

// function f to exit from the loop


// if given condition is not true
void f()
{
printf("Not operator grammar");
exit(0);
}

void main()
{
char grm[20][20], c;

// Here using flag variable,


// considering grammar is not operator grammar
int i, n, j = 2, flag = 0;

// taking number of productions from user


scanf("%d", &n);
for (i = 0; i < n; i++)
scanf("%s", grm[i]);

31
for (i = 0; i < n; i++) {
c = grm[i][2];

while (c != '\0') {

if (grm[i][3] == '+' || grm[i][3] == '-'


|| grm[i][3] == '*' || grm[i][3] == '/')

flag = 1;

else {

flag = 0;
f();
}

if (c == '$') {
flag = 0;
f();
}

c = grm[i][++j];
}
}

if (flag == 1)
printf("Operator grammar"); }
32
OUTPUT

33

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy