Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<stdio.h>
- #include<bits/stdc++.h>
- using namespace std;
- vector<string>alpha;
- vector<string>beta;
- int main()
- {
- freopen("lab_5.txt","r",stdin);
- string str;
- while(getline(cin,str))
- {
- int flag=0;
- string parent,child;
- cout<<str<<endl;
- int flag_alpha=1;
- for(int i=0; i<str.size(); i++)
- {
- if(str[i]=='-')
- {
- flag=1;
- i++;
- continue;
- }
- if(flag==0)
- {
- if(str[i]!=' ')
- parent+=str[i];
- }
- else if(flag==1)
- {
- if(!((str[i]>='a' && str[i]<='z') || (str[i]>='A' && str[i]<='Z')))
- {
- string symbol;
- if(str[i]=='|')
- {
- }
- else
- {
- symbol+=str[i];
- }
- if(flag_alpha==1)
- {
- if(child.size()!=0)
- alpha.push_back(child);
- if(symbol.size()!=0)
- alpha.push_back(symbol);
- }
- else
- {
- if(child.size()!=0)
- beta.push_back(child);
- if(symbol.size()!=0)
- beta.push_back(symbol);
- }
- if(str[i]=='|')
- {
- flag_alpha=0;
- }
- child.clear();
- symbol.clear();
- }
- else
- {
- child+=str[i];
- }
- }
- }
- if(child.size()!=0)
- {
- if(flag_alpha==1)
- {
- if(child.size()!=0)
- alpha.push_back(child);
- }
- else
- {
- if(child.size()!=0)
- beta.push_back(child);
- }
- }
- child.clear();
- cout<<"parent = "<<parent<<endl;
- cout<<"alpha= ";
- for(int i=0; i<alpha.size() ; i++)
- {
- if(alpha[i]!=parent)
- cout<<alpha[i]<<" ";
- }
- cout<<endl;
- cout<<"Beta= ";
- for(int i=0; i<beta.size() ; i++)
- cout<<beta[i]<<" ";
- cout<<endl;
- if(alpha[alpha.size()-1]!=parent)
- {
- cout<<"Left recurtion detected"<<endl;
- cout<<"solution"<<endl;
- cout<<parent<<"->";
- for(int i=0; i<beta.size() ; i++)
- cout<<beta[i];
- cout<<parent<<"'"<<endl;
- cout<<parent<<"'->";
- for(int i=0; i<alpha.size() ; i++)
- {
- if(alpha[i]!=parent)
- cout<<alpha[i];
- }
- cout<<parent<<"' | epsilon"<<endl;
- }
- else
- cout<<"No left recursion"<<endl;
- alpha.clear();
- beta.clear();
- parent.clear();
- cout<<"\n\n\n\n"<<endl;
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment