[text] an

Viewer

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

Editor

You can edit this paste and save as new:


File Description
  • an
  • Paste Code
  • 11 Dec-2023
  • 2.65 Kb
You can Share it: