You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
// Including Libraries
#include<bits/stdc++.h>usingnamespacestd;// Global Variablesint z = 0, i = 0, j = 0, c = 0;
// Modify array size to increase// length of string to be parsedchar a[16], ac[20], stk[15], act[10];
// This Function will check whether// the stack contain a production rule// which is to be Reduce.// Rules can be E->2E2 , E->3E3 , E->4voidcheck()
{
// Copying string to be printed as actionstrcpy(ac,"REDUCE TO E -> ");
// c=length of input stringfor(z = 0; z < c; z++)
{
// checking for producing rule E->4if(stk[z] == '4')
{
printf("%s4", ac);
stk[z] = 'E';
stk[z + 1] = '\0';
//printing actionprintf("\n$%s\t%s$\t", stk, a);
}
}
for(z = 0; z < c - 2; z++)
{
// checking for another productionif(stk[z] == '2' && stk[z + 1] == 'E' &&
stk[z + 2] == '2')
{
printf("%s2E2", ac);
stk[z] = 'E';
stk[z + 1] = '\0';
stk[z + 2] = '\0';
printf("\n$%s\t%s$\t", stk, a);
i = i - 2;
}
}
for(z = 0; z < c - 2; z++)
{
//checking for E->3E3if(stk[z] == '3' && stk[z + 1] == 'E' &&
stk[z + 2] == '3')
{
printf("%s3E3", ac);
stk[z]='E';
stk[z + 1]='\0';
stk[z + 2]='\0';
printf("\n$%s\t%s$\t", stk, a);
i = i - 2;
}
}
return ; // return to main
}
// Driver Functionintmain()
{
printf("GRAMMAR is -\nE->2E2 \nE->3E3 \nE->4\n");
// a is input stringstrcpy(a,"32423");
// strlen(a) will return the length of a to c
c=strlen(a);
// "SHIFT" is copied to act to be printedstrcpy(act,"SHIFT");
// This will print Labels (column name)printf("\nstack \t input \t action");
// This will print the initial// values of stack and inputprintf("\n$\t%s$\t", a);
// This will Run upto length of input stringfor(i = 0; j < c; i++, j++)
{
// Printing actionprintf("%s", act);
// Pushing into stack
stk[i] = a[j];
stk[i + 1] = '\0';
// Moving the pointer
a[j]='';
// Printing actionprintf("\n$%s\t%s$\t", stk, a);
// Call check function ..which will// check the stack whether its contain// any production or notcheck();
}
// Rechecking last time if contain// any valid production then it will// replace otherwise invalidcheck();
// if top of the stack is E(starting symbol)// then it will accept the inputif(stk[0] == 'E' && stk[1] == '\0')
printf("Accept\n");
else//else rejectprintf("Reject\n");
}