中间代码生成(6)

2019-01-07 12:17

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 tokens;

vector::iterator token_ix;

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::iterator ix = tokens.begin(); ix != tokens.end(); ++ix) cout << \ << (*ix).get_ptr_id_table() << \ cout << endl << endl; cout << \符号表:\ my_id_tab.display(); cout << endl << endl; cout << \中间代码生成结果(三地址码形式):\ my_quads_code.display(); }

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::iterator reserve = token_ix; try{ var_declaration(); }catch(logic_error e) { token_ix = reserve; my_par_table_chain.mktable(); fun_declaration(); my_par_table_chain.jumpout(); } }

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::iterator reserve = token_ix; try{ var_declaration(); local_declarations(); }catch(logic_error e) { token_ix = reserve; } }

void statement_list() { vector::iterator reserve = token_ix; try{ statement(); statement_list(); }catch(logic_error e) { token_ix = reserve; } }

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); }


中间代码生成(6).doc 将本文的Word文档下载到电脑 下载失败或者文档不完整,请联系客服人员解决!

下一篇:品牌管理课程论文

相关阅读
本类排行
× 注册会员免费下载(下载后可以自由复制和排版)

马上注册会员

注:下载文档有可能“只有目录或者内容不全”等情况,请下载之前注意辨别,如果您已付费且无法下载或内容有问题,请联系我们协助你处理。
微信: QQ: