语义分析实验报告(共7页).doc
精选优质文档-倾情为你奉上 实验三 语法分析 科3 李君林一实验目的:通过使用、剖析和扩充TINY语言的语义分析程序,掌握编译器的语义分析程序的构造方法。二实验内容(一)运行TINY的语义分析程序(二)扩充TINY的语法分析程序提示:考虑作用域(如:函数)和数组时可能需要修改符号表。三实验步骤1.先读懂TINY语义程序(相关联的文件:MAIN.C ANALYZE.C ANALYZE.H)(1)buildSymtab(syntaxTree); /根据语法树建立符号表通过递归调用 traverse(syntaxTree,insertNode,nullProc);进行static void insertNode( TreeNode * t),这样将遇到与ID有关的Node信息通过void st_insert( char * name, int lineno, int loc,int len )加入到hashTableh数据结构中。(2)接着调用typeCheck(syntaxTree);进行类型检测通过递归调用 traverse(syntaxTree,nullProc,checkNode);将语法树遍历,然后调用static void checkNode(TreeNode * t)对节点进行类型检测2.扩充TINY的语法分析程序本次实验我首先将源程序实现的功能改成符合C_MINUS的符号表与类型检测然后加入没申明调用与数组调用错误即数组没申明而调用数组类型。四实验结果1.正确的测试程序/*/int gcd (int u,int v)if(v=0) return u;else return gcd(v,u);void main(void)int x;int y;read x;x=y=2;while(x>0) y=y-1;write y;return (gcd(x,y);/*/运行结果:经检验测试程序代码无语义错误2.错误测试程序/*/int gcd (int u,int v)if(v=0) return u;else return gcd(v,u);void main(void)int x;int y;read x;t=1;x=y=2;x2=2;while(x>0) y=y-1;write y;return (gcd(x,y);/*/实验结果:检测到13行 t没有申明检测到15行 x不是一个数组五实验心得通过本次实验学会了使用、剖析和扩充TINY语言的语义分析程序,掌握编译器的语义分析程序的构造方法。加深了对书本语义分析的理解,感受到学以致用的快感,增强对本课程的兴趣。实验中遇到的最大问题:如何查询符号表判断数组,后面在其数据结构中增加了一个属性Len,如果不是数组将其赋为-1.六关键程序代码(ANALYZE.C)/*/* File: analyze.c */* Semantic analyzer implementation */* for the TINY compiler */* Compiler Construction: Principles and Practice */* Kenneth C. Louden */*/#include "globals.h"#include "symtab.h"#include "analyze.h"/* counter for variable memory locations */static int location = 0;/* Procedure traverse is a generic recursive * syntax tree traversal routine: * it applies preProc in preorder and postProc * in postorder to tree pointed to by t */static void traverse( TreeNode * t, void (* preProc) (TreeNode *), void (* postProc) (TreeNode *) ) if (t != NULL) preProc(t); int i; for (i=0; i < MAXCHILDREN; i+) traverse(t->childi,preProc,postProc); postProc(t); traverse(t->sibling,preProc,postProc); /* nullProc is a do-nothing procedure to * generate preorder-only or postorder-only * traversals from traverse */static void nullProc(TreeNode * t) if (t=NULL) return; else return;static void typeError(TreeNode * t, char * message) fprintf(listing,"Type error at line %d: %sn",t->lineno,message);Error = TRUE;static void unDecError(TreeNode * t) fprintf(listing,"Type error at line %d: the %s doesn't declarationn",t->lineno,t->attr.name);Error = TRUE;static void notArrayError(TreeNode * t) fprintf(listing,"Type error at line %d: the ID %s isn't a Arrayn",t->lineno,t->attr.name);Error = TRUE;/* Procedure insertNode inserts * identifiers stored in t into * the symbol table */static void insertNode( TreeNode * t) switch (t->nodekind) case StmtK: switch (t->kind.stmt) default: break; break; case ExpK: switch (t->kind.exp) case IdK: if (st_lookup(t->attr.name) = -1) /* not yet in table, so treat as new definition */ unDecError(t); /st_insert(t->attr.name,t->lineno,location+,0); else /* already in table, so ignore location, add line number of use only */ / printf("LEN:%dn",t->length); if(t->length!=-1&&st_isArray(t->attr.name)=-1) notArrayError(t); else st_insert(t->attr.name,t->lineno,0,-1); break; default: break; break;case DecK: switch(t->kind.deck) case VarK: if (st_lookup(t->attr.name) = -1) /* not yet in table, so treat as new definition */ 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 /* already in table, so ignore location, add line number of use only */ st_insert(t->attr.name,t->lineno,0,-1); case ParaK:if (st_lookup(t->attr.name) = -1) /* not yet in table, so treat as new definition */ if(t->length=-1)st_insert(t->attr.name,t->lineno,location+,-1);elsest_insert(t->attr.name,t->lineno,location+,t->length);else /* already in table, so ignore location, add line number of use only */ st_insert(t->attr.name,t->lineno,0,-1); break;case FunK: if (st_lookup(t->attr.name) = -1) /* not yet in table, so treat as new definition */ st_insert(t->attr.name,t->lineno,location+,-1); else /* already in table, so ignore location, add line number of use only */ st_insert(t->attr.name,t->lineno,0,-1); break; default: break; break; default: break; /* Function buildSymtab constructs the symbol * table by preorder traversal of the syntax tree */void buildSymtab(TreeNode * syntaxTree) fprintf(listing,"nunDecError and arrayCallError checkn"); traverse(syntaxTree,insertNode,nullProc); fprintf(listing,"nunDecError and arrayCallError check finishedn"); if (TraceAnalyze) if (TraceAnalyze) fprintf(listing,"nBuilding Symbol Table.n"); printSymTab(listing); /* Procedure checkNode performs * type checking at a single tree node */static void checkNode(TreeNode * t) switch (t->nodekind) case ExpK: switch (t->kind.exp) case OpK: if (t->child0->type != Integer) | (t->child1->type != Integer) typeError(t,"Op applied to non-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; else t->type = Integer; break; case ConstK: case IdK: t->type = Integer; break; default: break; break; case StmtK: switch (t->kind.stmt) case SelK: if (t->child0->type = Integer) typeError(t->child0,"if test is not Boolean"); break; case IteK: if (t->child0->type = Integer) typeError(t->child0,"while test is not Boolean"); break;case WriteK:if (t->child0->type != Integer)typeError(t->child0,"write of non-integer value");break; default: break; break; default: break; /* Procedure typeCheck performs type checking * by a postorder syntax tree traversal */void typeCheck(TreeNode * syntaxTree)traverse(syntaxTree,nullProc,checkNode);专心-专注-专业