Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*in the name of Allah */
- # include <list>
- # include <deque>
- # include <bitset>
- # include <algorithm>
- # include <functional>
- # include <numeric>
- # include <utility>
- # include <sstream>
- # include <iostream>
- # include <iomanip>
- # include <cstdio>
- # include <cmath>
- # include <cstdlib>
- # include <ctime>
- # include <set>
- # include <map>
- # include <cmath>
- # include <queue>
- # include <limits>
- # include <stack>
- # include <vector>
- # include <cstring>
- # include <cstdio>
- # include <fstream>
- using namespace std;
- # define MEM(array,w) memset(array,w,sizeof array)
- # define ULL unsigned long long
- # define eps 1e-9
- # define SS stringstream
- # define FOR(i, a, b) for (int i=a; i<b; i++)
- # define REP(i, a) FOR(i, 0, a)
- # define rive(s) reverse(s.begin(),s.end())
- # define PII pair<int , int>
- # define MPSS map<string, string>
- # define MPIS map<int, string>
- # define MPSI map<string, int>
- # define MPII map<int, int>
- # define MPIC map<int,char>
- # define MPCI map<char, int>
- # define all(c) (c).begin(), (c).end()
- # define VS vector<string>
- # define VI vector<int>
- # define VC vector<char>
- # define VB vector<bool>
- # define sz(x) x.size()
- # define pb push_back
- # define STI set<int>
- # define STC set<char>
- # define STS set<string>
- # define OK(R,C) if(i<0 && j<0 && j==C && i==R)
- # define MP make_pair
- typedef pair<string,string> PCC;
- string hasi=":-)";
- string sad=":-(";
- int main()
- {
- int test,Ctest=0;
- int non, ter;
- cout<<"how many non terminal input? ";
- cin>>non;
- cout<<"Give the input:\n";
- string nn;
- VS VN;
- REP(i,non)
- {
- cin>>nn;
- VN.pb(nn);
- }
- cout<<"how many are terminal input? ";
- cin>>ter;
- cout<<"Give the input:\n";
- string mm;
- VS VT;
- REP(i,ter)
- {
- cin>>mm;
- VT.pb(mm);
- }
- map<PCC,string> mpc;
- map<PCC,string>::iterator it;
- cout<<"Make the grammer table below."<<endl;
- getchar();
- for(int i=0;i<non;i++)
- {
- for(int j=0;j<ter;j++)
- {
- string c;
- cout<<"Grammer for non-terminal input ("<<VN[i]<<") and terminal input ("<<VT[j]<<") is: ";
- getline(cin,c);
- mpc[MP(VN[i],VT[j])]=c;
- }
- }
- cout<<"Give the input string\n";
- string st;
- getline(cin,st);
- vector<string>input;
- stringstream io(st);
- string tt;
- while(io>>tt)input.pb(tt);
- int ssz=sz(st)+1;
- input.pb("$");
- string stk;
- stk+=(VN[0]);
- stk.pb('$');
- int i=0;
- while(!stk.empty())
- {
- string x;
- if(stk[1]!='\'')
- x=stk.substr(0,1);
- else x=stk.substr(0,2);
- string a=input[i];
- string sstk=stk.substr(stk.size()-1);
- sstk+=stk.substr(0,stk.size()-1);
- cout<<"Stack= "<<sstk<<"; ";
- int l=0;
- if(isalpha(x[l]) && islower(x[l]))
- {
- while(isalpha(x[l])&&islower(x[l]))l++;
- x=stk.substr(0,l+1);
- }
- for(int h=0;h<10-stk.size();h++)cout<<" ";
- cout<<"Input= ";
- int ssz2=0;
- for(int j=i;j<input.size();j++)
- {
- cout<<input[j];
- if(j<input.size()-1)cout<<" ";
- ssz2+=input[j].size();
- }
- cout<<";";
- if(find(all(VT),x)!=VT.end() || x=="$")
- {
- if(x==a)
- {
- stk.erase(0,x.size());
- i++;
- }
- else {cout<<"Not Accepted\n";return 0;}
- }
- else
- {
- bool paici=0;
- for(it =mpc.begin(); it != mpc.end(); it++)
- {
- string p=(*it).first.first;
- string q=(*it).first.second;
- string item=(*it).second;
- if(p==x && q==a)
- {
- for(int lk=0;lk<(ssz-ssz2);lk++)cout<<" ";
- cout<<"output: "<<p<<"->"<<item;
- stk.erase(0,x.size());
- if(item!="e")
- stk.insert(0,item);
- paici=1;
- }
- }
- if(paici==0){cout<<endl;REP(m,15)cout<<"~";cout<<sad;cout<<" Not Accepted ";cout<<sad;REP(m,15)cout<<"~";return 0;}
- }
- cout<<endl;
- }
- cout<<endl;REP(m,15)cout<<"~";cout<<hasi;cout<<" Accepted ";cout<<hasi;REP(m,15)cout<<"~";
- cout<<endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement