[text] Ganjai Abhiram

Viewer

copydownloadembedprintName: Ganjai Abhiram
  1. #include<stdio.h>
  2. #include<string.h>
  3. #include<stdlib.h>
  4. char *input;
  5. int i=0;
  6. char lasthandle[6],stack[50],handles[][5]={"E*E","E+E","i"};
  7. int top=0,l;
  8. char prec[4][4]={
  9.                 '>', '<','<','>',
  10.                 '>', '>','<','>',
  11.                 '>', '>','>','>',
  12.                 '<', '<','<','A'};
  13.  
  14. int getindex(char c)
  15. {
  16.         switch(c)
  17.     {
  18.                 case '+':return 0;
  19.                 case '*':return 1;
  20.                 case 'i':return 2;
  21.                 case '$':return 3;
  22.     }
  23. }
  24.  
  25.  
  26. int shift()
  27. {
  28.         stack[++top]=input[i++];
  29.         stack[top+1]='\0';
  30. }
  31.  
  32.  
  33. int reduce()
  34. {
  35. int i,len,found,t;
  36. for(i=0;i<5;i++)//selecting handles
  37.     {
  38.     len=strlen(handles[i]);
  39.     if(stack[top]==handles[i][0]&&top+1>=len)
  40.         {
  41.         found=1;
  42.         for(t=0;t<len;t++)
  43.             {
  44.             if(stack[top-t]!=handles[i][t])
  45.                 {
  46.                 found=0;
  47.                 break;
  48.                 }
  49.             }
  50.         if(found==1)
  51.             {
  52.             stack[top-t+1]='E';
  53.             top=top-t+1;
  54.             strcpy(lasthandle,handles[i]);
  55.             stack[top+1]='\0';
  56.             return 1;//successful reduction
  57.             }
  58.         }
  59.    }
  60. return 0;
  61. }
  62.  
  63.  
  64.  
  65. void dispstack()
  66. {
  67.         int j;
  68.         for(j=0;j<=top;j++)
  69.                 printf("%c",stack[j]);
  70. }
  71.  
  72.  
  73.  
  74. void dispinput()
  75. {
  76. int j;
  77. for(j=i;j<l;j++)
  78.     printf("%c",*(input+j));
  79. }
  80.  
  81. void main()
  82. {
  83.         int j;
  84.         input=(char*)malloc(50*sizeof(char));
  85.         printf("\nEnter the string\n");
  86.         scanf("%s",input);
  87.         input=strcat(input,"$");
  88.         l=strlen(input);
  89.         strcpy(stack,"$");
  90.         printf("\nSTACK\tINPUT\tACTION");
  91.         while(i<l)
  92.         {
  93.                 shift();
  94.                 printf("\n");
  95.                 dispstack();
  96.                 printf("\t");
  97.                 dispinput();
  98.                 printf("\tShift");
  99.                 if(prec[getindex(stack[top])][getindex(input[i])]=='>')
  100.                         {
  101.                         while(reduce())
  102.                                 {
  103.                                 printf("\n");
  104.                                 dispstack();
  105.                                 printf("\t");
  106.                                 dispinput();
  107.                                 printf("\tReduced: E->%s",lasthandle);
  108.                                 }
  109.                         }
  110.         }
  111.         if(strcmp(stack,"$E$")==0)
  112.                 printf("\nAccepted\n");
  113.         else
  114.                 printf("\nNot Accepted\n");
  115. }

Editor

You can edit this paste and save as new:


File Description
  • Ganjai Abhiram
  • Paste Code
  • 11 Dec-2023
  • 1.91 Kb
You can Share it: