当前位置:文档之家› 华南理工大学《编译原理》实验报告

华南理工大学《编译原理》实验报告

华南理工大学编译原理课程实验报告实验题目:Implement parser, analyzer, code generator for TINY+姓名: 黄炜杰学号: 201230590051班级: 12计创组别: 无合作者: 无指导教师: 刘欣欣The rule type-specifier -> int | bool | char is a typical example of leave node. For these leave nodes, we only need to use the current token to determine which kind it is.TreeNode*type_specifier(void){TreeNode*t=newStmtNode(TypespeK);switch(token){case INT:t->child[0]= newDeclNode(IntK);match(INT);break;case CHAR:t->child[0]=newDeclNode(CharK);match(CHAR);break;case BOOL:t->child[0]=newDeclNode(BoolK);match(BOOL);break;default:syntaxError("unexpected token -> ");printToken(token,tokenString);token=getToken();break;}return t;}The above situations nearly cover all the technique used in parser. But we should note that, this grammar is not a LL(1) grammar, we cannot build a syntax tree of parser tree by this grammar.See the grammer rule: exp -> arithmetic-exp | bool-exp | string-expWe should focus on the arithmetic-exp | bool-exp, see their production rule:Now we have get the type of every node of the tree, it‟s time for error checking. The technique is simple, we just need to compare the type of children and the node itself, for example:case EQ:if(t->child[0]->type!=t->child[1]->type)typeError(t,"Comparision operation = applied to different type");elset->type= Boolean;break;This code check whether the two operands of equal operation is the same.To check whether a variable is defined before used or defined more than once, we only need to search the variable in the hash table to check whether it has exist. This is the search function:int check_id(char*name){int i;for(i=0;i<SIZE;++i){if(hashTable[i]!=NULL){BucketList l=hashTable[i];while(l!=NULL){LineList t=l->lines;if(!strcmp(name,l->name)){return1;}while(t!=NULL){t=t->next;}l=l->next;}}}return0;}The construction of symbol is very easy, we only need to modify the table by adding a “Type” column, and assign the type of each variable at proper position:void printSymTab(FILE*listing){int i;fprintf(listing,"Variable Name Type Location Line Numbers\n");fprintf(listing,"------------- ---- -------- ------------\n");for(i=0;i<SIZE;++i){if(hashTable[i]!=NULL){BucketList l=hashTable[i];while(l!=NULL){LineList t=l->lines;fprintf(listing,"%-14s ",l->name);fprintf(listing,"%-6s",l->type);fprintf(listing,"%-8d ",l->memloc);while(t!=NULL){fprintf(listing,"%4d ",t->lineno);t=t->next;}fprintf(listing,"\n");l=l->next;}}}}cGen(tree->sibling);}}For the generation part of control statement, it is easy to implement:void tacode_if_Else(TreeNode*tree,char*next){strcpy(bool_true,tacode_newLable());strcpy(bool_false,tacode_newLable());printf("%s\nlabel %s\n",tacode_exp(tree->child[0],bool_true,bool_false).tacode, bool_true);cGen(tree->child[1]);printf("goto %s\nlabel %s\n",next,bool_false);cGen(tree->child[2]);printf("label %s\n",next);}We just simply pass it to the genExp part to finish the detailed operation.For expression part, I divide+ - x ÷ id string num and or > < >= <= for two part for convince. Implementation of them are mainly based on function sprint:/* and or > < >= <=*/NodeAttri tacode_exp(TreeNode*tree,char*bool1_true,char*bool1_false){char*op="if %s > %s goto %s\ngoto %s";char*op2="%s\nlabel %s\n%s";if(tree->attr.op== AND ||tree->attr.op==OR){strcpy(bool2_true,tacode_newLable());if(tree->attr.op== AND){att1=tacode_exp(tree->child[0],bool2_true,bool1_false);att2=tacode_exp(tree->child[1],bool1_true,bool1_false);}elseatt1=tacode_exp(tree->child[0],bool1_true,bool2_false);att2=tacode_exp(tree->child[1],bool1_true,bool1_false);}sprintf(att.tacode,op2,att1.tacode,bool2_false,att2.tacode);}else{char*name1=genExp(tree->child[0]).name;char*name2=genExp(tree->child[1]).name;switch(tree->attr.op){case GREATER:case GREATERORLESS:case SMALLORLESS:case EQ:case LT:sprintf(att.tacode,op,name1,name2,bool1_true,bool1_false);break;default:break;}}return att;}Note that bool1_true, bool1_false, bool2_true, bool2_false are used to determine label for multi Boolean expression.【实验过程】(实验步骤、记录、数据、分析)Construct the complier by the rules in …实验方案‟ part, I build the whole complier successfully. It works well under all the test samples.In this test, we use two TINY+ samples:Sample1:int A,B,C,D;while A<C and B>D do附注:实验报告说明1.实验项目名称:要用最简练的语言反映实验的内容。

相关主题