Download as pdf or txt
Download as pdf or txt
You are on page 1of 2

SLR Parsing Table

Program:-
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_STATES 10
#define MAX_SYMBOLS 10
struct Production {
char lhs;
char rhs[20];
};
void closure(int state);
void constructSLRTable();
void printSLRTable();
struct Production grammar[] = {
{'S', "E"},
{'E', "E+T"},
{'E', "T"},
{'T', "T*F"},
{'T', "F"},
{'F', "(E)"},
{'F', "id"}
};
int numProductions = sizeof(grammar) / sizeof(grammar[0]);
void closure(int state) {
}
char parsingTable[MAX_STATES][MAX_SYMBOLS];
int main() {
constructSLRTable();
printSLRTable();
return 0;
}
void constructSLRTable() {
memset(parsingTable, 'e', sizeof(parsingTable));
}
void printSLRTable() {
printf("\nSLR Parsing Table:\n\n");
printf("State\t");
for (char symbol = 'a'; symbol <= 'z'; ++symbol) {
printf("%c\t", symbol);
}
printf("$\t\n");
for (int state = 0; state < MAX_STATES; ++state) {
printf("%d\t", state);
for (char symbol = 'a'; symbol <= 'z'; ++symbol) {
printf("%c\t", parsingTable[state][symbol]);
}
printf("%c\t\n", parsingTable[state]['$']);
}
}

Output:-

You might also like