7)PREDICTIVE PARSING ALGORITHM
Mon Apr 07 2025 19:31:39 GMT+0000 (Coordinated Universal Time)
Saved by
@sem
#include <stdio.h>
#include <string.h>
char prod[2][10] = { "S->aA", "A->b" };
char nonTerminals[2][10] = { "S", "A" };
char terminals[3][10] = { "a", "b", "$" };
char table[3][4][15]; // 3 rows (S, A + header), 4 columns (a, b, $, + header)
int getRow(char nt) {
switch (nt) {
case 'S': return 1;
case 'A': return 2;
}
return 0;
}
int getCol(char t) {
switch (t) {
case 'a': return 1;
case 'b': return 2;
case '$': return 3;
}
return 0;
}
int main() {
// Initialize table with empty strings
for (int i = 0; i < 3; i++)
for (int j = 0; j < 4; j++)
strcpy(table[i][j], " ");
// Fill headers
strcpy(table[0][0], " ");
strcpy(table[0][1], "a");
strcpy(table[0][2], "b");
strcpy(table[0][3], "$");
strcpy(table[1][0], "S");
strcpy(table[2][0], "A");
// Fill table using FIRST sets
strcpy(table[getRow('S')][getCol('a')], "S->aA");
strcpy(table[getRow('A')][getCol('b')], "A->b");
// Print the table
printf("Predictive Parsing Table:\n");
printf("-----------------------------------------\n");
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 4; j++) {
printf("%-12s", table[i][j]);
}
printf("\n-----------------------------------------\n");
}
return 0;
}
content_copyCOPY
Comments