diff --git a/version0.7/n.cpp b/version0.7/n.cpp index c6fa10c..13a9cdc 100644 --- a/version0.7/n.cpp +++ b/version0.7/n.cpp @@ -4,6 +4,7 @@ enum token_type { __stack_end, + __null_end,// $ __equal,// = __cmp_equal,// == __cmp_not_equal,// != @@ -21,8 +22,11 @@ enum token_type __dot,// . __var,// var reserve word __func,// func reserve word - __unknown_type_id,__identifier,__identifiers, - __scalar,__scalars,__list,__hash, + __id,__identifier,__identifiers,__identifier_end, + __parameter,__parameters,__parameter_end, + __number,__string, + __scalar,__scalars,__scalar_end, + __list,__hash, __hash_member,__hash_members, __statement,__statements, __function,//function(){} @@ -32,11 +36,12 @@ enum token_type __return }; -struct token_seq +const int max_token_len=20; +struct cmp_seq { - int tokens[15]; + int tokens[max_token_len]; int res; -}par[13]= +}par[]= { {{__var,__identifier,__equal,__scalar,__semi}, __definition}, {{__var,__identifier,__equal,__identifier,__semi}, __definition}, @@ -52,42 +57,7 @@ struct token_seq {{__identifier,__left_curve,__right_curve}, __identifier}, {{__identifier,__left_curve,__scalar,__right_curve}, __identifier} }; - -struct par_info -{ - int res; - int len; -}; -par_info isPar(int *t) -{ - par_info temp; - temp.len=0; - temp.res=0; - for(int i=0;i<13;++i) - { - int cnt=0; - for(int j=0;j<15;++j) - { - if(par[i].tokens[j]) - ++cnt; - else - break; - } - for(int j=0;j main_stack; + std::stack comp_stack; + std::stack recognized_stack; + std::stack error_stack; + public: + void stack_input(std::stack& temp) + { + while(!temp.empty()) + { + main_stack.push(temp.top()); + temp.pop(); + } + return; + } + void turn_back_stack_member(const int num) + { + for(int i=0;i=0;--j) + { + if(par[i].tokens[j]) + { + temp.type=par[i].tokens[j]; + comp_stack.push(temp); + } + } + int cnt=0; + while((!comp_stack.empty()) && (!main_stack.empty())) + { + if(comp_stack.top().type==__null_end) + { + ++cnt; + comp_stack.pop(); + if(comp_stack.empty()) + break; + } + if(comp_stack.top().type==main_stack.top().type) + { + ++cnt; + comp_stack.pop(); + recognized_stack.push(main_stack.top()); + main_stack.pop(); + } + else + { + turn_back_stack_member(cnt); + break; + } + } + if(comp_stack.empty()) + { + ispar=true; + break; + } + } + if(!ispar) + { + error_stack.push(main_stack.top()); + main_stack.pop(); + } + } + if(!error_stack.empty()) + { + std::stack temp; + while(!error_stack.empty()) + { + temp.push(error_stack.top()); + error_stack.pop(); + } + std::cout<<">>[Error] Parse error:"; + int now_line=0; + while(!temp.empty()) + { + if(temp.top().line!=now_line) + { + now_line=temp.top().line; + std::cout<>[Parse] 0 error occurred."<::iterator i=lexer.begin();i!=lexer.end();++i) { parse_unit temp_parse; @@ -420,83 +500,9 @@ class nasal_parser } parser.push(temp_parse);//push this into stack } - while(!parser.empty()) - { - int tbl[15]={0}; - std::stack temp; - for(int i=0;i<15;++i) - { - if(!parser.empty()) - { - temp.push(parser.top()); - parser.pop(); - } - else - break; - } - for(int i=0;i<15;++i) - { - if(!temp.empty()) - { - tbl[i]=temp.top().type; - parser.push(temp.top()); - temp.pop(); - } - else - break; - } - for(int i=0;i<15;++i) - { - if(tbl[i]) - break; - if(!tbl[i] && i==14) - return; - } - par_info tk=isPar(tbl); - if(tk.res) - { - parse_unit temp; - temp.line=parser.top().line; - temp.type=tk.res; - for(int i=0;i>[Parser] Complete scanning."< temp_stack; - while(!parser.empty()) - { - if((parser.top().type!=__stack_end) && (parser.top().type!=__statement) && (parser.top().type!=__statements) && (parser.top().type!=__function) && (parser.top().type!=__definition) && (parser.top().type!=__assignment) && (parser.top().type!=__loop) && (parser.top().type!=__choose)) - temp_stack.push(parser.top()); - parser.pop(); - } - if(!temp_stack.empty()) - { - std::cout<<">>[Error] Parse error."<>[Parser] No error occurred."< main_stack; - std::stack cmp_stack; - std::stack recog_stack; + std::stack comp_stack; + std::stack recognized_stack; + std::stack error_stack; public: - + void stack_input(std::stack& temp) + { + while(!temp.empty()) + { + main_stack.push(temp.top()); + temp.pop(); + } + return; + } + void turn_back_stack_member(const int num) + { + for(int i=0;i=0;--j) + { + if(par[i].tokens[j]) + { + temp.type=par[i].tokens[j]; + comp_stack.push(temp); + } + } + int cnt=0; + while((!comp_stack.empty()) && (!main_stack.empty())) + { + if(comp_stack.top().type==__null_end) + { + ++cnt; + comp_stack.pop(); + if(comp_stack.empty()) + break; + } + if(comp_stack.top().type==main_stack.top().type) + { + ++cnt; + comp_stack.pop(); + recognized_stack.push(main_stack.top()); + main_stack.pop(); + } + else + { + turn_back_stack_member(cnt); + break; + } + } + if(comp_stack.empty()) + { + ispar=true; + break; + } + } + if(!ispar) + { + error_stack.push(main_stack.top()); + main_stack.pop(); + } + } + if(!error_stack.empty()) + { + std::stack temp; + while(!error_stack.empty()) + { + temp.push(error_stack.top()); + error_stack.pop(); + } + std::cout<<">>[Error] Parse error:"; + int now_line=0; + while(!temp.empty()) + { + if(temp.top().line!=now_line) + { + now_line=temp.top().line; + std::cout<>[Parse] 0 error occurred."<