Advertisement
Eather

Code for Nonrecursive predictive parsing

May 10th, 2011
942
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.42 KB | None | 0 0
  1.                            /*in the name of Allah */
  2. # include <list>
  3. # include <deque>
  4. # include <bitset>
  5. # include <algorithm>
  6. # include <functional>
  7. # include <numeric>
  8. # include <utility>
  9. # include <sstream>
  10. # include <iostream>
  11. # include <iomanip>
  12. # include <cstdio>
  13. # include <cmath>
  14. # include <cstdlib>
  15. # include <ctime>
  16. # include <set>
  17. # include <map>
  18. # include <cmath>
  19. # include <queue>
  20. # include <limits>
  21. # include <stack>
  22. # include <vector>
  23. # include <cstring>
  24. # include <cstdio>
  25. # include <fstream>
  26. using namespace std;
  27.  
  28. # define MEM(array,w)   memset(array,w,sizeof array)
  29. # define ULL unsigned long long
  30. # define eps 1e-9
  31. # define SS stringstream
  32. # define FOR(i, a, b) for (int i=a; i<b; i++)
  33. # define REP(i, a) FOR(i, 0, a)
  34. # define rive(s) reverse(s.begin(),s.end())
  35. # define PII pair<int , int>
  36. # define MPSS map<string, string>
  37. # define MPIS map<int, string>
  38. # define MPSI map<string, int>
  39. # define MPII map<int, int>
  40. # define MPIC map<int,char>
  41. # define MPCI map<char, int>
  42. # define all(c) (c).begin(), (c).end()
  43. # define VS vector<string>
  44. # define VI vector<int>
  45. # define VC vector<char>
  46. # define VB vector<bool>
  47. # define sz(x) x.size()
  48. # define pb push_back
  49. # define STI set<int>
  50. # define STC set<char>
  51. # define STS set<string>
  52. # define OK(R,C) if(i<0 && j<0 && j==C && i==R)
  53. # define MP make_pair
  54.  
  55. typedef pair<string,string> PCC;
  56. string hasi=":-)";
  57. string sad=":-(";
  58. int main()
  59. {
  60.     int test,Ctest=0;
  61.     int non, ter;
  62.  
  63.     cout<<"how many non terminal input? ";
  64.     cin>>non;
  65.  
  66.     cout<<"Give the input:\n";
  67.     string nn;
  68.     VS VN;
  69.     REP(i,non)
  70.     {
  71.         cin>>nn;
  72.         VN.pb(nn);
  73.     }
  74.  
  75.     cout<<"how many are terminal input? ";
  76.     cin>>ter;
  77.  
  78.     cout<<"Give the input:\n";
  79.     string mm;
  80.     VS VT;
  81.     REP(i,ter)
  82.     {
  83.         cin>>mm;
  84.         VT.pb(mm);
  85.     }
  86.  
  87.     map<PCC,string> mpc;
  88.     map<PCC,string>::iterator it;
  89.  
  90.     cout<<"Make the grammer table below."<<endl;
  91.     getchar();
  92.     for(int i=0;i<non;i++)
  93.     {
  94.         for(int j=0;j<ter;j++)
  95.         {
  96.             string c;
  97.             cout<<"Grammer for non-terminal input ("<<VN[i]<<") and terminal input ("<<VT[j]<<") is: ";
  98.             getline(cin,c);
  99.             mpc[MP(VN[i],VT[j])]=c;
  100.         }
  101.     }
  102.  
  103.     cout<<"Give the input string\n";
  104.     string st;
  105.     getline(cin,st);
  106.  
  107.     vector<string>input;
  108.  
  109.     stringstream io(st);
  110.     string tt;
  111.     while(io>>tt)input.pb(tt);
  112.  
  113.     int ssz=sz(st)+1;
  114.  
  115.     input.pb("$");
  116.  
  117.     string stk;
  118.     stk+=(VN[0]);
  119.     stk.pb('$');
  120.  
  121.     int i=0;
  122.  
  123.     while(!stk.empty())
  124.     {
  125.         string x;
  126.         if(stk[1]!='\'')
  127.         x=stk.substr(0,1);
  128.         else x=stk.substr(0,2);
  129.  
  130.         string a=input[i];
  131.         string sstk=stk.substr(stk.size()-1);
  132.         sstk+=stk.substr(0,stk.size()-1);
  133.         cout<<"Stack= "<<sstk<<"; ";
  134.  
  135.         int l=0;
  136.         if(isalpha(x[l]) && islower(x[l]))
  137.         {
  138.             while(isalpha(x[l])&&islower(x[l]))l++;
  139.             x=stk.substr(0,l+1);
  140.         }
  141.  
  142.         for(int h=0;h<10-stk.size();h++)cout<<" ";
  143.         cout<<"Input= ";
  144.         int ssz2=0;
  145.         for(int j=i;j<input.size();j++)
  146.         {
  147.             cout<<input[j];
  148.             if(j<input.size()-1)cout<<" ";
  149.             ssz2+=input[j].size();
  150.         }
  151.         cout<<";";
  152.  
  153.         if(find(all(VT),x)!=VT.end() || x=="$")
  154.         {
  155.             if(x==a)
  156.             {
  157.                 stk.erase(0,x.size());
  158.                 i++;
  159.             }
  160.             else {cout<<"Not Accepted\n";return 0;}
  161.         }
  162.         else
  163.         {
  164.             bool paici=0;
  165.             for(it =mpc.begin(); it != mpc.end(); it++)
  166.             {
  167.                 string p=(*it).first.first;
  168.                 string q=(*it).first.second;
  169.                 string item=(*it).second;
  170.                 if(p==x && q==a)
  171.                 {
  172.                     for(int lk=0;lk<(ssz-ssz2);lk++)cout<<" ";
  173.                     cout<<"output: "<<p<<"->"<<item;
  174.                     stk.erase(0,x.size());
  175.  
  176.                     if(item!="e")
  177.                     stk.insert(0,item);
  178.  
  179.                     paici=1;
  180.                 }
  181.             }
  182.             if(paici==0){cout<<endl;REP(m,15)cout<<"~";cout<<sad;cout<<" Not Accepted ";cout<<sad;REP(m,15)cout<<"~";return 0;}
  183.         }
  184.         cout<<endl;
  185.     }
  186.     cout<<endl;REP(m,15)cout<<"~";cout<<hasi;cout<<" Accepted ";cout<<hasi;REP(m,15)cout<<"~";
  187.     cout<<endl;
  188.  
  189.     return 0;
  190. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement