您好,欢迎访问三七文档
当前位置:首页 > 机械/制造/汽车 > 汽车理论 > 语义分析实验报告.doc
实验三语法分析20080810309科3李君林一.实验目的:通过使用、剖析和扩充TINY语言的语义分析程序,掌握编译器的语义分析程序的构造方法。二.实验内容(一)运行TINY的语义分析程序(二)扩充TINY的语法分析程序提示:考虑作用域(如:函数)和数组时可能需要修改符号表。三.实验步骤1.先读懂TINY语义程序(相关联的文件:MAIN.CANALYZE.CANALYZE.H)(1)buildSymtab(syntaxTree);//根据语法树建立符号表通过递归调用traverse(syntaxTree,insertNode,nullProc);进行staticvoidinsertNode(TreeNode*t),这样将遇到与ID有关的Node信息通过voidst_insert(char*name,intlineno,intloc,intlen)加入到hashTable[h]数据结构中。(2)接着调用typeCheck(syntaxTree);进行类型检测通过递归调用traverse(syntaxTree,nullProc,checkNode);将语法树遍历,然后调用staticvoidcheckNode(TreeNode*t)对节点进行类型检测2.扩充TINY的语法分析程序本次实验我首先将源程序实现的功能改成符合C_MINUS的符号表与类型检测然后加入没申明调用与数组调用错误即数组没申明而调用数组类型。四.实验结果1.正确的测试程序/**/intgcd(intu,intv[]){if(v==0)returnu;elsereturngcd(v,u);}voidmain(void){intx;inty;readx;x=y=2;while(x0)y=y-1;writey;return(gcd(x,y));}/**/运行结果:经检验测试程序代码无语义错误2.错误测试程序/**/intgcd(intu,intv[]){if(v==0)returnu;elsereturngcd(v,u);}voidmain(void){intx;inty;readx;t=1;x=y=2;x[2]=2;while(x0)y=y-1;writey;return(gcd(x,y));}/**/实验结果:检测到13行t没有申明检测到15行x不是一个数组五.实验心得通过本次实验学会了使用、剖析和扩充TINY语言的语义分析程序,掌握编译器的语义分析程序的构造方法。加深了对书本语义分析的理解,感受到学以致用的快感,增强对本课程的兴趣。实验中遇到的最大问题:如何查询符号表判断数组,后面在其数据结构中增加了一个属性Len,如果不是数组将其赋为-1.六.关键程序代码(ANALYZE.C)/****************************************************//*File:analyze.c*//*Semanticanalyzerimplementation*//*fortheTINYcompiler*//*CompilerConstruction:PrinciplesandPractice*//*KennethC.Louden*//****************************************************/#includeglobals.h#includesymtab.h#includeanalyze.h/*counterforvariablememorylocations*/staticintlocation=0;/*Proceduretraverseisagenericrecursive*syntaxtreetraversalroutine:*itappliespreProcinpreorderandpostProc*inpostordertotreepointedtobyt*/staticvoidtraverse(TreeNode*t,void(*preProc)(TreeNode*),void(*postProc)(TreeNode*)){if(t!=NULL){preProc(t);{inti;for(i=0;iMAXCHILDREN;i++)traverse(t-child[i],preProc,postProc);}postProc(t);traverse(t-sibling,preProc,postProc);}}/*nullProcisado-nothingprocedureto*generatepreorder-onlyorpostorder-only*traversalsfromtraverse*/staticvoidnullProc(TreeNode*t){if(t==NULL)return;elsereturn;}staticvoidtypeError(TreeNode*t,char*message){fprintf(listing,Typeerroratline%d:%s\n,t-lineno,message);Error=TRUE;}staticvoidunDecError(TreeNode*t){fprintf(listing,Typeerroratline%d:the%sdoesn'tdeclaration\n,t-lineno,t-attr.name);Error=TRUE;}staticvoidnotArrayError(TreeNode*t){fprintf(listing,Typeerroratline%d:theID%sisn'taArray\n,t-lineno,t-attr.name);Error=TRUE;}/*ProcedureinsertNodeinserts*identifiersstoredintinto*thesymboltable*/staticvoidinsertNode(TreeNode*t){switch(t-nodekind){caseStmtK:switch(t-kind.stmt){default:break;}break;caseExpK:switch(t-kind.exp){caseIdK:if(st_lookup(t-attr.name)==-1){/*notyetintable,sotreatasnewdefinition*/unDecError(t);//st_insert(t-attr.name,t-lineno,location++,0);}else{/*alreadyintable,soignorelocation,addlinenumberofuseonly*///printf(LEN:%d\n,t-length);if(t-length!=-1&&st_isArray(t-attr.name)==-1)notArrayError(t);elsest_insert(t-attr.name,t-lineno,0,-1);}break;default:break;}break;caseDecK:switch(t-kind.deck){caseVarK:if(st_lookup(t-attr.name)==-1){/*notyetintable,sotreatasnewdefinition*/if(t-length==-1){st_insert(t-attr.name,t-lineno,location++,-1);}else{st_insert(t-attr.name,t-lineno,location++,t-length);}if(t-length!=-1)location+=t-length-1;}else{/*alreadyintable,soignorelocation,addlinenumberofuseonly*/st_insert(t-attr.name,t-lineno,0,-1);}caseParaK:if(st_lookup(t-attr.name)==-1){/*notyetintable,sotreatasnewdefinition*/if(t-length==-1){st_insert(t-attr.name,t-lineno,location++,-1);}else{st_insert(t-attr.name,t-lineno,location++,t-length);}}else/*alreadyintable,soignorelocation,addlinenumberofuseonly*/st_insert(t-attr.name,t-lineno,0,-1);break;caseFunK:if(st_lookup(t-attr.name)==-1)/*notyetintable,sotreatasnewdefinition*/st_insert(t-attr.name,t-lineno,location++,-1);else/*alreadyintable,soignorelocation,addlinenumberofuseonly*/st_insert(t-attr.name,t-lineno,0,-1);break;default:break;}break;default:break;}}/*FunctionbuildSymtabconstructsthesymbol*tablebypreordertraversalofthesyntaxtree*/voidbuildSymtab(TreeNode*syntaxTree){fprintf(listing,\nunDecErrorandarrayCallErrorcheck\n);traverse(syntaxTree,insertNode,nullProc);fprintf(listing,\nunDecErrorandarrayCallErrorcheckfinished\n);if(TraceAnalyze){if(TraceAnalyze)fprintf(listing,\nBuildingSymbolTable...\n);printSymTab(listing);}}/*ProcedurecheckNodeperforms*typecheckingatasingletreenode*/staticvoidcheckNode(TreeNode*t){switch(t-nodekind){caseExpK:switch(t-kind.exp){caseOpK:if((t-child[0]-type!=Integer)||(t-child[1]-type!=Integer))typeError(t,Opappliedtonon-integer);if((t-attr.op==EQ)||(t-attr.op==LT)||(t-attr.op==BG)||(t-attr.op==LE)||(t-attr.op==BG)||(t-attr.op==UNEQ))t-type=Boolean;elset-type=Integer;break;caseConstK:caseIdK:t-type=Integer;break;default:break;}break;caseStmtK:switch(t-kind.stmt){caseSelK:if(t-child[0]-type==Integer)typeError(t-child[0],iftestisnotBoolean);break;caseIteK:if(t-child[0]-type==Integer)typeError(t-child[0],whiletestisnotBoolean);break;caseWriteK:if
本文标题:语义分析实验报告.doc
链接地址:https://www.777doc.com/doc-6618428 .html