Advertisement
Guest User

Code

a guest
Nov 26th, 2012
213
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.81 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <string>
  4. using namespace std;
  5.  
  6. int main()
  7. {
  8.     int x = 0;
  9.     string grammar [25];
  10.     char ntl [25];
  11.     int ntlindex = 0;
  12.     string line;
  13.     ifstream file;
  14.     file.open("CFG.txt");
  15.     ofstream output;
  16.     output.open("TEST.txt");
  17.  
  18.  
  19.  
  20.     if (file.is_open())
  21.     {
  22.         while (file.good())
  23.         {
  24.             getline (file,line);
  25.             grammar[x] = line;
  26.             //cout << line << endl;
  27.  
  28.             x++;
  29.         }
  30.         file.close();
  31.     }
  32. /*
  33. cout << endl;
  34. cout << endl << endl;
  35.  
  36. for (int i = 0; i < x; i++)
  37. {
  38.     cout << grammar[i] << endl;
  39. }
  40.  
  41. cout << endl;
  42. cout << endl;
  43.  
  44. for (int i = 4; i < x; i++)
  45.     cout << grammar[i] << endl;
  46.  
  47. */cout << endl << endl;
  48.  
  49.  
  50. for (int i = 4; i < x; i++)
  51. {
  52.  
  53.     for (int j = 5; j < grammar[i].length(); j++)
  54.     {
  55.         cout << grammar[i][j];
  56.         if (grammar[i][j] == '$')
  57.         {
  58.             ntl[ntlindex]=grammar[i][0];
  59.             ntlindex++;
  60.         }
  61.  
  62.         if (grammar[i][j] == '|')
  63.         {
  64.             grammar[i][j] = '!';
  65.         }
  66.     }
  67.     cout << endl;
  68. }
  69.  
  70. cout << endl << endl;
  71.  
  72. /*for (int a = 4; a < x; a++)
  73. {
  74.     for (int b = 5; b < grammar[a].length(); b++)
  75.     {
  76.         for (int c = 0; c < 25 && ntl[c] != 'Ì'; c++)
  77.         {
  78.             if (grammar[a][b] == ntl[c])
  79.                 b++;
  80.         }
  81.         cout << grammar[a][b];
  82.     }
  83.     cout << endl;
  84. }*/
  85.  
  86.  
  87.  
  88.  
  89. /*for (int i = 0; i < 25 && ntl[i] != 'Ì' ; i++)
  90. {
  91.     cout << ntl[i] << endl;
  92. }*/
  93.  
  94. for (int i = 4; i < x; i++)
  95. {
  96.     //cout << "i is " << i << endl;
  97.  
  98.     for (int j = 5; j < grammar[i].length(); j++)  
  99.     {
  100.         int yes = 0;
  101.         //cout << "J is " << j << endl;
  102.         for (int k = 0; (k < 25) && (ntl[k] != 'Ì'); k++)
  103.         {
  104.  
  105.             //cout << "k is " << k << endl;
  106.             if (grammar[i][j] == ntl[k] && j < grammar[i].length())
  107.             {
  108.        
  109.                 yes = 1;
  110.                 //cout << "j is equal to " << j << endl;
  111.                 int temp = j;
  112.                 j++;
  113.  
  114.  
  115.                
  116.  
  117.                 for (j ; !(grammar[i][j] == '!') && (j < grammar[i].length()); j++)
  118.                 {
  119.                     cout << grammar[i][j];
  120.                     output << grammar[i][i];
  121.  
  122.    
  123.                 }
  124.                
  125.                
  126.                 cout << '|';
  127.                 output << '|';
  128.                 //cout << " j is equal to " << j << endl;
  129.                 j--;
  130.                
  131.                 for (j; !(grammar[i][j] == ' ') && !(grammar[i][j] == '!'); j--)
  132.                 {
  133.                     cout << "";
  134.                     output << "";
  135.                 }
  136.                
  137.                 j++;
  138.  
  139.                 //cout << "j is equal to" << j << endl;
  140.  
  141.                 for (j; j <= temp; j++)
  142.                 {
  143.                     cout << grammar[i][j];
  144.                     output << grammar[i][j];
  145.                 }
  146.  
  147.                 //cout << "j is equal to" << j << endl;
  148.             }
  149.             cout << grammar[i][j];
  150.             output << grammar[i][j];
  151.  
  152.             if (grammar[i][j] == '!' && yes == 1)
  153.             {
  154.                
  155.                
  156.                     for (int b = 5; b < grammar[i].length(); b++)
  157.                     {
  158.                         for (int c = 0; c < 25 && ntl[c] != 'Ì'; c++)
  159.                         {
  160.                             if (grammar[i][b] == ntl[c])
  161.                             b++;
  162.                         }
  163.                     cout << grammar[i][b];
  164.                     output << grammar[i][b];
  165.                     }
  166.            
  167.                
  168.             }
  169.  
  170.                        
  171.         }
  172.  
  173.        
  174.     }
  175.    
  176.     cout << endl;
  177.     output << endl;
  178. }
  179.  
  180. output.close();
  181.  
  182. return 0;
  183. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement