Advertisement
Sri27119

cd8 operator precedent

Nov 20th, 2024
14
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.08 KB | None | 0 0
  1. //S->aB|c operator precedent
  2. //S->AB|C not operator precedent
  3.  
  4. #include <stdio.h>
  5. #include <string.h>
  6. #include <stdbool.h>
  7.  
  8. bool is_operator_precedent(const char *rule) {
  9. bool last_was_non_terminal = false;
  10.  
  11. for (int i = 0; rule[i]; i++) {
  12. if (rule[i] >= 'A' && rule[i] <= 'Z') {
  13. if (last_was_non_terminal)
  14. return false; // Two consecutive non-terminals
  15. last_was_non_terminal = true;
  16. } else if (rule[i] >= 'a' && rule[i] <= 'z') {
  17. last_was_non_terminal = false; // Terminal resets the flag
  18. } else if (rule[i] == 'e') {
  19. return false; // Epsilon not allowed
  20. }
  21. }
  22. return true;
  23. }
  24.  
  25. int main() {
  26. char production[20];
  27.  
  28. printf("Enter production rule (e.g., S-->AB|a): ");
  29. fgets(production, sizeof(production), stdin);
  30. production[strcspn(production, "\n")] = '\0'; // Remove newline
  31.  
  32. printf("%s operator precedent grammar\n",
  33. is_operator_precedent(production) ? "Operator precedent" : "Not operator precedent");
  34.  
  35. return 0;
  36. }
  37.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement