Friday, 31 July 2020

2. REMOVAL OF λ/є FROM THE TRANSITION DIAGRAM


Source code:

#include<stdio.h>

#include<conio.h>

#include<string.h>

void main()

{

      int i,j,n,k=0,p=0,q=0,z=0,r=0,y=0,l=0,flag;

      char pr[20][20],s[20],ntt[20],nt[10],t[10];

      char matrix[10][10][10],ch,ch1,ch2,c[1],f[1],ch3;

      int m,mx,my;

      clrscr();

      c[0]='^';

      f[0]='Z';

      printf("Enter the no. of productions");

      scanf("%d",&n);

      printf("\nEnter the productions");

      for(i=0;i<n;i++)

      {

 

            scanf("%s",&pr[i]);

      }

      for(i=0;i<n;i++)

      {

            m=strlen(pr[i]);

            for(j=0;j<m;j++)

            {

                  s[k]=pr[i][j];

                  k++;

            }

      }

      s[k]='\0';

      for(i=0;i<k;i++)

      {

            for(j=0;j<k;j++)

            {

                  if(s[i]<=s[j])

                  {

                        ch=s[i];

                        s[i]=s[j];

                        s[j]=ch;

                  }

            }

      }

      for(i=0;i<k;i++)

      {

            ch=s[i];

            if(ch!=s[i+1])

            {

                  if(ch!='=')

                  {

                        s[i]=ch;

                        ntt[z]=s[i];

                        z++;

                  }

            }

      }

      ntt[z]='\0';

      printf("\n The symbols are:\n");

      printf("%s",ntt);

 

      for(i=0;i<z;i++)

      {

            if(isupper(ntt[i]))

            {

                  nt[r]=ntt[i];

                  r++;

            }

            else

            {

                  t[y]=ntt[i];

                  y++;

            }

            nt[r]='\0';

            t[y]='\0';

      }

      printf("\n");

      strcat(t,c);

      strcat(nt,f);

      r++;

      y++;

      t[y]='\0';

      nt[r]='\0';

      printf("Nonterminals:%s",nt);

      printf("\n");

      printf("Terminals:%s",t);

      getch();

//---------------------Finding & Printing the matrix-----------------------------------

      ch='\0';

      flag=1;

      while(flag==1)

      {

            flag=0;

            for(i=0;i<r;i++)

            {

                  for(j=0;j<y;j++)

                  {

                        matrix[i][j][0]='\0';

                  }

            }

            for(i=0;i<n;i++)

            {

                  ch1=pr[i][0];

                  for(p=0;p<r;p++)

                  {

                        if(ch1==nt[p])

                        mx=p;

                  }

                  if(strlen(pr[i])>3)

                  {

                        ch2=pr[i][2];

                        if(islower(ch2))

                        {

                              for(q=0;q<y;q++)

                              {

                                    if(ch2==t[q])

                                    my=q;

                              }

                              ch3=pr[i][3];

                        }

                        else

                        {

                              my=y-1;

                              ch2=pr[i][2];

                              ch3=pr[i][3];

                        }

                  }

                  else

                  {

                        ch2=pr[i][2];

                        if(islower(ch2))

                        {

                              for(p=0;p<y;p++)

                              {

                                    if(ch2==t[p])

                                    my=p;

                              }

                              ch3='Z';

                        }

                        else

                        {

                              my=y-1;

                              ch3=pr[i][2];

                        }

                  }

                  k=strlen(matrix[mx][my]);

                  matrix[mx][my][k]=ch3;

                  matrix[mx][my][k+1] ='\0';

            }

            printf("\nSymbol Table is:\n");

            printf("  ");

            for(i=0;i<y;i++)

            printf("%2c ",t[i]);

            printf("\n");

            for(i=0;i<r;i++)

            {

                  printf("%2c",nt[i]);

                  for(j=0;j<y;j++)

                  {

                        printf("%2s ",matrix[i][j]);

                  }

                  printf("\n");

            }

            getch();

//----------------------Removal of lamda----------------------------------

            j=0;

            for(i=0;i<n;i++)

            {

                  if(islower(pr[i][2]))

                  {

                        strcpy(pr[j],pr[i]);

                        j++;

                  }

            }

            n=j;

            my=0;

            for(i=0;i<r;i++)

            {

                  k=0;

                  while(matrix[i][y-1][k]!='\0')

                  {

                        ch=matrix[i][y-1][k];

                        for(p=0;p<r;p++)

                        {

                              if(ch==nt[p])

                              {

                                    mx=p;

                              }

                        }

                        k++;

                        for(l=0;l<y;l++)

                        {

                              m=0;

                              while(matrix[mx][l][m]!='\0')

                              {

                                    pr[n][0]=nt[i];

                                    pr[n][1]='=';

                                    if(t[l]=='^')

                                    {

                                          pr[n][2]=matrix[mx][l][m];

                                          pr[n][3]='\0';

                                          flag=1;

                                    }

                                    else

                                    {

                                          pr[n][2]=t[l];

                                          pr[n][3]=matrix[mx][l][m];

                                          pr[n][4]='\0';

                                    }

                                    n++;

                                    m++;

                              }

                        }

                  }

            }

            for(i=0;i<n;i++)

            {

                  printf("\n%s",pr[i]);

            }

      }

      getch();

}

Output:

 Enter the no. of productions4

 

Enter the productionsA=B B=aS S=A B=w

 

 The symbols are:

ABSaw

Nonterminals:ABSZ

Terminals:aw^

Symbol Table is:

   a  w  ^

 A       B

 B S  Z

 S       A

 Z

 

B=aS

B=w

A=aS

A=wZ

S=B

Symbol Table is:

   a  w  ^

 A S  Z

 B S  Z

 S       B

 Z

 

B=aS

B=w

A=aS

A=wZ

S=aS

S=wZ

 

 


No comments:

Post a Comment

If you have any doubts please let me know