Guest User

Untitled

a guest
Dec 15th, 2017
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.57 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<bits/stdc++.h>
  3.  
  4. using namespace std;
  5.  
  6. vector<string>alpha;
  7. vector<string>beta;
  8.  
  9. int main()
  10. {
  11. freopen("lab_5.txt","r",stdin);
  12. string str;
  13.  
  14. while(getline(cin,str))
  15. {
  16.  
  17. int flag=0;
  18. string parent,child;
  19. cout<<str<<endl;
  20.  
  21. int flag_alpha=1;
  22.  
  23. for(int i=0; i<str.size(); i++)
  24. {
  25. if(str[i]=='-')
  26. {
  27. flag=1;
  28. i++;
  29. continue;
  30. }
  31. if(flag==0)
  32. {
  33. if(str[i]!=' ')
  34. parent+=str[i];
  35.  
  36.  
  37. }
  38. else if(flag==1)
  39. {
  40.  
  41. if(!((str[i]>='a' && str[i]<='z') || (str[i]>='A' && str[i]<='Z')))
  42. {
  43. string symbol;
  44.  
  45. if(str[i]=='|')
  46. {
  47.  
  48. }
  49. else
  50. {
  51. symbol+=str[i];
  52. }
  53.  
  54. if(flag_alpha==1)
  55. {
  56. if(child.size()!=0)
  57. alpha.push_back(child);
  58.  
  59. if(symbol.size()!=0)
  60. alpha.push_back(symbol);
  61. }
  62. else
  63. {
  64. if(child.size()!=0)
  65. beta.push_back(child);
  66.  
  67. if(symbol.size()!=0)
  68. beta.push_back(symbol);
  69. }
  70.  
  71. if(str[i]=='|')
  72. {
  73. flag_alpha=0;
  74. }
  75.  
  76. child.clear();
  77. symbol.clear();
  78. }
  79. else
  80. {
  81. child+=str[i];
  82. }
  83.  
  84. }
  85. }
  86. if(child.size()!=0)
  87. {
  88. if(flag_alpha==1)
  89. {
  90. if(child.size()!=0)
  91. alpha.push_back(child);
  92. }
  93. else
  94. {
  95. if(child.size()!=0)
  96. beta.push_back(child);
  97. }
  98.  
  99. }
  100. child.clear();
  101.  
  102.  
  103. cout<<"parent = "<<parent<<endl;
  104. cout<<"alpha= ";
  105. for(int i=0; i<alpha.size() ; i++)
  106. {
  107. if(alpha[i]!=parent)
  108. cout<<alpha[i]<<" ";
  109.  
  110. }
  111. cout<<endl;
  112.  
  113. cout<<"Beta= ";
  114. for(int i=0; i<beta.size() ; i++)
  115. cout<<beta[i]<<" ";
  116.  
  117. cout<<endl;
  118.  
  119. if(alpha[alpha.size()-1]!=parent)
  120. {
  121. cout<<"Left recurtion detected"<<endl;
  122.  
  123. cout<<"solution"<<endl;
  124.  
  125. cout<<parent<<"->";
  126. for(int i=0; i<beta.size() ; i++)
  127. cout<<beta[i];
  128.  
  129. cout<<parent<<"'"<<endl;
  130.  
  131. cout<<parent<<"'->";
  132.  
  133. for(int i=0; i<alpha.size() ; i++)
  134. {
  135. if(alpha[i]!=parent)
  136. cout<<alpha[i];
  137.  
  138. }
  139. cout<<parent<<"' | epsilon"<<endl;
  140.  
  141.  
  142. }
  143. else
  144. cout<<"No left recursion"<<endl;
  145.  
  146. alpha.clear();
  147. beta.clear();
  148. parent.clear();
  149.  
  150. cout<<"\n\n\n\n"<<endl;
  151. }
  152. return 0;
  153. }
Advertisement
Add Comment
Please, Sign In to add comment