The following is the C Program for Implementation of functionalities of Predictive Parser.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 | #include <stdio.h> #include <string.h> char prol[7][10] = { "S", "A", "A", "B", "B", "C", "C" }; char pror[7][10] = { "A", "Bb", "Cd", "aB", "@", "Cc", "@" }; char prod[7][10] = { "S->A", "A->Bb", "A->Cd", "B->aB", "B->@", "C->Cc", "C->@" }; char first[7][10] = { "abcd", "ab", "cd", "a@", "@", "c@", "@" }; char follow[7][10] = { "$", "$", "$", "a$", "b$", "c$", "d$" }; char table[5][6][10]; int numr(char c) { switch (c) { case 'S': return 0; case 'A': return 1; case 'B': return 2; case 'C': return 3; case 'a': return 0; case 'b': return 1; case 'c': return 2; case 'd': return 3; case '$': return 4; } return (2); } int main() { int i, j, k; for (i = 0; i < 5; i++) for (j = 0; j < 6; j++) strcpy(table[i][j], " "); printf("The following grammar is used for Parsing Table:\n"); for (i = 0; i < 7; i++) printf("%s\n", prod[i]); printf("\nPredictive parsing table:\n"); fflush(stdin); for (i = 0; i < 7; i++) { k = strlen(first[i]); for (j = 0; j < 10; j++) if (first[i][j] != '@') strcpy(table[numr(prol[i][0]) + 1][numr(first[i][j]) + 1], prod[i]); } for (i = 0; i < 7; i++) { if (strlen(pror[i]) == 1) { if (pror[i][0] == '@') { k = strlen(follow[i]); for (j = 0; j < k; j++) strcpy(table[numr(prol[i][0]) + 1][numr(follow[i][j]) + 1], prod[i]); } } } strcpy(table[0][0], " "); strcpy(table[0][1], "a"); strcpy(table[0][2], "b"); strcpy(table[0][3], "c"); strcpy(table[0][4], "d"); strcpy(table[0][5], "$"); strcpy(table[1][0], "S"); strcpy(table[2][0], "A"); strcpy(table[3][0], "B"); strcpy(table[4][0], "C"); printf("\n--------------------------------------------------------\n"); for (i = 0; i < 5; i++) for (j = 0; j < 6; j++) { printf("%-10s", table[i][j]); if (j == 5) printf("\n--------------------------------------------------------\n"); } } |
Output: Predictive parser in C
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | The following grammar is used for Parsing Table: S->A A->Bb A->Cd B->aB B->@ C->Cc C->@ Predictive parsing table: -------------------------------------------------------- a b c d $ -------------------------------------------------------- S S->A S->A S->A S->A -------------------------------------------------------- A A->Bb A->Bb A->Cd A->Cd -------------------------------------------------------- B B->aB B->@ B->@ B->@ -------------------------------------------------------- C C->@ C->@ C->@ -------------------------------------------------------- |