编译原理实验三语义分析

上传人:pu****.1 文档编号:498686205 上传时间:2022-09-09 格式:DOCX 页数:8 大小:44.89KB
返回 下载 相关 举报
编译原理实验三语义分析_第1页
第1页 / 共8页
编译原理实验三语义分析_第2页
第2页 / 共8页
编译原理实验三语义分析_第3页
第3页 / 共8页
编译原理实验三语义分析_第4页
第4页 / 共8页
编译原理实验三语义分析_第5页
第5页 / 共8页
点击查看更多>>
资源描述

《编译原理实验三语义分析》由会员分享,可在线阅读,更多相关《编译原理实验三语义分析(8页珍藏版)》请在金锄头文库上搜索。

1、编译原理实验三 语法分析并进行语义分析输入:经过词法分析后形成的token和tokenstring 输出:检查有无语法错误,形成语法树,并检查是否符合语义样例程序已经能对变量声明填符号表、进行类型检查。 文法:stmt_seq -statement ; stmt_seq | statement statement-decl_stmt | assign_stmt decl_stmt-type var_list type-int |floatvar_list-id , var_list | id assign_stmt- id := exp exp-exp + term | exp - term

2、|term term- term * factor | term * factor | factor factor-id | num | ( exp )要求掌握理解程序设计方法。样例程序#include#includetypedefenumMINUS,PLUS,TIMES,OVER,LPAREN,RPAREN,SEMI,ASSIGN,NUM,ID,INT,FLOAT,COMMA,DOLLAR t oken ty pe;/*记号*/typedef enum stmtk,expk nodekind;typedef enum ifk,assignk,declk stmtkind;typedef en

3、um opk,constk,idk expkind;typedef enum integer,real exptype;typedef struct treenode struct treenode * child3;struct treenode * sibling;nodekind nodek;exptype dtype ;union stmtkind stmt; expkind exp; kind;union tokentype op;int val;char * name; attr; treenode;typedef struct bucketchar * name; exptype

4、 dtype; struct bucket * next; bucket;bucket * hashtable211;/*tokentype token6=ID,ASSIGN,NUM,PLUS,NUM,INT,FLOAT,COMMA,DOLLAR;char tokenstring630=ab,:=,12,+,5,$;*/tokentype token=INT,ID,COMMA,ID,SEMI, ID,ASSIGN,NUM,PLUS,NUM,TIMES,NUM,SEMI,ID,ASSIGN,NUM,DOLLAR;char丄 I丄 | CCI r * 丄“ ii I ii n n nn n n n

5、 I n n _ n n 4 o * I ii ii c * *1* i* O * n n 1tokenstring30=int,ab,xy,;,ab,:=,12,+,5,*,3,;, xy,:=,34,$;int wordindex=0; /*以上两个数组的索引*/treenode*decl();treenode*factor();treenode*term();treenode*exp();treenode*assign_stmt();treenode*stmt_seq();pretraverse(treenode *); int hash(char * );void st_insert(

6、char*,exptype);int st_lookup (char*);void buildsymtab(treenode * );void setnodetype(treenode * );main()treenode * t;t=stmt_seq();/*语法分析建语法树*/buildsymtab(t);/*建符号表*/pretraverse(t); /*遍历语法树*/ setnodetype(t);/*类型检查设置*/treenode * stmt_seq()treenode*t;treenode*p;if( tokenwordindex=INT)|(tokenwordindex=FL

7、OAT) t=decl(); if(tokenwordindex=ID) t=assign_stmt();p=t;while( (tokenwordindex=SEMI)& (tokenwordindex!=DOLLAR)treenode * q;wordindex+; q=assign_stmt();p-sibling=q;p=q;return t;treenode * assign_stmt()treenode * t=(treenode *)malloc(sizeof(treenode); if(tokenwordindex=ID)t-nodek=stmtk; t-kind.stmt=a

8、ssignk; t-attr.name=tokenstringwordindex; wordindex+;else printf(error);exit(1);if(tokenwordindex=ASSIGN)wordindex+;else printf(error);exit(1);t-child0=exp();t-child1=NULL;t-child2=NULL; t-sibling=NULL;return t;treenode * exp()treenode * t;t=term();while(tokenwordindex=PLUS)|(tokenwordindex=MINUS) t

9、reenode * p=(treenode *)malloc(sizeof(treenode);p-nodek=expk; p-kind.exp=opk;p-attr.op=tokenwordindex; p-child0=t;t=p;wordindex+; t-child1=term();t-child2=NULL; t-sibling=NULL;return t;treenode * term()treenode * t=factor();while(tokenwordindex=TIMES)|(tokenwordindex=OVER) treenode * p=(treenode *)m

10、alloc(sizeof(treenode); p-nodek=expk;p-kind.exp=opk; p-attr.op=tokenwordindex;p-child0=t;t=p;wordindex+; t-child1=factor(); t-child2=NULL;t-sibling=NULL;return t;treenode * factor()treenode * t;switch(tokenwordindex)case LPAREN : wordindex+; t=exp(); if(tokenwordindex=RPAREN) wordindex+;else printf(

11、error);exit(1); break;case NUM : t=(treenode *)malloc(sizeof(treenode); t-nodek=expk;t-kind.exp=constk; t-attr.val=atoi(tokenstringwordindex); t-child0=NULL;t-child1=NULL; t-child2=NULL;t-sibling=NULL; wordindex+;break;case ID : t=(treenode *)malloc(sizeof(treenode); t-nodek=expk;t-kind.exp=idk;t-at

12、tr.name=tokenstringwordindex;wordindex+;break;default:printf(error);return t; pretraverse(treenode * t)if (t!=NULL) if (t-nodek=stmtk) printf(stmt-id:%sn,t-attr.name);if(t-nodek=expk&printf(exp-id:%sn,t-attr.name);if (t-nodek=expk&printf(exp-op:%dn,t-attr.op);if(t-nodek=expk&printf(exp-val:%dn,t-att

13、r.val);t-kind.exp=idk)t-kind.exp=opk)t-kind.exp=constk)if(t-child0!=NULL) pretraverse(t-child0);if(t-child1!=NULL) pretraverse(t-child1);if(t-child2!=NULL) pretraverse(t-child2);if(t-sibling!=NULL) pretraverse(t-sibling); treenode * decl()treenode * p,* q,* t1;treenode * t=(treenode *)malloc(sizeof(

14、treenode); t-nodek=stmtk;t-kind.stmt=declk; t-attr.name=tokenstringwordindex; t-child1=NULL;t-child2=NULL; wordindex+;if(tokenwordindex=ID)t1=(treenode *)malloc(sizeof(treenode); t-child0=t1;t1-nodek= expk;t1-kind.exp=idk; t1-attr.name=tokenstringwordindex; t1-child0=NULL;t1-child1=NULL;t1-child2=NULL;t1-sibling=NULL;wordindex+

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 学术论文 > 其它学术论文

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号