Created
February 19, 2024 09:37
-
-
Save nikhil-RGB/1d9b855ea449d6371e68056ee511cc04 to your computer and use it in GitHub Desktop.
Experiment 5
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <vector> | |
#include <string> | |
using namespace std; | |
int main() | |
{ | |
int n; | |
cout<<"\nEnter number of non terminals: "; | |
cin>>n; | |
cout<<"\nEnter non terminals one by one: "; | |
int i; | |
vector<string> nonter(n); | |
vector<int> leftrecr(n,0); | |
for(i=0;i<n;++i) { | |
cout<<"\nNon terminal "<<i+1<<" : "; | |
cin>>nonter[i]; | |
} | |
vector<vector<string> > prod; | |
cout<<"\nEnter '^' for null"; | |
for(i=0;i<n;++i) { | |
cout<<"\nNumber of "<<nonter[i]<<" productions: "; | |
int k; | |
cin>>k; | |
int j; | |
cout<<"\nOne by one enter all "<<nonter[i]<<" productions"; | |
vector<string> temp(k); | |
for(j=0;j<k;++j) { | |
cout<<"\nRHS of production "<<j+1<<": "; | |
string abc; | |
cin>>abc; | |
temp[j]=abc; | |
if(nonter[i].length()<=abc.length()&&nonter[i].compare(abc.substr(0,nonter[i].length()))==0) leftrecr[i]=1; | |
} | |
prod.push_back(temp); | |
} | |
for(i=0;i<n;++i) { | |
cout<<leftrecr[i]; | |
} | |
for(i=0;i<n;++i) { | |
if(leftrecr[i]==0) | |
continue; | |
int j; | |
nonter.push_back(nonter[i]+"'"); | |
vector<string> temp; | |
for(j=0;j<prod[i].size();++j) { | |
if(nonter[i].length()<=prod[i][j].length()&&nonter[i].compare(prod[i][j].substr(0,nonter[i].length ()))==0) { | |
string | |
abc=prod[i][j].substr(nonter[i].length(),prod[i][j].length()-nonter[i].length())+nonter[i]+"'"; temp.push_back(abc); | |
prod[i].erase(prod[i].begin()+j); | |
--j; | |
} | |
else { | |
prod[i][j]+=nonter[i]+"'"; | |
} | |
} | |
temp.push_back("^"); | |
prod.push_back(temp); | |
} | |
cout<<"\n\n"; | |
cout<<"\nNew set of non-terminals: "; | |
for(i=0;i<nonter.size();++i) | |
cout<<nonter[i]<<" "; | |
cout<<"\n\nNew set of productions: "; | |
for(i=0;i<nonter.size();++i) { | |
int j; | |
for(j=0;j<prod[i].size();++j) { | |
cout<<"\n"<<nonter[i]<<" -> "<<prod[i][j]; } | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment