Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //S->aB|c operator precedent
- //S->AB|C not operator precedent
- #include <stdio.h>
- #include <string.h>
- #include <stdbool.h>
- bool is_operator_precedent(const char *rule) {
- bool last_was_non_terminal = false;
- for (int i = 0; rule[i]; i++) {
- if (rule[i] >= 'A' && rule[i] <= 'Z') {
- if (last_was_non_terminal)
- return false; // Two consecutive non-terminals
- last_was_non_terminal = true;
- } else if (rule[i] >= 'a' && rule[i] <= 'z') {
- last_was_non_terminal = false; // Terminal resets the flag
- } else if (rule[i] == 'e') {
- return false; // Epsilon not allowed
- }
- }
- return true;
- }
- int main() {
- char production[20];
- printf("Enter production rule (e.g., S-->AB|a): ");
- fgets(production, sizeof(production), stdin);
- production[strcspn(production, "\n")] = '\0'; // Remove newline
- printf("%s operator precedent grammar\n",
- is_operator_precedent(production) ? "Operator precedent" : "Not operator precedent");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement