Advertisement
maycod23

LL_1_Parser

Jun 1st, 2022
775
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.26 KB | None | 0 0
  1. // #include<iostream>
  2. // using namespace std;
  3. #include<stdio.h>
  4. #include<string.h>
  5. #define TSIZE 128
  6. int table[100][TSIZE];
  7. char terminal[TSIZE];
  8. char nonterminal[26];
  9. struct product {
  10.     char str[100];
  11.     int len;
  12. } pro[20];
  13. int no_pro;
  14. char first[26][TSIZE];
  15. char follow[26][TSIZE];
  16. char first_rhs[100][TSIZE];
  17. int isNT(char c) {return c >= 'A' && c <= 'Z';}
  18. void readFromFile()
  19. {   FILE* fptr;
  20.     fptr = fopen("code.txt", "r");
  21.     char buffer[255];
  22.     int i, j;
  23.     while (fgets(buffer, sizeof(buffer), fptr)) {
  24.         printf("%s", buffer);
  25.         j = 0;
  26.         nonterminal[buffer[0] - 'A'] = 1;
  27.         for (i = 0; i < strlen(buffer) - 1; ++i) {
  28.             if (buffer[i] == '|') {
  29.                 ++no_pro;
  30.                 pro[no_pro - 1].str[j] = '\0';
  31.                 pro[no_pro - 1].len = j;
  32.                 pro[no_pro].str[0] = pro[no_pro - 1].str[0];
  33.                 pro[no_pro].str[1] = pro[no_pro - 1].str[1];
  34.                 pro[no_pro].str[2] = pro[no_pro - 1].str[2];
  35.                 j = 3;
  36.             }
  37.             else {
  38.                 pro[no_pro].str[j] = buffer[i];
  39.                 ++j;
  40.                 if (!isNT(buffer[i]) && buffer[i] != '-' && buffer[i] != '>') {
  41.                     terminal[buffer[i]] = 1;
  42.                 }
  43.             }
  44.         }
  45.         pro[no_pro].len = j;
  46.         ++no_pro;
  47.     }
  48. }
  49. void add_FIRST_A_to_FOLLOW_B(char A, char B)
  50. {   int i;
  51.     for (i = 0; i < TSIZE; ++i) {
  52.         if (i != '^')
  53.             follow[B - 'A'][i] = follow[B - 'A'][i] || first[A - 'A'][i];
  54.     }
  55. }
  56. void add_FOLLOW_A_to_FOLLOW_B(char A, char B)
  57. {   int i;
  58.     for (i = 0; i < TSIZE; ++i) {
  59.         if (i != '^')
  60.             follow[B - 'A'][i] = follow[B - 'A'][i] || follow[A - 'A'][i];
  61.     }
  62. }
  63. void FOLLOW()
  64. {   int t = 0;
  65.     int i, j, k, x;
  66.     while (t++ < no_pro) {
  67.         for (k = 0; k < 26; ++k) {
  68.             if (!nonterminal[k]) continue;
  69.             char nt = k + 'A';
  70.             for (i = 0; i < no_pro; ++i) {
  71.                 for (j = 3; j < pro[i].len; ++j) {
  72.                     if (nt == pro[i].str[j]) {
  73.                         for (x = j + 1; x < pro[i].len; ++x) {
  74.                             char sc = pro[i].str[x];
  75.                             if (isNT(sc)) {
  76.                                 add_FIRST_A_to_FOLLOW_B(sc, nt);
  77.                                 if (first[sc - 'A']['^'])
  78.                                     continue;
  79.                             }
  80.                             else {
  81.                                 follow[nt - 'A'][sc] = 1;
  82.                             }
  83.                             break;
  84.                         }
  85.                         if (x == pro[i].len)
  86.                             add_FOLLOW_A_to_FOLLOW_B(pro[i].str[0], nt);
  87.                     }
  88.                 }
  89.             }
  90.         }
  91.     }
  92. }
  93. void add_FIRST_A_to_FIRST_B(char A, char B) {
  94.     int i;
  95.     for (i = 0; i < TSIZE; ++i) {
  96.         if (i != '^') {
  97.             first[B - 'A'][i] = first[A - 'A'][i] || first[B - 'A'][i];
  98.         }
  99.     }
  100. }
  101. void FIRST() {
  102.     int i, j;
  103.     int t = 0;
  104.     while (t < no_pro) {
  105.         for (i = 0; i < no_pro; ++i) {
  106.             for (j = 3; j < pro[i].len; ++j) {
  107.                 char sc = pro[i].str[j];
  108.                 if (isNT(sc)) {
  109.                     add_FIRST_A_to_FIRST_B(sc, pro[i].str[0]);
  110.                     if (first[sc
  111.                               - 'A']['^'])
  112.                         continue;
  113.                 }
  114.                 else {
  115.                     first[pro[i].str[0]
  116.                           - 'A'][sc] = 1;
  117.                 }
  118.                 break;
  119.             }
  120.             if (j == pro[i].len)
  121.                 first[pro[i].str[0]
  122.                       - 'A']['^'] = 1;
  123.         }
  124.         ++t;
  125.  
  126.     }
  127. }
  128. void add_FIRST_A_to_FIRST_RHS__B(char A, int B) {
  129.     int i;
  130.     for (i = 0; i < TSIZE; ++i) {
  131.         if (i != '^')
  132.             first_rhs[B][i] = first[A
  133.                                     - 'A'][i] || first_rhs[B][i];
  134.  
  135.     }
  136. }
  137. void FIRST_RHS() {
  138.     int i, j;
  139.     int t = 0;
  140.     while (t < no_pro) {
  141.         for (i = 0; i < no_pro; ++i) {
  142.             for (j = 3; j < pro[i].len; ++j) {
  143.                 char sc = pro[i].str[j];
  144.                 if (isNT(sc)) {
  145.                     add_FIRST_A_to_FIRST_RHS__B(sc, i);
  146.                     if (first[sc
  147.                               - 'A']['^'])
  148.                         continue;
  149.                 }
  150.                 else {
  151.                     first_rhs[i][sc] = 1;
  152.                 }
  153.                 break;
  154.             }
  155.             if (j == pro[i].len)
  156.                 first_rhs[i]['^'] = 1;
  157.         }
  158.         ++t;
  159.  
  160.     }
  161. }
  162. int main() {
  163.     printf("UE193117 UDESH BIND PRODUCTIONS:");
  164.     readFromFile();
  165.     follow[pro[0].str[0]
  166.            - 'A']['$'] = 1;
  167.     FIRST();
  168.     FOLLOW();
  169.     FIRST_RHS();
  170.     int i, j, k;
  171.     printf("\n");
  172.     for (i = 0; i < no_pro; ++i) {
  173.         if (i == 0 || (pro[i - 1].str[0] != pro[i].str[0])) {
  174.             char c = pro[i].str[0];
  175.             printf("FIRST OF %c: ", c);
  176.             for (j = 0; j < TSIZE; ++j) {
  177.                 if (first[c - 'A'][j]) {
  178.                     printf("%c ", j);
  179.                 }
  180.             }
  181.             printf("\n");
  182.         }
  183.     }
  184.     printf("\n");
  185.     for (i = 0; i < no_pro; ++i) {
  186.         if (i == 0 || (pro[i - 1].str[0] != pro[i].str[0])) {
  187.             char c = pro[i].str[0];
  188.             printf("FOLLOW OF %c: ", c);
  189.             for (j = 0; j < TSIZE; ++j) {
  190.                 if (follow[c - 'A'][j]) {
  191.                     printf("%c ", j);
  192.                 }
  193.             }
  194.             printf("\n");
  195.         }
  196.     }
  197.     printf("\n");
  198.     for (i = 0; i < no_pro; ++i) {
  199.         printf("FIRST OF %s: ", pro[i].str);
  200.         for (j = 0; j < TSIZE; ++j) {
  201.             if (first_rhs[i][j]) {
  202.                 printf("%c ", j);
  203.             }
  204.         }
  205.         printf("\n");
  206.     }
  207.     terminal['$'] = 1;
  208.     terminal['^'] = 0;
  209.     printf("\n");
  210.     printf("\n\t**************** LL(1) PARSING TABLE *******************\n");
  211.     printf("\t--------------------------------------------------------\n");
  212.     printf("%-10s", "");
  213.     for (i = 0; i < TSIZE; ++i) {
  214.         if (terminal[i]) printf("%-10c", i);
  215.     }
  216.     printf("\n");
  217.     int p = 0;
  218.     for (i = 0; i < no_pro; ++i) {
  219.         if (i != 0 && (pro[i].str[0] != pro[i - 1].str[0]))
  220.             p = p + 1;
  221.         for (j = 0; j < TSIZE; ++j) {
  222.             if (first_rhs[i][j] && j != '^') {
  223.                 table[p][j] = i + 1;
  224.             }
  225.             else if (first_rhs[i]['^']) {
  226.                 for (k = 0; k < TSIZE; ++k) {
  227.                     if (follow[pro[i].str[0] - 'A'][k]) {
  228.                         table[p][k] = i + 1;
  229.                     }
  230.                 }
  231.             }
  232.         }
  233.     }
  234.     k = 0;
  235.     for (i = 0; i < no_pro; ++i) {
  236.         if (i == 0 || (pro[i - 1].str[0] != pro[i].str[0])) {
  237.             printf("%-10c", pro[i].str[0]);
  238.             for (j = 0; j < TSIZE; ++j) {
  239.                 if (table[k][j]) {
  240.                     printf("%-10s", pro[table[k][j] - 1].str);
  241.                 }
  242.                 else if (terminal[j]) {
  243.                     printf("%-10s", "");
  244.                 }
  245.             }
  246.             ++k;
  247.             printf("\n");
  248.         }
  249.     }
  250.     return 0;
  251. }
  252.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement