LL(1)

0

 #include <stdio.h>

#include <string.h>

#include <stdbool.h>


#define MAX_RULES 10

#define MAX_NON_TERMINALS 10

#define MAX_TERMINALS 10


struct ParsingTable {

    char nonTerminals[MAX_NON_TERMINALS];

    char terminals[MAX_TERMINALS];

    char table[MAX_NON_TERMINALS][MAX_TERMINALS][MAX_RULES];

};


void initializeParsingTable(struct ParsingTable *pt) {

    memset(pt->table, '\0', sizeof(pt->table));

    memset(pt->nonTerminals, '\0', sizeof(pt->nonTerminals));

    memset(pt->terminals, '\0', sizeof(pt->terminals));

}


void displayGrammar(char grammar[MAX_RULES][20], int numRules) {

    printf("Grammar Used:\n");

    for (int i = 0; i < numRules; ++i) {

        printf("%s\n", grammar[i]);

    }

}


void displayParsingTable(struct ParsingTable *pt) {

    printf("\nParsing Table:\n");


    printf("\tTerminals:\t");

    for (int i = 0; pt->terminals[i] != '\0'; ++i) {

        printf("%c\t", pt->terminals[i]);

    }

    printf("\n");


    for (int i = 0; pt->nonTerminals[i] != '\0'; ++i) {

        printf("NonTerminal %c:\t", pt->nonTerminals[i]);

        for (int j = 0; pt->terminals[j] != '\0'; ++j) {

            if (strlen(pt->table[i][j]) == 0) {

                printf("-\t");

            } else {

                printf("%s\t", pt->table[i][j]);

            }

        }

        printf("\n");

    }

}


int main() {

    char grammar[MAX_RULES][20] = {

        "S->aAd",

        "A->b",

        "A->cA"

    };

    int numRules = 3;


    struct ParsingTable pt;

    initializeParsingTable(&pt);


    strcpy(pt.nonTerminals, "SA");

    strcpy(pt.terminals, "abcd");


    for (int i = 0; i < numRules; ++i) {

        char nonTerminal = grammar[i][0];

        int row = strchr(pt.nonTerminals, nonTerminal) - pt.nonTerminals;


        char terminal = grammar[i][3];

        int col = strchr(pt.terminals, terminal) - pt.terminals;


        strcpy(pt.table[row][col], &grammar[i][3]);

    }


    displayGrammar(grammar, numRules);

    displayParsingTable(&pt);


    return 0;

}

Post a Comment

0Comments

GUYS IF YOU HAVE ANY DOUBT. PLEASE LET ME KNOW

Post a Comment (0)