T_tasir256

Untitled

Nov 17th, 2021
570
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.01 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. using namespace std;
  4. int main()
  5. {
  6.     int n, j, l, i, m;
  7.     int len[10] = {};
  8.     string a, b1, b2, flag;
  9.     char c;
  10.     cout << "Enter the Parent Non-Terminal : ";
  11.     cin >> c;
  12.     a.push_back(c);
  13.     b1 += a + "\'->";
  14.     b2 += a + "\'\'->";
  15.     ;
  16.     a += "->";
  17.     cout << "Enter total number of productions : ";
  18.     cin >> n;
  19.     for (i = 0; i < n; i++)
  20.     {
  21.         cout << "Enter the Production " << i + 1 << " : ";
  22.         cin >> flag;
  23.         len[i] = flag.size();
  24.         a += flag;
  25.         if (i != n - 1)
  26.         {
  27.             a += "|";
  28.         }
  29.     }
  30.     cout << "The Production Rule is : " << a << endl;
  31.     char x = a[3];
  32.     for (i = 0, m = 3; i < n; i++)
  33.     {
  34.         if (x != a[m])
  35.         {
  36.             while (a[m++] != '|')
  37.                 ;
  38.         }
  39.         else
  40.         {
  41.             if (a[m + 1] != '|')
  42.             {
  43.                 b1 += "|" + a.substr(m + 1, len[i] - 1);
  44.                 a.erase(m - 1, len[i] + 1);
  45.             }
  46.             else
  47.             {
  48.                 b1 += "#";
  49.                 a.insert(m + 1, 1, a[0]);
  50.                 a.insert(m + 2, 1, '\'');
  51.                 m += 4;
  52.             }
  53.         }
  54.     }
  55.     char y = b1[6];
  56.     for (i = 0, m = 6; i < n - 1; i++)
  57.     {
  58.         if (y == b1[m])
  59.         {
  60.             if (b1[m + 1] != '|')
  61.             {
  62.                 flag.clear();
  63.                 for (int s = m + 1; s < b1.length(); s++)
  64.                 {
  65.                     flag.push_back(b1[s]);
  66.                 }
  67.                 b2 += "|" + flag;
  68.                 b1.erase(m - 1, flag.length() + 2);
  69.             }
  70.             else
  71.             {
  72.                 b1.insert(m + 1, 1, b1[0]);
  73.                 b1.insert(m + 2, 2, '\'');
  74.                 b2 += "#";
  75.                 m += 5;
  76.             }
  77.         }
  78.     }
  79.     b2.erase(b2.size() - 1);
  80.     cout << "After Left Factoring : " << endl;
  81.     cout << a << endl;
  82.     cout << b1 << endl;
  83.     cout << b2 << endl;
  84.     return 0;
  85. }
Advertisement
Add Comment
Please, Sign In to add comment