fork download
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <ctype.h>
  4.  
  5. char stack[100][10];
  6. int top = -1;
  7. int pos = 0;
  8. char input[100];
  9.  
  10. void push(const char *s) {
  11. strcpy(stack[++top], s);
  12. }
  13.  
  14. void pop() {
  15. top--;
  16. }
  17.  
  18. void printStack() {
  19. for (int i = 0; i <= top; i++)
  20. printf("%s", stack[i]);
  21. printf("\n");
  22. }
  23.  
  24. // Reduce with lookahead
  25. int reduce(char lookahead) {
  26.  
  27. // F -> id
  28. if (top >= 0 &&
  29. stack[top][0] >= 'a' &&
  30. stack[top][0] <= 'z')
  31. {
  32. pop();
  33. push("F");
  34. return 1;
  35. }
  36.  
  37. // F -> (E)
  38. if (top >= 2 &&
  39. strcmp(stack[top-2], "(") == 0 &&
  40. strcmp(stack[top-1], "E") == 0 &&
  41. strcmp(stack[top], ")") == 0)
  42. {
  43. pop(); pop(); pop();
  44. push("F");
  45. return 1;
  46. }
  47.  
  48. // T -> T * F
  49. if (top >= 2 &&
  50. strcmp(stack[top-2], "T") == 0 &&
  51. strcmp(stack[top-1], "*") == 0 &&
  52. strcmp(stack[top], "F") == 0)
  53. {
  54. pop(); pop(); pop();
  55. push("T");
  56. return 1;
  57. }
  58.  
  59. // T -> F
  60. if (top >= 0 && strcmp(stack[top], "F") == 0)
  61. {
  62. pop();
  63. push("T");
  64. return 1;
  65. }
  66.  
  67. // E -> E + T (مع مراعاة precedence)
  68. if (top >= 2 &&
  69. strcmp(stack[top-2], "E") == 0 &&
  70. strcmp(stack[top-1], "+") == 0 &&
  71. strcmp(stack[top], "T") == 0)
  72. {
  73. // لو الجاي * نستنى
  74. if (lookahead == '*')
  75. return 0;
  76.  
  77. pop(); pop(); pop();
  78. push("E");
  79. return 1;
  80. }
  81.  
  82. // ✅ التعديل هنا
  83. // E -> T (مع مراعاة precedence)
  84. if (top >= 0 && strcmp(stack[top], "T") == 0)
  85. {
  86. // لو الجاي * يبقى لسه هنكوّن T * F
  87. if (lookahead == '*')
  88. return 0;
  89.  
  90. pop();
  91. push("E");
  92. return 1;
  93. }
  94.  
  95. return 0;
  96. }
  97.  
  98. int main() {
  99. printf("Enter an Expression:\n");
  100. fgets(input, 100, stdin);
  101.  
  102. while (input[pos]) {
  103.  
  104. if (isspace(input[pos])) {
  105. pos++;
  106. continue;
  107. }
  108.  
  109. char temp[2] = {input[pos], '\0'};
  110. push(temp);
  111. pos++;
  112.  
  113. printf("Shift: ");
  114. printStack();
  115.  
  116. while (reduce(input[pos])) {
  117. printf("Reduce: ");
  118. printStack();
  119. }
  120. }
  121.  
  122. while (reduce('\0')) {
  123. printf("Reduce: ");
  124. printStack();
  125. }
  126.  
  127. if (top == 0 && strcmp(stack[0], "E") == 0)
  128. printf("String Accepted\n");
  129. else
  130. printf("String Rejected\n");
  131.  
  132. return 0;
  133. }
Success #stdin #stdout 0.01s 5320KB
stdin
(a+(b*a))
stdout
Enter an Expression:
Shift: (
Shift: (a
Reduce: (F
Reduce: (T
Reduce: (E
Shift: (E+
Shift: (E+(
Shift: (E+(b
Reduce: (E+(F
Reduce: (E+(T
Reduce: (E+(E
Shift: (E+(E*
Shift: (E+(E*a
Reduce: (E+(E*F
Reduce: (E+(E*T
Reduce: (E+(E*E
Shift: (E+(E*E)
Shift: (E+(E*E))
String Rejected