diff --git a/version2.0/abstract_syntax_tree.h b/version2.0/abstract_syntax_tree.h index aef6696..0ed3dea 100644 --- a/version2.0/abstract_syntax_tree.h +++ b/version2.0/abstract_syntax_tree.h @@ -6,6 +6,7 @@ class abstract_syntax_tree private: // basic elements int line; + int symbol_number; int node_type; std::list children; @@ -30,6 +31,7 @@ class abstract_syntax_tree void set_clear(); void set_node_type(const int); void set_node_line(const int); + void set_symbol_number(const int); void set_var_string(std::string); void set_var_number(std::string); void set_var_name(std::string); @@ -38,6 +40,7 @@ class abstract_syntax_tree // get int get_node_type(); int get_node_line(); + int get_symbol_number(); double get_var_number(); std::string get_var_string(); std::string get_var_name(); @@ -48,6 +51,7 @@ abstract_syntax_tree::abstract_syntax_tree() { node_type=__null_type; line=0; + symbol_number=-1; var_number=0; var_string=""; var_name=""; @@ -59,6 +63,7 @@ abstract_syntax_tree::abstract_syntax_tree(const abstract_syntax_tree& tmp) { node_type=tmp.node_type; line=tmp.line; + symbol_number=tmp.symbol_number; var_number=tmp.var_number; var_string=tmp.var_string; var_name=tmp.var_name; @@ -76,6 +81,7 @@ abstract_syntax_tree& abstract_syntax_tree::operator=(const abstract_syntax_tree { node_type=tmp.node_type; line=tmp.line; + symbol_number=tmp.symbol_number; var_number=tmp.var_number; var_string=tmp.var_string; var_name=tmp.var_name; @@ -114,6 +120,7 @@ void abstract_syntax_tree::set_clear() { node_type=__null_type; line=0; + symbol_number=-1; var_number=0; var_string=""; var_name=""; @@ -139,6 +146,12 @@ void abstract_syntax_tree::set_node_line(const int __line) return; } +void abstract_syntax_tree::set_symbol_number(const int __sym_num) +{ + symbol_number=__sym_num; + return; +} + void abstract_syntax_tree::set_var_string(std::string __str) { var_string=__str; @@ -176,6 +189,11 @@ int abstract_syntax_tree::get_node_line() return line; } +int abstract_syntax_tree::get_symbol_number() +{ + return symbol_number; +} + double abstract_syntax_tree::get_var_number() { return var_number; diff --git a/version2.0/nasal_parse.h b/version2.0/nasal_parse.h index 73d62de..20f258f 100644 --- a/version2.0/nasal_parse.h +++ b/version2.0/nasal_parse.h @@ -9,8 +9,60 @@ class nasal_parse token this_token; int error; abstract_syntax_tree root; + + // most important function of parser + // these two functions are used to get and push token + // by using them,parser can generate ast + void get_token(); + void push_token(); + + // block statements generation + abstract_syntax_tree block_generate(); + + // check ';' + void check_semi(); + + // check '(' confliction + bool check_multi_assignment();// check multi_call_id '=' multi_scalar + bool check_multi_scalar(); // check multi_scalar + bool check_var_in_curve(); // check multi_definition: (var id,id,id) + bool check_function_end(abstract_syntax_tree&); // check end of definition or '=' is a function + + /* + calculation() will get elements generated by and_calculation() + and_calculation() will get elements generated by or_calculation() + or_calculation() will get elements generated by cmp_calculation() + cmp_calculation() will get elements generated by additive_calculation() + additive_calculation() will get elements generated by multive_calculation() + multive_calculation() will get elements generated by assign_calculation()(assignment '=' ) and scalar_generate() + assign_calculation() get elements from scalar_generate() + please notice that: + if the elements begin with '!' or '-',multive_calculation() gets them from scalar_generate() + if not,multive_calculation() gets them from assign_calculation() + because '!' and '-' cannot be used with assignment together such as: -a=1 + */ + abstract_syntax_tree calculation(); + abstract_syntax_tree and_calculation(); + abstract_syntax_tree or_calculation(); + abstract_syntax_tree cmp_calculation(); + abstract_syntax_tree additive_calculation(); + abstract_syntax_tree multive_calculation(); + abstract_syntax_tree assign_calculation(); + abstract_syntax_tree scalar_generate(); + + // normal data type generation + abstract_syntax_tree hash_generate(); + abstract_syntax_tree vector_generate(); + abstract_syntax_tree function_generate(); + + // return_expr() generates ebnf: [] ';' + abstract_syntax_tree return_expr(); + abstract_syntax_tree multi_scalar_assignment(); + abstract_syntax_tree definition(); + abstract_syntax_tree loop_expr(); + abstract_syntax_tree conditional_expr(); public: - // basic + // basic functions void delete_all_elements() { // used in 'del' command @@ -23,54 +75,12 @@ class nasal_parse } void print_detail_token(); void get_token_list(std::list&); - void get_token(); - void push_token(); int get_error(); abstract_syntax_tree& get_root(); - void check_semi(); - - // check '(' confliction - bool check_multi_assignment();// check multi_call_id '=' multi_scalar - bool check_multi_scalar(); // check multi_scalar - bool check_var_in_curve(); // check multi_definition: (var id,id,id) - bool check_function_end(abstract_syntax_tree&); // check end of definition or '=' is a function - + // abstract_syntax_tree generation - // block statements generation + // main process of parser void main_generate(); - abstract_syntax_tree block_generate(); -/* - calculation() will get elements generated by and_calculation() - and_calculation() will get elements generated by or_calculation() - or_calculation() will get elements generated by cmp_calculation() - cmp_calculation() will get elements generated by additive_calculation() - additive_calculation() will get elements generated by multive_calculation() - multive_calculation() will get elements generated by assign_calculation()(assignment '=' ) and scalar_generate() - assign_calculation() get elements from scalar_generate() - please notice that: - if the elements begin with '!' or '-',multive_calculation() gets them from scalar_generate() - if not,multive_calculation() gets them from assign_calculation() - because '!' and '-' cannot be used with assignment together such as: -a=1 -*/ - abstract_syntax_tree calculation(); - abstract_syntax_tree and_calculation(); - abstract_syntax_tree or_calculation(); - abstract_syntax_tree cmp_calculation(); - abstract_syntax_tree additive_calculation(); - abstract_syntax_tree multive_calculation(); - abstract_syntax_tree assign_calculation(); - abstract_syntax_tree scalar_generate(); - - abstract_syntax_tree hash_generate(); - abstract_syntax_tree vector_generate(); - abstract_syntax_tree function_generate(); - - // return_expr() generates ebnf: [] ';' - abstract_syntax_tree return_expr(); - abstract_syntax_tree multi_scalar_assignment(); - abstract_syntax_tree definition(); - abstract_syntax_tree loop_expr(); - abstract_syntax_tree conditional_expr(); }; void nasal_parse::print_detail_token() diff --git a/version2.0/nasal_symbol_table.h b/version2.0/nasal_symbol_table.h index 93ae9d5..0533878 100644 --- a/version2.0/nasal_symbol_table.h +++ b/version2.0/nasal_symbol_table.h @@ -33,21 +33,21 @@ class nasal_symbol_table { private: int error; - std::list global_symbol_dictionary; - std::list global_scope; - std::list local_symbol_dictionary; - std::list > block_list; + std::list global_symbol_dictionary;// used to save symbols appeared in global scope + std::list local_symbol_dictionary; // used to save symbols appeared in local scopes + std::list global_scope; // global scope + std::list > local_list; // local scopes void symbol_table_block_generate(abstract_syntax_tree&); + void global_scope_add_symbol(symbol_table_unit); + void local_list_add_scope(); + void local_list_del_scope(); + void local_scope_add_symbol(symbol_table_unit); + int search_symbol_id(symbol_table_unit); public: nasal_symbol_table(); ~nasal_symbol_table(); void set_scope_clear(); void print_symbol_table(); - void global_scope_add_symbol(symbol_table_unit); - void block_list_add_scope(); - void block_list_del_scope(); - void block_scope_add_symbol(symbol_table_unit); - int search_symbol_id(symbol_table_unit); int get_error(); void symbol_table_main_generate(abstract_syntax_tree&); }; @@ -58,7 +58,7 @@ nasal_symbol_table::nasal_symbol_table() global_symbol_dictionary.clear(); global_scope.clear(); local_symbol_dictionary.clear(); - block_list.clear(); + local_list.clear(); return; } @@ -68,7 +68,7 @@ nasal_symbol_table::~nasal_symbol_table() global_symbol_dictionary.clear(); global_scope.clear(); local_symbol_dictionary.clear(); - block_list.clear(); + local_list.clear(); return; } @@ -78,7 +78,7 @@ void nasal_symbol_table::set_scope_clear() global_symbol_dictionary.clear(); global_scope.clear(); local_symbol_dictionary.clear(); - block_list.clear(); + local_list.clear(); return; } @@ -109,6 +109,7 @@ void nasal_symbol_table::global_scope_add_symbol(symbol_table_unit tmp) for(std::list::iterator i=global_scope.begin();i!=global_scope.end();++i) { // this conditional expression is used to avoid wrong use of this function + // repeat symbol will get the same number if(i->symbol_name==tmp.symbol_name) return; symbol_number_gen=i->symbol_number; @@ -119,39 +120,50 @@ void nasal_symbol_table::global_scope_add_symbol(symbol_table_unit tmp) return; } -void nasal_symbol_table::block_list_add_scope() +void nasal_symbol_table::local_list_add_scope() { std::list new_scope; - block_list.push_back(new_scope); + local_list.push_back(new_scope); return; } -void nasal_symbol_table::block_list_del_scope() +void nasal_symbol_table::local_list_del_scope() { - if(block_list.empty()) + if(local_list.empty()) { ++error; std::cout<<">>[Symbol-error] fatal error: empty block_scope_list."<::iterator i=block_list.back().begin();i!=block_list.back().end();++i) - { - // this conditional expression is used to avoid wrong use of this function - if(i->symbol_name==tmp.symbol_name) - return; - symbol_number_gen=i->symbol_number; - } + // get last symbol number + for(std::list >::iterator i= local_list.begin();i!=local_list.end();++i) + if(!i->empty()) + for(std::list::iterator j=i->begin();j!=i->end();++j) + { + // this conditional expression is used to avoid wrong use of this function + // repeat symbol will get the same number + ++i; + if(i==local_list.end()) + { + --i; + if(j->symbol_name==tmp.symbol_name) + return; + } + else + --i; + symbol_number_gen=j->symbol_number; + } tmp.symbol_number=symbol_number_gen+1; - block_list.back().push_back(tmp); + local_list.back().push_back(tmp); local_symbol_dictionary.push_back(tmp); } else @@ -167,8 +179,8 @@ int nasal_symbol_table::search_symbol_id(symbol_table_unit tmp) // in both global and local scope // symbol_number begins with 1 int ret_number=-1; - if(!block_list.empty()) - for(std::list >::iterator i=block_list.begin();i!=block_list.end();++i) + if(!local_list.empty()) + for(std::list >::iterator i=local_list.begin();i!=local_list.end();++i) for(std::list::iterator j=i->begin();j!=i->end();++j) if(j->symbol_name==tmp.symbol_name) ret_number=j->symbol_number; @@ -200,6 +212,7 @@ void nasal_symbol_table::symbol_table_main_generate(abstract_syntax_tree& root) tmp.symbol_line=i->get_children().front().get_node_line(); tmp.symbol_name=i->get_children().front().get_var_name(); this->global_scope_add_symbol(tmp); + i->get_children().front().set_symbol_number(this->search_symbol_id(tmp)); this->symbol_table_block_generate(i->get_children().back()); } else @@ -211,6 +224,34 @@ void nasal_symbol_table::symbol_table_main_generate(abstract_syntax_tree& root) void nasal_symbol_table::symbol_table_block_generate(abstract_syntax_tree& node) { + if(node.get_node_type()==__function) + { + this->local_list_add_scope(); + + this->local_list_del_scope(); + } + else if(node.get_node_type()==__conditional) + { + + } + else if(node.get_node_type()==__normal_statement_block) + { + + } + else if(node.get_node_type()==__id) + { + symbol_table_unit tmp; + tmp.symbol_line=node.get_node_line(); + tmp.symbol_name=node.get_var_name(); + int id_symbol_number=this->search_symbol_id(tmp); + node.set_symbol_number(id_symbol_number); + } + else + { + if(!node.get_children().empty()) + for(std::list::iterator i=node.get_children().begin();i!=node.get_children().end();++i) + this->symbol_table_block_generate(*i); + } return; }