Skip to main content

Program to remove left factoring of grammar



Program to remove left factoring of grammar : 

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

//Structure Declaration

struct production
{
            char lf;
            char rt[10];
            int prod_rear;
            int fl;
};
struct production prodn[20],prodn_new[20];     //Creation of object

//Variables Declaration

int b=-1,d,f,q,n,m=0,c=0;
char terminal[20],nonterm[20],alpha[10],extra[10];
char epsilon='^';

//Beginning of Main Program

void main()
{
   clrscr();

   //Input of Special characters
   cout<<"\nEnter the number of Special characters(except non-terminals): ";
   cin>>q;
   cout<<"Enter the special characters for your production: ";
   for(int cnt=0;cnt<q;cnt++)
   {
     cin>>alpha[cnt];
   }

   //Input of Productions

   cout<<"\nEnter the number of productions: ";
   cin>>n;
   for(cnt=0;cnt<=n-1;cnt++)
   {
     cout<<"Enter the "<< cnt+1<<" production: ";
     cin>>prodn[cnt].lf;
     cout<<"->";
     cin>>prodn[cnt].rt;
     prodn[cnt].prod_rear=strlen(prodn[cnt].rt);
     prodn[cnt].fl=0;
   }

   //Condition for left factoring

   for(int cnt1=0;cnt1<n;cnt1++)
   {
     for(int cnt2=cnt1+1;cnt2<n;cnt2++)
     {
            if(prodn[cnt1].lf==prodn[cnt2].lf)
            {
              cnt=0;
              int p=-1;
              while((prodn[cnt1].rt[cnt]!='\0')&&(prodn[cnt2].rt[cnt]!='\0'))
              {
                if(prodn[cnt1].rt[cnt]==prodn[cnt2].rt[cnt])
                {
                  extra[++p]=prodn[cnt1].rt[cnt];
                  prodn[cnt1].fl=1;
                  prodn[cnt2].fl=1;
                }
                else
                {
                  if(p==-1)
                          break;
                  else
                  {
                          int h=0,u=0;
                          prodn_new[++b].lf=prodn[cnt1].lf;
                          strcpy(prodn_new[b].rt,extra);
                          prodn_new[b].rt[p+1]=alpha[c];
                          prodn_new[++b].lf=alpha[c];
                          for(int g=cnt;g<prodn[cnt2].prod_rear;g++)
                           prodn_new[b].rt[h++]=prodn[cnt2].rt[g];
                           prodn_new[++b].lf=alpha[c];
                          for(g=cnt;g<=prodn[cnt1].prod_rear;g++)
                           prodn_new[b].rt[u++]=prodn[cnt1].rt[g];
                           m=1;
                           break;
                   }
                 }
                 cnt++;
               }
               if((prodn[cnt1].rt[cnt]==0)&&(m==0))
               {
                         int h=0;
                         prodn_new[++b].lf=prodn[cnt1].lf;
                         strcpy(prodn_new[b].rt,extra);
                         prodn_new[b].rt[p+1]=alpha[c];
                         prodn_new[++b].lf=alpha[c];
                         prodn_new[b].rt[0]=epsilon;
                         prodn_new[++b].lf=alpha[c];
                         for(int g=cnt;g<prodn[cnt2].prod_rear;g++)
                         prodn_new[b].rt[h++]=prodn[cnt2].rt[g];
               }
               if((prodn[cnt2].rt[cnt]==0)&&(m==0))
               {
                 int h=0;
                 prodn_new[++b].lf=prodn[cnt1].lf;
                 strcpy(prodn_new[b].rt,extra);
                 prodn_new[b].rt[p+1]=alpha[c];
                 prodn_new[++b].lf=alpha[c];
                 prodn_new[b].rt[0]=epsilon;
                 prodn_new[++b].lf=alpha[c];
                 for(int g=cnt;g<prodn[cnt1].prod_rear;g++)
                  prodn_new[b].rt[h++]=prodn[cnt1].rt[g];
               }
               c++;
               m=0;
             }
       }
    }

   //Display of Output

    cout<<"\n\n********************************";
    cout<<"\n      AFTER LEFT FACTORING      ";
    cout<<"\n********************************";
    cout<<endl;
    for(int cnt3=0;cnt3<=b;cnt3++)
            {
                        cout<<"Production "<<cnt3+1<<" is: ";
                        cout<<prodn_new[cnt3].lf;
                        cout<<"->";
                        cout<<prodn_new[cnt3].rt;
                        cout<<endl<<endl;
            }

    for(int cnt4=0;cnt4<n;cnt4++)
    {
    if(prodn[cnt4].fl==0)
    {
    cout<<"Production  "<<cnt3++<<" is: ";
    cout<<prodn[cnt4].lf;
    cout<<"->";
    cout<<prodn[cnt4].rt;
    cout<<endl<<endl;
    }
  }
 getche();
}    //end of main program







Comments

Popular posts from this blog

Recursive program to insert a star between pair of identical characters

Given a string with repeated characters, we have to insert a star i.e. ” * “  between pair of adjacent identical characters using recursion. Examples: Input : aabb Output : a*ab*b Input : xxxy Output : x*x*xy Recommended: Please try your approach on  {IDE}  first, before moving on to the solution. Approach: If there is an empty string then simply return. This forms our  base condition . Else we do the following- Check if the first two characters are identical. If yes, then insert ” * ” between them. As we have now checked for identical characters at first two positions of the string so we now make a recursive call  without the first character of the string . The above approach has been implemented below: C filter_none edit play_arrow brightness_4 // Recursive CPP program to insert * between // two consecutive same characters. #include <iostream> using namespace std;     // Funct...

Count the number of objects using Static member function

Write a program to design a class having static member function named showcount() which has the property of displaying the number of objects created of the class. Explanation:  In this program we are simply explaining the approach of static member function. We can define class members and member functions as static using static keyword. Before understanding static member function, we must understand static member. When we declare a member of a class as static it means no matter how many objects of the class are created, there is  only one copy of the static member . Important points about Static : A static member is shared by all objects of the class, all static data is initialized to zero when the first object is created, if no other initialization is present. A static member function can only access static data member, other static member functions and any other functions from outside the class. By declaring a function member as static, we make it independent of any ...

Send and Receive JSON over sockets in server and client application in C

I want to send the data in JSON over sockets in a server-client application written in C. I am using json-c / libjson library for handling JSON data in C application. By working on some of the tutorials I am able to create JSON object and able to parse it successfully. Now I want to use the JSON data format for the communication of server-client. Here is part of my server and client code server.c int main () { int listenfd = 0 , connfd = 0 ; //related with the server struct sockaddr_in serv_addr ; //json_object * jobj; uint8_t buf [ 158 ], i ; memset (& buf , '0' , sizeof ( buf )); listenfd = socket ( AF_INET , SOCK_STREAM , 0 ); serv_addr . sin_family = AF_INET ; serv_addr . sin_addr . s_addr = htonl ( INADDR_ANY ); serv_addr . sin_port = htons ( 8888 ); bind ( listenfd , ( struct sockaddr *)& serv_addr , sizeof ( serv_addr )); printf ( "binding\n" ); listen ( listenf...