if(ix == -1) //有可能是标识符使用前未定义,也有可能name是一个NUM值转化过来的,我们只考虑后者,把前者默认为不会发生的情况 { //因此,这个编译器暂时不支持对“变量必须先定义后使用”的检查 Name_type_add temp; temp.add = str_to_int(name); temp.name = \ temp.type = \ temp.isfun = false; add_ret = -1; return temp; } else { add_ret = ix; return chain[ix].lookup(name); } }
void Par_table_chain::set_name_type(string name, string type) { chain[cur].set_proc(name,type); }
[2] 中间代码生成器主体
源文件:parser.cpp
#include \#include \#include \#include \#include
using std::domain_error; using std::logic_error; using std::vector; using std::string; using std::cout; using std::endl;
vector
vector
extern Id_table my_id_tab;
Par_table_chain my_par_table_chain; Quads_code my_quads_code;
//////////////////////////////////////
//调用词法分析器,把词法分析的结果放在token容器中 void lexer() { try{ tokens.clear(); while(1) { token temp; temp = nexttoken();
tokens.push_back(temp); } }catch (domain_error e) { } }
int newlabel() { static int my_label = 100; return ++my_label; }
//match()有两种重载类型
void match(int mode, string lexeme) { if((*token_ix).get_token_type() == mode && my_id_tab.get_lexeme((*token_ix).get_ptr_id_table()) == lexeme) { ++token_ix; } else throw logic_error(\}
int match(int mode) { if((*token_ix).get_token_type() == mode) { int temp = (*token_ix).get_ptr_id_table(); ++token_ix; return temp; } else throw logic_error(\}
//语法分析 void parse() { program(); cout << \词法分析结果(记号类型,词素):\ for(vector
void program() { lexer(); //这个函数调用词法分析器做一遍操作,建立二元记号组
token_ix = tokens.begin(); my_par_table_chain.mktable(); my_par_table_chain.set_name_type(\ declaration_list(); }
void declaration_list() { declaration(); if(token_ix != tokens.end()) declaration_list(); else ; }
void declaration() { vector
void var_declaration() { match(INT, \ string id_name = my_id_tab.get_lexeme(match(ID)); match(';'); my_par_table_chain.enter(id_name, \}
void fun_declaration() { string type_spec = type_specifier(); string id_name = my_id_tab.get_lexeme(match(ID)); my_par_table_chain.set_name_type(id_name, type_spec); my_par_table_chain.add_to_previous(); my_quads_code.gen_entry(id_name); match('('); if((*token_ix).get_token_type() != ')') params(); match(')'); compound_stmt(); }
string type_specifier() { try{ match(INT, \ return \ }catch(logic_error e) { match(VOID, \ return \
} }
void params() { param(); if((*token_ix).get_token_type() == ',') { match(','); params(); } }
void param() { match(INT, \ string id_name = my_id_tab.get_lexeme(match(ID)); my_par_table_chain.enter(id_name, \ my_quads_code.gen_param_load(my_par_table_chain, \}
void compound_stmt() { match('{'); local_declarations(); statement_list(); match('}'); }
void local_declarations() { vector
void statement_list() { vector
void statement() { int cur_type = (*token_ix).get_token_type(); switch(cur_type) { case '{':
my_par_table_chain.mktable(); my_par_table_chain.add_to_previous(); compound_stmt(); my_par_table_chain.jumpout(); break; case IF: selection_stmt(); break; case WHILE: iteration_stmt(); break; case RETURN: return_stmt(); break; case ID: case NUM: case '(': expression_stmt(); break; default: throw logic_error(\ } }
void expression_stmt() { if((*token_ix).get_token_type() == ';') match(';'); else { expression(); match(';'); } }
void selection_stmt() { match(IF, \ match('('); int to_true = newlabel(); int to_false = newlabel(); condition_expression(to_true, to_false); match(')'); my_quads_code.gen_label(to_true); statement(); if(my_id_tab.get_lexeme((*token_ix).get_ptr_id_table()) == \ { int to_next = newlabel(); match(ELSE, \ my_quads_code.gen_goto(to_next); my_quads_code.gen_label(to_false); statement(); my_quads_code.gen_label(to_next); } else { my_quads_code.gen_label(to_false); }
百度搜索“77cn”或“免费范文网”即可找到本站免费阅读全部范文。收藏本站方便下次阅读,免费范文网,提供经典小说综合文库中间代码生成(6)在线全文阅读。
相关推荐: