Advertisement
Kaidul

Parse Table

Feb 6th, 2013
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.43 KB | None | 0 0
  1. using namespace std;
  2. #include <cstring>
  3. #include <fstream>
  4. #include <iostream>
  5. #include <map>
  6. #include <set>
  7. #include <sstream>
  8. #include <vector>
  9.  
  10. #define Range 20
  11.  
  12. struct nonTerminal {
  13.     char nt;
  14.     vector <char> first, follow, production[Range], fp[Range];
  15.     vector <string> pstring;
  16.     int npro;
  17. } nterm[Range];
  18.  
  19. struct pos {
  20.     int x, y, z;
  21. } p;
  22.  
  23. map <char, int> m;
  24. map < char, map <char, string> > produc;
  25. vector <string> grammer;
  26. string parseTable[Range][Range];
  27. bool dp[Range];
  28.  
  29. set <char> row, column;
  30.  
  31. string charToString(char c) {
  32.     stringstream ss;
  33.     ss << c;
  34.     return (ss.str());
  35. }
  36.  
  37.  
  38. void first(char c) {
  39.     if(dp[m[c]]) return;
  40.     for(int i = 0; i < nterm[m[c] - 1].npro; i++) {
  41.         if(!m[nterm[m[c] - 1].production[i][0]]) {
  42.             nterm[m[c] - 1].first.push_back(nterm[m[c] - 1].production[i][0]);
  43.         } else {
  44.             first(nterm[m[c] - 1].production[i][0]);
  45.             nterm[m[c] - 1].first = nterm[m[nterm[m[c] - 1].production[i][0]] - 1].first;
  46.         }
  47.     }
  48.     dp[m[c]] = true;
  49. }
  50.  
  51. pos search(char c) {
  52.     for(int i = 0; i < grammer.size(); i++)
  53.         for(int j = 0; j < nterm[i].npro; j++)
  54.             for (int k = 0; k < nterm[i].production[j].size(); k++)
  55.                 if(nterm[i].production[j][k] == c) {
  56.                     p.x = i, p.y = j, p.z = k;
  57.                     return p;
  58.                 }
  59. }
  60.  
  61. void follow(char c) {
  62.     pos _p = search(c);
  63.     nterm[m[c] - 1].follow.push_back('$');
  64.     if( _p.z < nterm[_p.x].production[_p.y].size() - 1) {
  65.         char var = nterm[_p.x].production[_p.y][_p.z + 1];
  66.         if(!m[var]) { // f = (e)
  67.             nterm[m[c] - 1].follow.push_back(var);
  68.             return;
  69.         }
  70.         int item = m[var] - 1;
  71.  
  72.         for(int i = 0; i < nterm[item].first.size(); i++) {
  73.             char t = nterm[item].first[i];
  74.             if(t == '0') continue;
  75.             nterm[m[c] - 1].follow.push_back(t);
  76.         }
  77.         for (int k = 0; k < nterm[item].npro; k++) {
  78.             if(nterm[item].production[k].size() == 1 && nterm[item].production[k][0] == '0') {
  79.                 //here is a confusion
  80.                 for(int x = 0; x < nterm[m[nterm[_p.x].nt] - 1].follow.size(); x++) {
  81.                     char t = nterm[m[nterm[_p.x].nt] - 1].follow[x];
  82.                     if(t == '$') continue;
  83.                     nterm[m[c] - 1].follow.push_back(t);
  84.                 }
  85.                 break;
  86.             }
  87.         }
  88.  
  89.     } else {
  90.         for(int i = 0; i < nterm[m[nterm[_p.x].nt] - 1].follow.size(); i++) {
  91.             char t = nterm[m[nterm[_p.x].nt] - 1].follow[i];
  92.             if(t == '$') continue;
  93.             nterm[m[c] - 1].follow.push_back(t);
  94.         }
  95.     }
  96. }
  97.  
  98. int main() {
  99.     freopen("input.txt", "r", stdin);
  100.     freopen("output.txt", "w", stdout);
  101.     string in, str, save;
  102.     while(getline (cin, in)) {
  103.         grammer.push_back(in);
  104.     }
  105.     int i, j;
  106.     for(i = 0; i < grammer.size(); i++) {
  107.         stringstream stream(grammer[i]);
  108.         char variable;
  109.         stream >> variable;
  110.         row.insert(variable);
  111.         m[variable] = i + 1;
  112.         str = charToString(variable);
  113.         str.append(" -> ");
  114.         save = str; //back up
  115.         nterm[i].nt = variable;
  116.         stream >> variable;
  117.         stream >> variable;
  118.         int n = 0;
  119.         while(stream >> variable) {
  120.             if(variable == '|') {
  121.                 nterm[i].pstring.push_back(str);
  122.                 str = save; //restore
  123.                 n++;
  124.                 continue;
  125.             }
  126.             nterm[i].production[n].push_back(variable);
  127.             str.append(charToString(variable));
  128.         }
  129.         nterm[i].pstring.push_back(str);
  130.         nterm[i].npro = n + 1;
  131.     }
  132.  
  133.     memset(dp, false, sizeof dp);
  134.     for(j = 0; j < i; j++) first(nterm[j].nt);
  135.     for(j = 0; j < i; j++) follow(nterm[j].nt);
  136.  
  137.     for(j = 0; j < i; j++) {
  138.         for (int k =0; k < nterm[j].npro; k++) {
  139.             bool flag = false;
  140.             for(int l = 0; l < nterm[j].production[k].size(); l++) {
  141.                 char var = nterm[j].production[k][l];
  142.                 if(!m[var]) {
  143.                     if(var == '0') {
  144.                         for(int x = 0; x < nterm[j].follow.size(); x++) {
  145.                             nterm[j].fp[k].push_back(nterm[j].follow[x]);
  146.                         }
  147.                     } else nterm[j].fp[k].push_back(nterm[j].production[k][l]);
  148.                     break;
  149.                 }
  150.                 for(int x = 0; x < nterm[m[var] - 1].first.size(); x++) {
  151.                     if(nterm[m[var] - 1].first[x] == '0') flag = true;
  152.                     nterm[j].fp[k].push_back(nterm[m[var] - 1].first[x]);
  153.                 }
  154.                 if(flag == false) break;
  155.             }
  156.         }
  157.     }
  158.  
  159.  
  160.     for(j = 0; j < i; j++) {
  161.         for (int k = 0; k < nterm[j].npro; k++) {
  162.             for(int l = 0; l < nterm[j].fp[k].size(); l++) {
  163.                 column.insert(nterm[j].fp[k][l]);
  164.                 produc[nterm[j].nt][nterm[j].fp[k][l]] = nterm[j].pstring[k];
  165.             }
  166.         }
  167.     }
  168.  
  169.     set <char> ::iterator it, jt;
  170.  
  171.     cout << "\n-------------------------------------Table------------------------------------\n";
  172.     cout << "\nNon_Terminal                         Input Symbol\n\n";
  173.  
  174.     cout << "        ";
  175.     for (it = column.begin(); it != column.end(); it++) printf("%9c", *it);
  176.     cout << "\n\n";
  177.  
  178.     for (it = row.begin(); it != row.end(); it++) {
  179.         printf("%5c", *it);
  180.         for (jt = column.begin(); jt != column.end(); jt++) {
  181.             if(produc[*it][*jt]!= "") printf("%10s", (produc[*it][*jt]).c_str());
  182.             else printf("          ");
  183.         }
  184.         cout << "\n\n";
  185.     }
  186.     return false;
  187. }
  188.  
  189. /**
  190.  
  191. ----- input.txt -------
  192. e -> tE
  193. E -> +tE | 0
  194. t -> fT
  195. T -> *fT | 0
  196. f -> (e) | x | y
  197.  
  198.  
  199.  
  200. -----------output.txt-----------------
  201.  
  202.  
  203. -------------------------------------Table------------------------------------
  204.  
  205. Non_Terminal                         Input Symbol
  206.  
  207.                 $        (        )        *        +        x        y
  208.  
  209.     E    E -> 0              E -> 0            E -> +tE
  210.  
  211.     T    T -> 0              T -> 0  T -> *fT    T -> 0
  212.  
  213.     e             e -> tE                                 e -> tE   e -> tE
  214.  
  215.     f            f -> (e)                                  f -> x    f -> y
  216.  
  217.     t             t -> fT                                 t -> fT   t -> fT
  218.  
  219.  
  220.  
  221.  
  222. **/
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement