diff --git a/version0.7/n.cpp b/version0.7/n.cpp index 13a9cdc..412d946 100644 --- a/version0.7/n.cpp +++ b/version0.7/n.cpp @@ -22,15 +22,17 @@ enum token_type __dot,// . __var,// var reserve word __func,// func reserve word - __id,__identifier,__identifiers,__identifier_end, + __id,__identifier,__identifiers,__identifier_end,__lacked_identifier, __parameter,__parameters,__parameter_end, __number,__string, __scalar,__scalars,__scalar_end, __list,__hash, - __hash_member,__hash_members, + __hash_member,__hash_members,__hash_member_end,__hash_suffix, __statement,__statements, __function,//function(){} - __definition,__assignment,__calculation, + __definition, + __assignment,__pre_assignment, + __calculation, __loop,__continue,__break,__for,__forindex,__foreach,__while,// for()while() continue; break; __choose,__if,__elsif,__else,// if else if else __return @@ -43,19 +45,10 @@ struct cmp_seq int res; }par[]= { - {{__var,__identifier,__equal,__scalar,__semi}, __definition}, - {{__var,__identifier,__equal,__identifier,__semi}, __definition}, - {{__var,__identifier,__equal,__list,__semi}, __definition}, - {{__var,__identifier,__equal,__hash,__semi}, __definition}, - {{__var,__identifier,__equal,__function}, __definition}, - {{__var,__left_curve,__identifiers,__right_curve,__equal,__identifier,__semi},__definition}, - {{__var,__left_curve,__identifiers,__right_curve,__equal,__list,__semi}, __definition}, - {{__identifier,__dot,__identifier}, __identifier}, - {{__identifier,__left_bracket,__scalar,__right_bracket}, __identifier}, - {{__identifier,__left_bracket,__identifier,__right_bracket}, __identifier}, - {{__identifier,__left_bracket,__calculation,__right_bracket}, __identifier}, - {{__identifier,__left_curve,__right_curve}, __identifier}, - {{__identifier,__left_curve,__scalar,__right_curve}, __identifier} + {{__var,__id,__equal,__string,__semi},__definition}, + {{__number}, __scalar}, + {{__var,__id,__equal,__scalar,__semi},__definition}, + {{__var,__id,__equal,__id,__semi}, __definition} }; int num_of_par=sizeof(par)/sizeof(cmp_seq); @@ -164,7 +157,7 @@ void print_token_type(int type) context="func"; break; case __id: - context="unknown_id"; + context="id"; break; case __identifier: context="identifier"; @@ -250,8 +243,6 @@ class PDA { private: std::stack main_stack; - std::stack comp_stack; - std::stack recognized_stack; std::stack error_stack; public: void stack_input(std::stack& temp) @@ -263,71 +254,8 @@ class PDA } return; } - void turn_back_stack_member(const int num) + void print_error() { - 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; @@ -355,6 +283,127 @@ class PDA std::cout<<">>[Parse] 0 error occurred."< recognized_stack; + std::stack comp_stack; + parse_unit temp; + temp.line=0; + temp.type=0; + for(int i=0;i=0;--j) + { + if(par[i].tokens[j]) + { + temp.type=par[i].tokens[j]; + comp_stack.push(temp); + } + } + while((!comp_stack.empty()) && (!main_stack.empty())) + { + if(comp_stack.top().type==main_stack.top().type) + { + comp_stack.pop(); + recognized_stack.push(main_stack.top()); + main_stack.pop(); + } + else if(comp_stack.top().type==__null_end) + comp_stack.pop(); + else if(comp_stack.top().type!=main_stack.top().type) + { + if(!extend_comp_progress(comp_stack.top().type)) + { + while(!recognized_stack.empty()) + { + main_stack.push(recognized_stack.top()); + recognized_stack.pop(); + } + break; + } + else + comp_stack.pop(); + } + } + if(comp_stack.empty()) + { + while(!recognized_stack.empty()) + recognized_stack.pop(); + return true; + } + } + } + return false; + } + void main_comp_progress() + { + std::stack recognized_stack; + std::stack comp_stack; + parse_unit temp; + temp.line=0; + temp.type=0; + + while(!main_stack.empty()) + { + bool ispar=false; + for(int i=0;i=0;--j) + { + if(par[i].tokens[j]) + { + temp.type=par[i].tokens[j]; + comp_stack.push(temp); + } + } + while((!comp_stack.empty()) && (!main_stack.empty())) + { + if(comp_stack.top().type==main_stack.top().type) + { + comp_stack.pop(); + recognized_stack.push(main_stack.top()); + main_stack.pop(); + } + else if(comp_stack.top().type==__null_end) + comp_stack.pop(); + else if(comp_stack.top().type!=main_stack.top().type) + { + if(!extend_comp_progress(comp_stack.top().type)) + { + while(!recognized_stack.empty()) + { + main_stack.push(recognized_stack.top()); + recognized_stack.pop(); + } + break; + } + else + comp_stack.pop(); + } + } + if(comp_stack.empty()) + { + while(!recognized_stack.empty()) + recognized_stack.pop(); + ispar=true; + break; + } + } + if(!ispar && !main_stack.empty()) + { + error_stack.push(main_stack.top()); + main_stack.pop(); + } + } + print_error(); + return; + } }; class nasal_parser @@ -391,7 +440,7 @@ class nasal_parser } else if((*i).type==IDENTIFIER) { - temp_parse.type=__identifier; + temp_parse.type=__id; } else if(((*i).content=="for") || ((*i).content=="foreach") || ((*i).content=="while") || ((*i).content=="forindex")) { @@ -443,7 +492,10 @@ class nasal_parser } else if(((*i).type==NUMBER) || ((*i).type==STRING)) { - temp_parse.type=__scalar; + if((*i).type==NUMBER) + temp_parse.type=__number; + else + temp_parse.type=__string; } else if(((*i).content=="+") || ((*i).content=="-") || ((*i).content=="*") || ((*i).content=="/") || ((*i).content=="~") || ((*i).content=="!")) { @@ -502,7 +554,7 @@ class nasal_parser } PDA automata; automata.stack_input(parser); - automata.comp_progress(); + automata.main_comp_progress(); return; } }; @@ -515,7 +567,6 @@ int main() std::string str="a.txt"; prog.input_file(str); lex.lexer_process(prog.use_file()); - lex.print_lexer(); pas.parse_quiet_process(lex.return_list()); return 0; } diff --git a/version0.7/pda.h b/version0.7/pda.h index 7519a87..0d8e619 100644 --- a/version0.7/pda.h +++ b/version0.7/pda.h @@ -12,6 +12,7 @@ struct parse_unit enum token_type { __stack_end, + __null_end,// $ __equal,// = __cmp_equal,// == __cmp_not_equal,// != @@ -29,13 +30,17 @@ enum token_type __dot,// . __var,// var reserve word __func,// func reserve word - __unknown_type_id,__identifier,__identifiers, + __id,__identifier,__identifiers,__identifier_end,__lacked_identifier, + __parameter,__parameters,__parameter_end, __number,__string, - __scalar,__scalars,__list,__hash, - __hash_member,__hash_members, + __scalar,__scalars,__scalar_end, + __list,__hash, + __hash_member,__hash_members,__hash_member_end,__hash_suffix, __statement,__statements, __function,//function(){} - __definition,__assignment,__calculation, + __definition, + __assignment,__pre_assignment, + __calculation, __loop,__continue,__break,__for,__forindex,__foreach,__while,// for()while() continue; break; __choose,__if,__elsif,__else,// if else if else __return @@ -68,8 +73,6 @@ class PDA { private: std::stack main_stack; - std::stack comp_stack; - std::stack recognized_stack; std::stack error_stack; public: void stack_input(std::stack& temp) @@ -81,71 +84,8 @@ class PDA } return; } - void turn_back_stack_member(const int num) + void print_error() { - 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; @@ -173,4 +113,125 @@ class PDA std::cout<<">>[Parse] 0 error occurred."< recognized_stack; + std::stack comp_stack; + parse_unit temp; + temp.line=0; + temp.type=0; + for(int i=0;i=0;--j) + { + if(par[i].tokens[j]) + { + temp.type=par[i].tokens[j]; + comp_stack.push(temp); + } + } + while((!comp_stack.empty()) && (!main_stack.empty())) + { + if(comp_stack.top().type==main_stack.top().type) + { + comp_stack.pop(); + recognized_stack.push(main_stack.top()); + main_stack.pop(); + } + else if(comp_stack.top().type==__null_end) + comp_stack.pop(); + else if(comp_stack.top().type!=main_stack.top().type) + { + if(!extend_comp_progress(comp_stack.top().type)) + { + while(!recognized_stack.empty()) + { + main_stack.push(recognized_stack.top()); + recognized_stack.pop(); + } + break; + } + else + comp_stack.pop(); + } + } + if(comp_stack.empty()) + { + while(!recognized_stack.empty()) + recognized_stack.pop(); + return true; + } + } + } + return false; + } + void main_comp_progress() + { + std::stack recognized_stack; + std::stack comp_stack; + parse_unit temp; + temp.line=0; + temp.type=0; + + while(!main_stack.empty()) + { + bool ispar=false; + for(int i=0;i=0;--j) + { + if(par[i].tokens[j]) + { + temp.type=par[i].tokens[j]; + comp_stack.push(temp); + } + } + while((!comp_stack.empty()) && (!main_stack.empty())) + { + if(comp_stack.top().type==main_stack.top().type) + { + comp_stack.pop(); + recognized_stack.push(main_stack.top()); + main_stack.pop(); + } + else if(comp_stack.top().type==__null_end) + comp_stack.pop(); + else if(comp_stack.top().type!=main_stack.top().type) + { + if(!extend_comp_progress(comp_stack.top().type)) + { + while(!recognized_stack.empty()) + { + main_stack.push(recognized_stack.top()); + recognized_stack.pop(); + } + break; + } + else + comp_stack.pop(); + } + } + if(comp_stack.empty()) + { + while(!recognized_stack.empty()) + recognized_stack.pop(); + ispar=true; + break; + } + } + if(!ispar && !main_stack.empty()) + { + error_stack.push(main_stack.top()); + main_stack.pop(); + } + } + print_error(); + return; + } };