Advertisement
Zanjo_Betchi

First - Follow [Zanjo]

Sep 17th, 2018
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.77 KB | None | 0 0
  1. /*
  2. @Author : Zanjo
  3. @Date Of Creation : 25/08/2018
  4.  
  5. Production Rules Used:
  6. X -> S@
  7. S -> ABC
  8. A -> a|bbD
  9. B -> a|#
  10. C -> b|#
  11. D -> c|#
  12.  
  13. P.S.- Assuming Epsilon transition to be #
  14. */
  15. #include<bits/stdc++.h>
  16. #define len(a) sizeof(a)/sizeof(a[0])
  17. using namespace std;
  18. char expr[100][100]={0},first[100][100]={0},follow[100][100]={0};
  19. string s[] = {"","X=S@","S=ABC","A=a|bbD","B=a|#","C=b|#","D=c|#"};
  20. vector< char > duplicate;
  21. int num_of_production = len(s),epsilon_detector = 0,NT=0;
  22. void add_first(char r, char l){
  23.     duplicate.clear();
  24.     if(l=='#')epsilon_detector = 1;
  25.     int index=-1,j=1;
  26.     for(int i =1;i<num_of_production;i++){
  27.         if (first[i][0] == r) {
  28.             index =i;
  29.             break;
  30.         }
  31.     }
  32.     if(index !=-1){
  33.         while(first[index][j]!=0){
  34.                 duplicate.push_back(first[index][j]);
  35.                 j++;}
  36.         if(!(NT && l=='#') && !count(duplicate.begin(),duplicate.end(),l)){
  37.         first[index][j] = l;}
  38.     }else{
  39.         j=1;
  40.         while(first[j][0]!=0)j++;
  41.         first[j][0]=r;
  42.         first[j][1]=l;
  43.     }
  44. }
  45. void print_first(){
  46.     int j;
  47.     cout << "FIRST of the given expressions: "<<endl<<endl;
  48.     for(int i =1;i<num_of_production;i++){
  49.             j=0;
  50.             cout << first[i][j++] << " -> ";
  51.         while(first[i][j]!=0){
  52.             cout << first[i][j]<<" ";
  53.             j++;
  54.         }
  55.         cout << endl;
  56.         j=0;
  57.     }
  58.     cout << endl;
  59. }
  60. int check_NT(char r, char l){
  61.     int j =1;
  62.     NT=1;
  63.     for(int i =1;i<num_of_production;i++){
  64.         if(first[i][0] == l){
  65.             while(first[i][j]!=0){
  66.                 add_first(r,first[i][j]);
  67.                 j++;
  68.             }
  69.             return true;
  70.         }
  71.     }
  72.     return false;
  73. }
  74. int num_of_NT(){
  75.     int counter = 0;
  76.     for(int i =1;i<num_of_production;i++)
  77.     {
  78.         if (expr[i][2]>='A'&&expr[i][2]<='Z')counter++;
  79.  
  80.     }
  81.     return counter;
  82. }
  83. void print_follow(){
  84.      int j;
  85.      cout << "FOLLOW of the given expressions: "<<endl<<endl;
  86.     for(int i =1;i<num_of_production;i++){
  87.             j=0;
  88.             cout << follow[i][j++] << " -> ";
  89.         while(follow[i][j]!=0){
  90.             cout <<follow[i][j]<<" ";
  91.             j++;
  92.         }
  93.         cout << endl;
  94.     }
  95.     cout << endl;
  96. }
  97. void add_follow_T(char r,char l){
  98.     int index=0;
  99.  
  100.     for(int i =1;i<num_of_production;i++){
  101.         if(r==follow[i][0]){index =i;break;}
  102.  
  103.     }
  104.     int j=0;
  105.     while(follow[index][j]!=0){
  106.             if(follow[index][j]==l&&j>0)return;
  107.             j++;}
  108.     follow[index][j]= l;
  109. }
  110. void add_follow_NT_R(char r,char l){
  111.     int index1, index2,j=1,x=0;
  112.     epsilon_detector=0;
  113.     duplicate.clear();
  114.     for(int i =1;i<num_of_production;i++){
  115.         if(follow[i][0]==r)index1 = i;
  116.         if(follow[i][0]==l)index2=i;
  117.     }
  118.     while(follow[index1][x]!=0){
  119.             if(x>0)
  120.             duplicate.push_back(follow[index1][x]);
  121.             x++;}
  122.     while(follow[index2][j]!=0)
  123.     {   if(!count(duplicate.begin(),duplicate.end(),follow[index2][j])){
  124.             follow[index1][x]=follow[index2][j];
  125.             x++;
  126.         }
  127.         j++;
  128.         }
  129. }
  130.  
  131. void add_follow_NT(char r, int x , int y){
  132.     epsilon_detector=0;
  133.     duplicate.clear();
  134.     if(expr[x][y]==0 || expr[x][y]=='|'){add_follow_NT_R(r,expr[x][0]);}
  135.     int index1,j,index2;
  136.  
  137.  
  138.     for(int i =1;i<num_of_production;i++){
  139.         if(first[i][0]==expr[x][y])index1 =i;
  140.         if(follow[i][0]==r)index2=i;
  141.     }
  142.     j=0;
  143.     while(follow[index2][j]!=0){
  144.  
  145.             if(j>0)
  146.             duplicate.push_back(follow[index2][j]);
  147.             j++;}
  148.     int counter1 =1;
  149.     while(first[index1][counter1]!=0){
  150.         if(first[index1][counter1]=='#'){epsilon_detector=2;
  151.         counter1++;
  152.         continue;}
  153.         if(!count(duplicate.begin(),duplicate.end(),first[index1][counter1]))
  154.         {follow[index2][j]=first[index1][counter1];
  155.         j++;}
  156.         //cout <<"Adding "<<first[index1][counter1]<<index2<<endl ;
  157.         counter1++;
  158.     }
  159.     if(epsilon_detector==2)add_follow_NT(r,  x ,  y+1);
  160. }
  161. void print_production(){
  162.     cout << "Given Production Rules:\n\n";
  163.     for(int i =1;i<num_of_production;i++){
  164.         cout <<s[i]<<endl;
  165.     }
  166.     cout << endl;
  167. }
  168. int main(){
  169.     int j =0;
  170.     for(int i =1;i<num_of_production;i++)
  171.         strcpy(expr[i],s[i].c_str());
  172.  
  173.  
  174.     int x =num_of_NT()+1;
  175.     int counter = x;
  176.     while(counter--){
  177.         for(int i = 1;i <num_of_production;i++){
  178.                     j=2;
  179.                     while(expr[i][j]!=0){
  180.                             epsilon_detector = 0;
  181.                             NT=0;
  182.                         if(!(expr[i][j]>='A' && expr[i][j]<='Z')){
  183.  
  184.                             add_first(expr[i][0],expr[i][j]);
  185.                              while(expr[i][j]!='|' && expr[i][j]!=0)j++;
  186.                             j++;
  187.  
  188.                         }else{
  189.                                 //cout << "NT Detected - "<<expr[i][j]<<" ";
  190.                                 epsilon_detector=0;
  191.                                 NT=0;
  192.                             check_NT(expr[i][0],expr[i][j]);
  193.                             NT=0;
  194.                             while(epsilon_detector && expr[i][++j] >='A' && expr[i][j]<='Z'){
  195.                                 epsilon_detector=0;
  196.                                 NT=0;
  197.                                 check_NT(expr[i][0],expr[i][j]);
  198.  
  199.                             }
  200.                             NT=0;
  201.                             if(epsilon_detector){add_first(expr[i][0],'#');}
  202.                             epsilon_detector = 0;
  203.                             NT=0;
  204.                             while(expr[i][j]!='|' && expr[i][j]!=0)j++;
  205.                             j++;
  206.  
  207.                         }
  208.  
  209.  
  210.                 }
  211.  
  212.         }
  213.     }
  214.  
  215.  
  216.     /* FOLLOW */
  217.     for(int i =1;i<num_of_production;i++){
  218.         follow[i][0] = expr[i][0];
  219.     }
  220.     counter =x;
  221.     while(counter--){
  222.             if(counter == x-1)add_follow_T(expr[1][0],'$');
  223.         for(int i = 1;i <num_of_production;i++){
  224.                 j = 2;
  225.             while(expr[i][j]!=0){
  226.  
  227.                 if(expr[i][j]>='A' && expr[i][j] <='Z' && expr[i][j+1]>='A' && expr[i][j+1] <='Z'){
  228.                     add_follow_NT(expr[i][j],i,j+1);
  229.                 }else if(expr[i][j]>='A' && expr[i][j] <='Z' && (expr[i][j+1]==0 || expr[i][j+1]=='|')){
  230.                     add_follow_NT_R(expr[i][j],expr[i][0]);
  231.                 }else if (expr[i][j]>='A' && expr[i][j] <='Z' && !((expr[i][j+1]>='A' && expr[i][j+1] <='Z'))){
  232.                     add_follow_T(expr[i][j],expr[i][j+1]);
  233.                 }
  234.  
  235.             j++;
  236.  
  237.             }
  238.         }
  239.  
  240.  
  241.     }
  242.  
  243.     //cout << endl;
  244.     print_production();
  245.     print_first();
  246.     print_follow();
  247.  
  248.     return 0;
  249. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement