Advertisement
palash25

shift reduce

Apr 28th, 2015
199
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.09 KB | None | 0 0
  1. shift reduce parser
  2.  
  3.  
  4. #include<stdio.h>
  5. #include<stdlib.h>
  6. #include<string.h>
  7. char ip_sym[15],stack[15];
  8. int ip_ptr=0,st_ptr=0,len,i;
  9. char temp[2],temp2[2];
  10. char act[15];
  11. void check();
  12. void main()
  13. {
  14.     clrscr();
  15. printf("\n\n\t Shift Reduce Parser\n");
  16. printf("\n\t***** ****** ******");
  17. printf("\n Grammar\n\n");
  18. printf("E->E+E\nE->E/E\n");
  19. printf("E->E*E\nE->a/b");
  20. printf("\n Enter the Input Symbol:\t");
  21. for(i=0;i<5;i++)
  22. {
  23. scanf("%c",&ip_sym[i]);
  24. }
  25. printf("\n\n\t Stack Implementation Table");
  26. printf("\n Stack\t\t Input Symbol\t\t Action");
  27.  
  28. strcpy(act,"shift");
  29. temp[0]=ip_sym[ip_ptr];
  30. temp[1]='\0';
  31. strcat(act,temp);
  32. printf("\n $\t\t %s$\t\t\t%s ",ip_sym,act);
  33. len=strlen(ip_sym);
  34.  
  35. for(i=0;i<=len-1;i++)
  36. {
  37. stack[st_ptr]=ip_sym[ip_ptr];
  38. stack[st_ptr+1]='\0';
  39. ip_sym[ip_ptr]=' ';
  40. //printf("\n$%s\t\t%s$\t\t\t",stack,ip_sym);
  41.  
  42. ip_ptr++;
  43.  
  44. strcpy(act,"shift");
  45. temp[0]=ip_sym[ip_ptr];
  46. temp[1]='\0';
  47. strcat(act,temp);
  48.  
  49. check();
  50. st_ptr++;
  51. }
  52. st_ptr++;
  53. check();
  54. check();
  55. }
  56. void check()
  57. {
  58. int flag=0;
  59. temp2[0]=stack[st_ptr];
  60. temp2[1]='\0';
  61. if(!strcmp(temp2,"a"))
  62. {
  63. printf("\n$%s\t\t%s$\t\t\tE->a",stack,ip_sym);
  64. stack[st_ptr]='E';
  65. if(ip_ptr!=len)
  66.    printf("\n$%s\t\t%s$\t\t\t%s",stack,ip_sym,act);
  67.  
  68. flag=1;
  69. }
  70.  if(!strcmp(temp2,"b"))
  71. {
  72. printf("\n$%s\t\t%s$\t\t\tE->b",stack,ip_sym);
  73. stack[st_ptr]='E';
  74. if(ip_ptr!=len)
  75.     printf("\n$%s\t\t%s$\t\t\t%s",stack,ip_sym,act);
  76. flag=1;
  77. }
  78.  
  79.  
  80. if((!strcmp(temp2,"+"))||(!strcmp(temp2,"*"))||(!strcmp(temp2,"/")))
  81. {
  82. printf("\n$%s\t\t%s$\t\t\t%s",stack,ip_sym,act);
  83. flag=1;
  84. }
  85. if((!strcmp(stack,"E+E"))||(!strcmp(stack,"E/E"))||(!strcmp(stack,"E*E")))
  86. {
  87. if(!strcmp(stack,"E+E"))
  88. printf("\n$%s\t\t%s$\t\t\tE->E+E",stack,ip_sym);
  89. else if(!strcmp(stack,"E/E"))
  90. printf("\n$%s\t\t%s$\t\t\tE->E/E",stack,ip_sym);
  91. else
  92. printf("\n$%s\t\t%s$\t\t\tE->E*E",stack,ip_sym);
  93.  
  94. strcpy(stack,"E");
  95. st_ptr=0;
  96. flag=1;
  97.  
  98. }
  99. if(!strcmpi(stack,"E")&&ip_ptr==len)
  100. {
  101. printf("\n$%s\t\t%s$\t\t\tAccept",stack,ip_sym);
  102. getch();
  103. exit(0);
  104. }
  105. if(flag==0)
  106. {
  107. printf("\n$%s\t\t%s$\t\t\tReject",stack,ip_sym);
  108. getch();
  109. exit(0);
  110. }
  111. return;
  112. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement