当前位置:文档之家› 语义分析实验报告

语义分析实验报告

实验三语法分析309 科3 李君林一.实验目的:通过使用、剖析和扩充TINY语言的语义分析程序,掌握编译器的语义分析程序的构造方法。

二.实验内容(一)运行TINY的语义分析程序(二)扩充TINY的语法分析程序提示:考虑作用域(如:函数)和数组时可能需要修改符号表。

三.实验步骤1.先读懂TINY语义程序(相关联的文件:)(1)buildSymtab(syntaxTree); 充TINY的语法分析程序本次实验我首先将源程序实现的功能改成符合C_MINUS的符号表与类型检测然后加入没申明调用与数组调用错误即数组没申明而调用数组类型。

四.实验结果1.正确的测试程序/**/int gcd (int u,int v[]){if(v==0)return u;elsereturn 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;elsereturn gcd(v,u);}void main(void){int x;int y;read x;t=1;x=y=2;x[2]=2;while(x>0)y=y-1;write y;return (gcd(x,y));}/**/实验结果:检测到13行 t没有申明检测到15行 x不是一个数组五.实验心得通过本次实验学会了使用、剖析和扩充TINY语言的语义分析程序,掌握编译器的语义分析程序的构造方法。

加深了对书本语义分析的理解,感受到学以致用的快感,增强对本课程的兴趣。

实验中遇到的最大问题:如何查询符号表判断数组,后面在其数据结构中增加了一个属性Len,如果不是数组将其赋为-1.六.关键程序代码()/****************************************************//* File: *//* Semantic analyzer implementation *//* for the TINY compiler *//* Compiler Construction: Principles and Practice *//* Kenneth C. Louden *//****************************************************/#include ""#include ""#include ""/* 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->child[i],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: %s\n",t->lineno,message);Error = TRUE;}static void unDecError(TreeNode * t){ fprintf(listing,"Type error at line %d: the %s doesn't declaration\n",t->lineno,t->;Error = TRUE;}static void notArrayError(TreeNode * t){ fprintf(listing,"Type error at line %d: the ID %s isn't a Array\n",t->lineno,t->; 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->{default:break;}break;case ExpK:switch (t->{ case IdK:if (st_lookup(t-> == -1){/* not yet in table, so treat as new definition */unDecError(t);.\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->{ case OpK:if ((t->child[0]->type != Integer) ||(t->child[1]->type != Integer))typeError(t,"Op applied to non-integer");if ((t-> == EQ) || (t-> == LT) || (t-> == BG)|| (t-> == LE) || (t-> == BG) || (t-> == UNEQ))t->type = Boolean;elset->type = Integer;break;case ConstK:case IdK:t->type = Integer;break;default:break;}break;case StmtK:switch (t->{ case SelK:if (t->child[0]->type == Integer)typeError(t->child[0],"if test is not Boolean");break;case IteK:if (t->child[0]->type == Integer)typeError(t->child[0],"while test is not Boolean");break;case WriteK:if (t->child[0]->type != Integer)typeError(t->child[0],"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); }。

相关主题