realsdx

SRL_TABLE

Feb 11th, 2020
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 11.42 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <ctype.h>
  4. int i2,j2,i,j,k,m2=0,n=0,o,p,ns=0,tn=0,rr=0,ch=0;
  5. char read[15][10],gl[15],gr[15][10],temp,templ[15],tempr[15][10],*ptr,temp2[5],dfa[15][15];
  6. char variables[15]={'\0'}, terminals[15]={'\0'};
  7. char slr[15][15][10]={'\0'};
  8. char foll[10];
  9. int len_var, len_ter;
  10. struct states//State for forming cannonical sets
  11. {
  12.     char lhs[15],rhs[15][10];
  13.     int n;
  14. }I[15];
  15. void follow(char c);//calculate follow
  16. void first(char c);//calculate first
  17. int compstruct(struct states s1,struct states s2)//Compare states with GOTO states and merge
  18. {
  19.     int t;
  20.     if(s1.n!=s2.n)
  21.         return 0;
  22.     if( strcmp(s1.lhs,s2.lhs)!=0 )
  23.         return 0;
  24.     for(t=0;t<s1.n;t++)
  25.         if( strcmp(s1.rhs[t],s2.rhs[t])!=0 )
  26.             return 0;
  27.     return 1;
  28. }
  29. void moreprod()//For finding CLOSURE of a set
  30. {
  31.     int r,s,t,l1=0,rr1=0;
  32.     char *ptr1,read1[15][10];
  33.     for(r=0;r<I[ns].n;r++)
  34.     {
  35.         ptr1=strchr(I[ns].rhs[l1],'.');
  36.         t=ptr1-I[ns].rhs[l1];
  37.         if( t+1==strlen(I[ns].rhs[l1]) )
  38.         {
  39.             l1++;
  40.             continue;
  41.         }
  42.         temp=I[ns].rhs[l1][t+1];
  43.         l1++;
  44.         for(s=0;s<rr1;s++)
  45.             if( temp==read1[s][0] )
  46.                 break;
  47.         if(s==rr1)
  48.         {
  49.             read1[rr1][0]=temp;
  50.             rr1++;
  51.         }
  52.         else
  53.             continue;
  54.  
  55.         for(s=0;s<n;s++)
  56.         {
  57.             if(gl[s]==temp)
  58.             {
  59.                 I[ns].rhs[I[ns].n][0]='.';
  60.                 I[ns].rhs[I[ns].n][1]='\0';
  61.                 strcat(I[ns].rhs[I[ns].n],gr[s]);
  62.                 I[ns].lhs[I[ns].n]=gl[s];
  63.                 I[ns].lhs[I[ns].n+1]='\0';
  64.                 I[ns].n++;
  65.             }
  66.         }
  67.     }
  68. }
  69. void canonical(int l)//To find the CANONICAL SET of items using GOTO and SHIFT
  70. {
  71.     int t1;
  72.     char read1[15][10],rr1=0,*ptr1;
  73.     for(i=0;i<I[l].n;i++)
  74.     {
  75.         temp2[0]='.';
  76.         ptr1=strchr(I[l].rhs[i],'.');
  77.         t1=ptr1-I[l].rhs[i];
  78.         if( t1+1==strlen(I[l].rhs[i]) )
  79.             continue;
  80.         temp2[1]=I[l].rhs[i][t1+1];
  81.         temp2[2]='\0';
  82.         for(j=0;j<rr1;j++)
  83.             if( strcmp(temp2,read1[j])==0 )
  84.                 break;
  85.         if(j==rr1)
  86.         {
  87.             strcpy(read1[rr1],temp2);
  88.             read1[rr1][2]='\0';
  89.             rr1++;
  90.         }
  91.         else
  92.             continue;
  93.         for(j=0;j<I[0].n;j++)
  94.         {
  95.             ptr=strstr(I[l].rhs[j],temp2);
  96.             if( ptr )
  97.             {
  98.                 templ[tn]=I[l].lhs[j];
  99.                 templ[tn+1]='\0';
  100.                 strcpy(tempr[tn],I[l].rhs[j]);
  101.                 tn++;
  102.             }
  103.         }
  104.         for(j=0;j<tn;j++)
  105.         {
  106.             ptr=strchr(tempr[j],'.');
  107.             p=ptr-tempr[j];
  108.             tempr[j][p]=tempr[j][p+1];
  109.             tempr[j][p+1]='.';
  110.             I[ns].lhs[I[ns].n]=templ[j];
  111.             I[ns].lhs[I[ns].n+1]='\0';
  112.             strcpy(I[ns].rhs[I[ns].n],tempr[j]);
  113.             I[ns].n++;
  114.         }
  115.         moreprod();
  116.         for(j=0;j<ns;j++)
  117.         {
  118.             //if ( memcmp(&I[ns],&I[j],sizeof(struct states))==1 )
  119.             if( compstruct(I[ns],I[j])==1 )
  120.             {
  121.                 I[ns].lhs[0]='\0';
  122.                 for(k=0;k<I[ns].n;k++)
  123.                     I[ns].rhs[k][0]='\0';
  124.                 I[ns].n=0;
  125.                 dfa[l][j]=temp2[1];
  126.                 break;
  127.             }
  128.         }
  129.         if(j<ns)
  130.         {
  131.             tn=0;
  132.             for(j=0;j<15;j++)
  133.             {
  134.                 templ[j]='\0';
  135.                 tempr[j][0]='\0';
  136.             }
  137.             continue;
  138.         }
  139.         dfa[l][j]=temp2[1];
  140.         printf("\n\nI%d :",ns);
  141.         for(j=0;j<I[ns].n;j++)
  142.             printf("\n\t%c -> %s",I[ns].lhs[j],I[ns].rhs[j]);
  143.         ns++;
  144.         tn=0;
  145.         for(j=0;j<15;j++)
  146.         {
  147.             templ[j]='\0';
  148.             tempr[j][0]='\0';
  149.         }
  150.     }
  151. }
  152.  
  153. void extract_var_char()//For determining TERMINAL and NOT-TERMINAL
  154. {
  155.     for (i=0;i<n;i++)
  156.     {
  157.         if (gl[i] >= 'A' && gl[i] <= 'Z')
  158.             if (!strchr(variables,gl[i]))
  159.                 sprintf(variables,"%s%c",variables,gl[i]);
  160.         for (j=0;j<strlen(gr[i]);j++)
  161.             if (gr[i][j] < 'A' || gr[i][j] > 'Z')
  162.                 if (!strchr(terminals,gr[i][j]))
  163.                     sprintf(terminals,"%s%c",terminals,gr[i][j]);
  164.     }
  165.     strcat(terminals,"$");
  166.     printf ("Variables : %s\nTerminals : %s\n",variables,terminals);
  167. }
  168.  
  169. void print_dfa(){
  170.     printf("\n\tDFA Table...\n\n");
  171.     for(i=0;i<ns;i++)
  172.     {
  173.         printf("I%d : ",i);
  174.         for(j=0;j<ns;j++)
  175.             if(dfa[i][j]!='\0')
  176.                 printf("'%c'->I%d | ",dfa[i][j],j);      
  177.         printf("\n\n");
  178.     }
  179. }
  180. void change_slr()
  181. {
  182.     strcpy(slr[2][0],"r2");
  183.     strcpy(slr[3][1],"r4");
  184.     strcpy(slr[4][2],"s4");
  185.     strcpy(slr[4][6],"8");
  186.     strcpy(slr[4][7],"2");
  187.     strcpy(slr[4][8],"3");
  188.     strcpy(slr[5][1],"r6");
  189. }
  190. void display_slr()//IT displays the SLR TABLE
  191. {
  192.     printf("\n\t\tSLR(1) Table...\n\n\t");
  193.     for (i=0;i<len_ter;i++)
  194.         printf("%c\t",terminals[i]);
  195.     for (i=0;i<len_var;i++)
  196.         printf("%c\t",variables[i]);
  197.     printf("\n");
  198.     for (i=0;i<ns;i++){
  199.         printf("I%d\t",i);
  200.         for (j=0;j<len_var+len_ter;j++)
  201.             printf("%s\t",slr[i][j]);
  202.         printf("\n");
  203.     }
  204. }
  205. void main()
  206. {
  207.     FILE *f;
  208.     int l;
  209.     for(i=0;i<15;i++)
  210.     {
  211.         I[i].n=0;
  212.         I[i].lhs[0]='\0';
  213.         I[i].rhs[0][0]='\0';
  214.         dfa[i][0]='\0';
  215.     }
  216.     f=fopen("grammar.txt","r");//Reading Grammar from text file
  217.     while(!feof(f))
  218.     {
  219.         fscanf(f,"%c",&gl[n]);
  220.         fscanf(f,"%s\n",gr[n]);
  221.         n++;
  222.     }
  223.     printf("\tGiven Grammar...\n");
  224.     for(i=0;i<n;i++)
  225.         printf("\t\t%c -> %s\n",gl[i],gr[i]);
  226.     I[0].lhs[0]='Z';
  227.     strcpy(I[0].rhs[0],".S");
  228.     I[0].n++;
  229.     l=0;
  230.     for(i=0;i<n;i++)
  231.     {
  232.         temp=I[0].rhs[l][1];
  233.         l++;
  234.         for(j=0;j<rr;j++)
  235.             if( temp==read[j][0] )
  236.                 break;
  237.         if(j==rr)
  238.         {
  239.             read[rr][0]=temp;
  240.             rr++;
  241.         }
  242.         else
  243.             continue;
  244.         for(j=0;j<n;j++)
  245.         {
  246.             if(gl[j]==temp)
  247.             {
  248.                 I[0].rhs[I[0].n][0]='.';
  249.                 strcat(I[0].rhs[I[0].n],gr[j]);
  250.                 I[0].lhs[I[0].n]=gl[j];
  251.                 I[0].n++;
  252.             }
  253.         }
  254.     }
  255.     ns++;
  256.     printf("\n\tCanonicals...\n");
  257.     printf("\nI%d :\n",ns-1);
  258.     for(i=0;i<I[0].n;i++)
  259.         printf("\t%c -> %s\n",I[0].lhs[i],I[0].rhs[i]);
  260.  
  261.     for(l=0;l<ns;l++){
  262.         canonical(l);
  263.     }
  264.     printf("\n\n");
  265.     print_dfa();
  266.      //~~~~~~~~~~~~~~~~~~~~~Construction of SLR(1) Table~~~~~~~~~~~~~~~~~~//
  267.     int t,tempo;      
  268.     extract_var_char();
  269.     len_var = strlen(variables);
  270.     len_ter = strlen(terminals);
  271.     int columns = len_var + len_ter;
  272.  
  273.     for (i=0;i<ns;i++)
  274.     {
  275.         for (j=0;j<ns;j++)
  276.             if (dfa[i][j] != '\0')
  277.                 if (strchr(terminals,dfa[i][j]))
  278.                     sprintf(slr[i][strchr(terminals,dfa[i][j])-terminals],"s%d",j);
  279.                 else if (strchr(variables,dfa[i][j]))
  280.                     sprintf(slr[i][len_ter+strchr(variables,dfa[i][j])-variables],"%d",j);
  281.         for (j=0;j<I[i].n;j++)
  282.         {
  283.             int temp_ind = strlen(I[i].rhs[j])-1; // to make the 1st state that end with "S." accept.
  284.             if (I[i].rhs[j][temp_ind] == '.')
  285.             {
  286.                 if (I[i].rhs[j][temp_ind-1] == 'S' && i==1)
  287.                     sprintf(slr[i][strchr(terminals,'$')-terminals],"accept");
  288.                 else
  289.                 {      
  290.                     follow(I[i].lhs[j]);
  291.                     int ha,prod_num,tempo_store;
  292.                     for (ha=0;gl[ha]!='\0';ha++)
  293.                     {
  294.                         if (strncmp(gr[ha],I[i].rhs[j],temp_ind)==0)
  295.                             prod_num = ha + 1;
  296.                     }
  297.                     for (ha=0; ha<m2 ; ha++)
  298.                     {
  299.                         tempo_store = strchr(terminals,foll[ha])-terminals;
  300.                         if (strlen(slr[i][tempo_store]) > 0)
  301.                         {
  302.                             if (slr[i][tempo_store][0] == 's')
  303.                                 printf("\nSR-Conflict occurred....\n");
  304.                             else
  305.                                 printf("\nRR-Conflict occurred....\n");
  306.                         }
  307.                         else
  308.                             sprintf(slr[i][tempo_store],"r%d",prod_num);
  309.                     }
  310.                     m2=0;
  311.                 }
  312.             }
  313.         }
  314.     }
  315.     change_slr();
  316.     display_slr();
  317.     //~~~~~~~~~~~~~~~~~~~~~~~~~~~Check for the given string~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~//
  318.     char input[100],stack[100]={'\0'};//Declaing Stack for PARSING process
  319.     printf("\nGive the input string : ");
  320.     scanf("%s",input);
  321.     int temp32,temp36,len_rhs,state,top=0,input_len = strlen(input);
  322.     if (input[input_len-1] != '$')
  323.         input[input_len++] = '$';
  324.     i=0;
  325.     stack[0] = '0';
  326.     printf("\n\nSTACK\t\tINPUT STRING\t\tACTION\n");
  327.     printf("-----\t\t------------\t\t------\n");
  328.     while(i<input_len)
  329.     {
  330.         printf("%s\t\t",stack);
  331.         for (j=i;j<input_len;j++)
  332.             printf("%c",input[j]);
  333.             printf("\t\t\t");
  334.         if (strchr(terminals,input[i]))
  335.         {
  336.             temp32 = strchr(terminals,input[i])-terminals; // only terminals are present in the input....
  337.             state = stack[top] - 48;          
  338.         }
  339.         else
  340.         {
  341.             printf("Unknown symbol encountered...\n");
  342.             break;
  343.         }
  344.         if (stack[top]=='1' && input[i]=='$' && strcmp(slr[state][temp32],"accept") == 0 )
  345.         {
  346.             printf("String Accepted...\n");
  347.             break;
  348.         }
  349.         else if(slr[state][temp32][0]=='s')
  350.         {
  351.             top++;
  352.             stack[top++] = input[i];
  353.             stack[top] = slr[state][temp32][1];
  354.             i++;
  355.             printf("Shift %c\n",terminals[temp32]);
  356.         }
  357.         else if(slr[state][temp32][0]=='r')
  358.         {
  359.             len_rhs = strlen(gr[slr[state][temp32][1]-49]);
  360.             printf("Reduce-step : %c -> %s \n",gl[slr[state][temp32][1]-49],gr[slr[state][temp32][1]-49]);
  361.             for (j=0;j<2*len_rhs;j++)
  362.                 stack[top--]='\0';
  363.             temp36 = stack[top] - 48;
  364.             stack[++top] = gl[slr[state][temp32][1]-49];
  365.             stack[++top] = slr[temp36][len_ter + strchr(variables,stack[top-1])-variables][0];
  366.         }
  367.         else
  368.         {
  369.             printf("String rejected...\n");
  370.             break;
  371.         }  
  372.     }
  373. }
  374. void follow(char c)
  375. {
  376.     if(gl[0]==c)
  377.     foll[m2++]='$';
  378.     for(i2=0;i2<n;i2++)
  379.         for(j2=0;j2<strlen(gr[i2]);j2++)
  380.             if(gr[i2][j2]==c)
  381.             {
  382.                 if(gr[i2][j2+1]!='\0')
  383.                 first(gr[i2][j2+1]);
  384.                 if(gr[i2][j2+1]=='\0'&&c!=gl[i2])
  385.                 follow(gl[i2]);
  386.             }
  387. }
  388. void first(char c)
  389. {
  390.     int k;
  391.     if(!(isupper(c)))
  392.         foll[m2++]=c;
  393.     for(k=0;k<n;k++)
  394.         if(gl[k]==c)
  395.             if(gr[k][0]=='$')
  396.                 follow(gl[i2]);
  397.             else if(islower(gr[k][0]))
  398.                 foll[m2++]=gr[k][0];
  399.             else
  400.                 first(gr[k][0]);
  401. }
  402.  
  403. // S S+A
  404. // S A
  405. // A A*B
  406. // A B
  407. // B (S)
  408. // B i
Add Comment
Please, Sign In to add comment