Untitled

 avatar
unknown
plain_text
9 months ago
3.4 kB
12
Indexable
#include <stdio.h>
#include <string.h>
#include <ctype.h>

char input[50][10];   // Token array
char stack[50][10];   // Stack array
int top = -1;
int n = 0;            // Number of tokens

// Function to print the stack
void printStack() {
    for (int j = 0; j <= top; j++)
        printf("%s", stack[j]);
}

// Reduce function: Applies rules like E+E -> E
int reduce() {
    int change = 0;

    // Rule: E + E → E
    if (top >= 2 && strcmp(stack[top - 2], "E") == 0 &&
        strcmp(stack[top - 1], "+") == 0 &&
        strcmp(stack[top], "E") == 0) {
        strcpy(stack[top - 2], "E");
        top -= 2;
        printf("REDUCE BY E -> E+E\n");
        change = 1;
    }

    // Rule: E * E → E
    else if (top >= 2 && strcmp(stack[top - 2], "E") == 0 &&
             strcmp(stack[top - 1], "*") == 0 &&
             strcmp(stack[top], "E") == 0) {
        strcpy(stack[top - 2], "E");
        top -= 2;
        printf("REDUCE BY E -> E*E\n");
        change = 1;
    }

    // Rule: ( E ) → E
    else if (top >= 2 && strcmp(stack[top - 2], "(") == 0 &&
             strcmp(stack[top - 1], "E") == 0 &&
             strcmp(stack[top], ")") == 0) {
        strcpy(stack[top - 2], "E");
        top -= 2;
        printf("REDUCE BY E -> (E)\n");
        change = 1;
    }

    // Rule: id → E
    else if (top >= 0 && strcmp(stack[top], "id") == 0) {
        strcpy(stack[top], "E");
        printf("REDUCE BY E -> id\n");
        change = 1;
    }

    return change;
}

// Tokenizer for the input string
void tokenize(char* expr) {
    int i = 0;
    while (expr[i] != '\0') {
        if (isspace(expr[i])) {
            i++;
            continue;
        } else if (isalpha(expr[i])) {
            if (expr[i] == 'i' && expr[i + 1] == 'd') {
                strcpy(input[n++], "id");
                i += 2;
            } else {
                printf("Invalid identifier, only 'id' is allowed.\n");
                return;
            }
        } else if (strchr("()+*-/", expr[i])) {
            char temp[2];
            temp[0] = expr[i];
            temp[1] = '\0';
            strcpy(input[n++], temp);
            i++;
        } else {
            printf("Invalid character: %c\n", expr[i]);
            return;
        }
    }
}

int main() {
    char expr[100];
    printf("Enter the input string (use 'id' only): ");
    scanf("%s", expr);

    tokenize(expr);

    printf("\nStack\t\tInput\t\tAction\n");
    printf("--------------------------------------------------\n");

    int i = 0;
    while (i < n) {
        // SHIFT
        strcpy(stack[++top], input[i++]);

        printStack();
        printf("\t\t");
        for (int j = i; j < n; j++)
            printf("%s", input[j]);
        printf("\t\tSHIFT\n");

        // Try reducing after every shift
        while (reduce()) {
            printStack();
            printf("\t\t");
            for (int j = i; j < n; j++)
                printf("%s", input[j]);
            printf("\t\t");
            printf("\n");
        }
    }

    // Final reduction after input is consumed
    while (reduce()) {
        printStack();
        printf("\t\t\t\t\n");
    }

    // Final acceptance check
    if (top == 0 && strcmp(stack[top], "E") == 0) {
        printf("\nAccepted: Input string is successfully parsed.\n");
    } else {
        printf("\nRejected: Input string could not be parsed.\n");
    }

    return 0;
}
Editor is loading...
Leave a Comment