fork download
  1. // Simple Shift Reduce parsing for E → E + E | id
  2. #include <stdio.h>
  3. #include <string.h>
  4. #include <ctype.h>
  5. // Stack to hold tokens and non-terminals during parsing
  6. char stack[100][10];
  7. int top = -1; // Points to the top of the stack
  8. int index1 = 0; // Input pointer/index
  9. char input[100]; // Holds the input string (e.g., "a+b+c")
  10.  
  11. // Push a symbol (token or non-terminal) onto the stack
  12. void push(const char *s)
  13. {
  14. strcpy(stack[++top], s);
  15. }
  16. // Pop the top of the stack
  17. void pop()
  18. {
  19. top--;
  20. }
  21. // Print current stack contents
  22. void printStack()
  23. {
  24. for (int i = 0; i <= top; i++) printf("%s", stack[i]);
  25. printf("\n");
  26. }
  27.  
  28.  
  29. // Try to apply a reduction rule to the top of the stack
  30. int reduce()
  31. {
  32. // Rule 1: E → E + E
  33. if (top >= 2 &&
  34. strcmp(stack[top-2], "E")==0 &&
  35. (strcmp(stack[top-1], "+")==0||strcmp(stack[top-1], "*")==0) &&
  36. strcmp(stack[top], "E")==0)
  37. {
  38. pop();
  39. pop();
  40. pop(); // Remove "E + E" (3 pops)
  41. push("E"); // Push "E" onto the stack
  42. return 1; // Indicate that a reduction occurred
  43. }
  44.  
  45. // Rule 2: E → id
  46. if (top!=-1 && stack[top][0]>='a'&&stack[top][0]<='z')
  47. {
  48. pop(); // Remove "id" (1 pop)
  49. push("E"); // Push "E" onto the stack
  50. return 1;
  51. }
  52.  
  53. //Rule 3: (E) -> E
  54. if(top>=2&&strcmp(stack[top-2], "(")==0&&strcmp(stack[top-1], "E")==0&&strcmp(stack[top], ")")==0){
  55. pop();
  56. pop();
  57. pop();
  58. push("E");
  59. return 1;
  60. }
  61.  
  62.  
  63. return 0; // No reduction matched
  64. }
  65.  
  66.  
  67. int main()
  68. {
  69. // Read input string (e.g., a+b+c)
  70. fgets(input, sizeof(input), stdin); //input string must not include white spaces
  71. input[strcspn(input, "\n")] = '\0';//this one will scan all input untill it reach "\n" then will return index
  72. // so if input is a + b
  73. //it will present a + b\n\0
  74. //in this case strcspn will return 5
  75. //then input[5]will = "\0"which mean endofstring
  76.  
  77. //E+a
  78. // Main parsing loop: continue until input ends and no more reductions are possible
  79. while (input[index1] != '\0')
  80. {
  81. if(input[index1] == ' '){//to ignore white spaces
  82. index1++;
  83. continue;
  84. }
  85. // SHIFT step: if input is not yet fully consumed
  86. // Check for "id" token
  87. char temp[2] = {input[index1], '\0'}; // turn character into string
  88. push(temp); // push actual character (like 'x')
  89. index1 ++; // Move input pointer ahead by 1
  90.  
  91. // Print stack after shift
  92. printf("Shift: ");
  93. printStack();
  94.  
  95.  
  96. // REDUCE step: apply all possible reductions after shift
  97. while (reduce())
  98. {
  99. printf("Reduce: ");
  100. printStack();
  101.  
  102. }
  103. }
  104.  
  105. // Final check: input is accepted if the stack has a single symbol "E"
  106. if (top == 0 && strcmp(stack[0], "E")==0)
  107. printf("String Accepted\n");
  108. else
  109. printf("String Rejected\n");
  110.  
  111. return 0;
  112. }
Success #stdin #stdout 0s 5312KB
stdin
Standard input is empty
stdout
String Rejected