Friday, 31 July 2020

9. PARSING OF INPUT STRING USING THE GIVEN SPF

Source code:

#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<ctype.h>
void main()
{
      int i,j,n,k=0,l=0,p=0,z=0,d=0,r=0,y=0;
      int c[20],m,ch,mx,my,x,x1,s1,flag;
      char pr[20][20],s[20],ntt[20],nt[10],t[10],pa[20];
      char string[20],string1[10][10],sym,ch1,ch2,handle[20];
      clrscr();
      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++;
                  l=k;
            }
      }
      for(i=0;i<l;i++)
      {
            for(j=i+1;j<l;j++)
            {
                  if(s[i]>=s[j])
                  {
                        ch=s[i];
                        s[i]=s[j];
                        s[j]=ch;
                  }
            }
      }
      for(i=0;i<l;i++)
      {
            ch=s[i];
            if(ch!=s[i+1])
             {
                  if(ch!='=')
                  {
                        s[p]=ch;
                        ntt[z]=s[p];
                        p++;
                        z++;
                  }
            }
         d=z;
      }
      printf("\n The symbols are:\n");
      for(i=0;i<d;i++)
      {
            printf("%c",ntt[i]);
      }
      for(i=0;i<d;i++)
      {
            if(isupper(ntt[i]))
            {
                  nt[r]=ntt[i];
                  r++;
            }
            else
            {
                  t[y]=ntt[i];
                  y++;
            }
      }
      printf("\n");
      printf("\nnon-terminals:");
      for(i=0;i<r;i++)
      printf("%c ",nt[i]);
      printf("\n");
      printf("\nterminals:");
      for(i=0;i<y;i++)
      printf("%c ",t[i]);
      printf("\n\nEnter the entries for SPF");
      for(i=0;i<2*d;i++)
      scanf("%d",&c[i]);
      printf("         SPF");
      printf("\n");
      printf("  ");
      for(i=0;i<d;i++)
      printf("%2c ",ntt[i]);
      printf("\n");
      printf("f  ");
      for(i=0;i<d;i++)
      printf("%2d ",c[i]);
      printf("\n");
      printf("g  ");
      for(i=0;i<d;i++)
      printf("%2d ",c[i+7]);
      getch();
//-----------------------------SPF parsing-----------------------------------
      printf("\n\nEnter the input string");
      scanf("%s",&pa);
      printf("\n");
      for(i=0;i<n;i++)
      {
            for(j=2;j<strlen(pr[i]);j++)
            {
                  string1[i][j-2]=pr[i][j];
            }
            string1[i][j-2] ='\0';
      }
      for(j=0;j<strlen(pa);j++)
      {
            i=0;
            while(i<strlen(pa))
            {
                  if(pa[i]=='#' && strlen(pa)>3)
                  {
                        sym='<';
                  }
                  else if(pa[i+1]=='#' && strlen(pa)>3)
                  {
                        sym='>';
                  }
                  else
                  {
                        ch1=pa[i];
                        for(x=0;x<d;x++)
                        {
                              if(ch1==ntt[x])
                              mx=x;
                        }
                        ch2=pa[i+1];
                        for(y=0;y<d;y++)
                        {
                              if(ch2==ntt[y])
                              my=y+7;
                        }
                        if(c[mx]<c[my])
                        sym='<';
                        else if(c[mx]>c[my])
                        sym='>';
                        else
                        sym='=';
                  }
                  if(sym=='=')
                  {
                        i=i;
                  }
                  else if(sym=='<')
                  {
                        x1=i;
                  }
                  else if(sym=='>')
                  {
                        s1=0;
                        for(k=x1+1;k<=i;k++)
                        {
                              handle[s1]=pa[k];
                              s1++;
                              flag=1;
                        }
                        handle[s1]='\0';
                        z=0,y=0;
                        for(x=0;x<n;x++)
                        {
                              if(strcmp(handle,string1[x])==0)
                              {
                              printf("\nHandle is %s = %c",string1[x],pr[x][0]);
                                    pa[x1+1]=pr[x][0];
                                    if(s1>1)
                                    {
                                          z=x1+s1+1;
                                          y=x1+2;
                                          while(pa[z]!='\0')
                                          {
                                                pa[y]=pa[z];
                                                z++;
                                                y++;
                                          }
                                          pa[y]='\0';
                                    }
                                    i=0;
                                    s1=0;
                                    printf("  string is %s",pa);
                                    flag=0;
                              }
                        }
                  }
                  i++;
            }
      }
      if(flag==0)
      {
            printf("\n string is  valid" );
      }
      else if(flag==1)
      {
            printf(" \n string is not valid");
      }

      getch();

}

Output:
Enter the no. of productions 4

Enter the productions  Z= bMb  M=a  M=(L  L=Ma)

 The symbols are: ()LMZab

non-terminals M Z

Terminals :( ) a b

Enter the entries for SPF :  2 8 8 7 1 9 4 5 9 2 4 1 7 7

SPF:
         (     )    L     M    Z     a     b
f        2    8    8     7      1     9    4
g       5     9    2     4      1     7   7

Enter the input string #b(aa)b#

 Handle is a = M           string is #b(Ma)b#
 Handle is Ma) = L       string is #b(Lb#
 Handle is (L = M         string is #bMb#
 Handle is bMb = Z      string is #Z#

 String is valid

No comments:

Post a Comment

If you have any doubts please let me know