fork download
  1. #include<string.h>
  2. #include <stdio.h>
  3. #include<conio.h>
  4.  
  5. char a[10];
  6. int top=-1,i;
  7. void error(){
  8. printf("Syntax Error");
  9. }
  10. void push(char k[]) //Pushes The Set Of Characters on to the Stack
  11. {
  12. for(i=0;k[i]!='\0';i++)
  13. {
  14. if(top<9)
  15. a[++top]=k[i];
  16. }
  17. }
  18. char TOS() //Returns TOP of the Stack
  19. {
  20. return a[top];
  21. }
  22. void pop() //Pops 1 element from the Stack
  23. {
  24. if(top>=0)
  25. a[top--]='\0';
  26. }
  27. void display() //Displays Elements Of Stack
  28. {
  29. for(i=0;i<=top;i++)
  30. printf("%c",a[i]);
  31. }
  32. void display1(char p[],int m) //Displays The Present Input String
  33. {
  34. int l;
  35. printf("\t");
  36. for(l=m;p[l]!='\0';l++)
  37. printf("%c",p[l]);
  38. }
  39. char* stack(){
  40. return a;
  41. }
  42. int main()
  43. {
  44. char ip[20],r[20],st,an;
  45. int ir,ic,j=0,k;
  46. char t[5][6][10]={"$","$","TH","$","TH","$",
  47. "+TH","$","e","e","$","e",
  48. "$","$","FU","$","FU","$",
  49. "e","*FU","e","e","$","e",
  50. "$","$","(E)","$","i","$"};
  51. clrscr();
  52. printf("\nEnter any String(Append with $)");
  53. gets(ip);
  54. printf("Stack\tInput\tOutput\n\n");
  55. push("$E");
  56. display();
  57. printf("\t%s\n",ip);
  58. for(j=0;ip[j]!='\0';)
  59. {
  60. if(TOS()==an)
  61. {
  62. pop();
  63. display();
  64. display1(ip,j+1);
  65. printf("\tPOP\n");
  66. j++;
  67. }
  68. an=ip[j];
  69. st=TOS();
  70. if(st=='E')ir=0;
  71. else if(st=='H')ir=1;
  72. else if(st=='T')ir=2;
  73. else if(st=='U')ir=3;
  74. else if(st=='F')ir=4;
  75. else {
  76. error();
  77. break;
  78. }
  79. if(an=='+')ic=0;
  80. else if(an=='*')ic=1;
  81. else if(an=='(')ic=2;
  82. else if(an==')')ic=3;
  83. else if((an>='a'&&an<='z')||(an>='A'&&an<='Z')){ic=4;an='i';}
  84. else if(an=='$')ic=5;
  85. strcpy(r,strrev(t[ir][ic]));
  86. strrev(t[ir][ic]);
  87. pop();
  88. push(r);
  89. if(TOS()=='e')
  90. {
  91. pop();
  92. display();
  93. display1(ip,j);
  94. printf("\t%c->%c\n",st,238);
  95. }
  96. else{
  97. display();
  98. display1(ip,j);
  99. printf("\t%c->%s\n",st,t[ir][ic]);
  100. }
  101. if(TOS()=='$'&&an=='$')
  102. break;
  103. if(TOS()=='$'){
  104. error();
  105. break;
  106. }
  107. }
  108. k=strcmp(stack(),"$");
  109. if(k==0 && i==strlen(ip))
  110. printf("\n Given String is accepted");
  111. else
  112. printf("\n Given String is not accepted");
  113. return 0;
  114. }
  115.  
  116.  
  117.  
Success #stdin #stdout #stderr 0.02s 7000KB
stdin
Standard input is empty
stdout
Standard output is empty
stderr
ERROR: /home/s2ahvl/prog:116:0: Syntax error: end_of_file_in_quasi_quotation
ERROR: '$runtoplevel'/0: Undefined procedure: program/0
   Exception: (3) program ? EOF: exit