diff --git a/version0.7/pda_demo.cpp b/version0.7/pda_demo.cpp new file mode 100644 index 0000000..8693229 --- /dev/null +++ b/version0.7/pda_demo.cpp @@ -0,0 +1,256 @@ +#include +#include +#include +#include + +enum token_type +{ + e=1,E,_E,T,_T,F,I,__add,__sub,__mul,__div,__left_curve,__right_curve,sharp +}; + +const int max_len=3; +struct cmp_seq +{ + int tokens[max_len]; + int res; +}; +cmp_seq par[]= +{ + {{T,_E}, E}, + {{__add,T,_E}, _E}, + {{__sub,T,_E}, _E}, + {{e}, _E}, + {{F,_T}, T}, + {{__mul,F,_T}, _T}, + {{__div,F,_T}, _T}, + {{e}, _T}, + {{__left_curve,E,__right_curve},F}, + {{I}, F} +}; +int num_of_par=sizeof(par)/sizeof(cmp_seq); + +cmp_seq first_set[]= +{ + {{__left_curve,I},E}, + {{__left_curve,I},T}, + {{__left_curve,I},F}, + {{__add,__sub,e},_E}, + {{__mul,__div,e},_T}, +}; +int num_of_set=sizeof(first_set)/sizeof(cmp_seq); + +bool isEnd(int type) +{ + return ((type==__left_curve) || (type==__right_curve) || (type==I) || (type==__add) || (type==__sub) || (type==__mul) || (type==__div) || (type==e) || (type==sharp)); +} +bool isHead(int head,int type) +{ + for(int i=0;i main_stack; + std::stack comp_stack; + public: + PDA(std::string& text) + { + main_stack.push(sharp); + for(int i=(int)text.length()-1;i>=0;--i) + { + if(text[i]=='(') + main_stack.push(__left_curve); + else if(text[i]==')') + main_stack.push(__right_curve); + else if(text[i]=='i') + main_stack.push(I); + else if(text[i]=='+') + main_stack.push(__add); + else if(text[i]=='-') + main_stack.push(__sub); + else if(text[i]=='*') + main_stack.push(__mul); + else if(text[i]=='/') + main_stack.push(__div); + } + comp_stack.push(sharp); + comp_stack.push(E); + } + void print_stack(std::stack& temp) + { + std::stack t; + while(!temp.empty()) + { + t.push(temp.top()); + print_token(t.top()); + temp.pop(); + } + while(!t.empty()) + { + temp.push(t.top()); + t.pop(); + } + return; + } + void print_main_and_comp() + { + print_stack(main_stack); + std::cout<<" "; + print_stack(comp_stack); + std::cout<=0;--j) + if(par[i].tokens[j]) + comp_stack.push(par[i].tokens[j]); + } + else if(!isHead(main_stack.top(),comp_stack.top())) + { + //to check if this can be extended as 'e' + int i=place_operator_end(comp_stack.top()); + if(i!=-1) + comp_stack.pop(); + else + { + std::cout<<"error."<>text; + PDA m(text); + m.work(); + } + return 0; +}