2022年编译原理实验三语义分析 .pdf
![资源得分’ title=](/images/score_1.gif)
![资源得分’ title=](/images/score_1.gif)
![资源得分’ title=](/images/score_1.gif)
![资源得分’ title=](/images/score_1.gif)
![资源得分’ title=](/images/score_05.gif)
《2022年编译原理实验三语义分析 .pdf》由会员分享,可在线阅读,更多相关《2022年编译原理实验三语义分析 .pdf(8页珍藏版)》请在淘文阁 - 分享文档赚钱的网站上搜索。
1、编译原理实验三语法分析并进行语义分析输入:经过词法分析后形成的token和 tokenstring 输出:检查有无语法错误,形成语法树,并检查是否符合语义。样例程序已经能对变量声明填符号表、进行类型检查。文法:stmt_seq -statement ; stmt_seq | statement statement-decl_stmt | assign_stmt decl_stmt-type var_list type-int |float var_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 #include typedef enum MINUS,PLUS,TIMES,OVER,LPAREN,RPAREN,SEMI,ASSIGN,NUM,ID,INT,FLOAT,COMMA,DOLLAR tokentype;/*记号 */ typedef enum stmtk,expk nodekind; typedef enum ifk,assignk,declk stmtkind; type
3、def enum 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 bucket 名师资
4、料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 1 页,共 8 页 - - - - - - - - - char * name; exptype dtype; struct bucket * next; bucket; bucket * hashtable211; /*tokentype token6=ID,ASSIGN,NUM,PLUS,NUM,INT,FLOAT,COMMA,DOLLAR; char tokenstring630=ab,:=,12,+,5,$;*/ tokentype
5、token=INT,ID,COMMA,ID,SEMI, ID,ASSIGN,NUM,PLUS,NUM,TIMES,NUM,SEMI,ID,ASSIGN,NUM,DOLLAR; char tokenstring30=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_
6、seq(); pretraverse(treenode *); int hash ( char * ); void st_insert( char * ,exptype); int st_lookup ( char * ); void buildsymtab(treenode * ); void setnodetype(treenode * ); main() treenode * t; t=stmt_seq(); /* 语法分析建语法树*/ buildsymtab(t); /*建符号表 */ pretraverse(t); /*遍历语法树 */ setnodetype(t); /*类型检查设
7、置*/ treenode * stmt_seq() treenode * t; treenode * p; if( tokenwordindex=INT)|(tokenwordindex=FLOAT) 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; 名师资料总结 - - -精品资料欢迎下载 - - - -
8、- - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 2 页,共 8 页 - - - - - - - - - return t; treenode * assign_stmt() treenode * t=(treenode *)malloc(sizeof(treenode); if(tokenwordindex=ID) t-nodek=stmtk; t-kind.stmt=assignk; t-attr.name=tokenstringwordindex; wordindex+; else printf(error); exit(1); if(
9、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) treenode * p=(treenode *)malloc(sizeof(treenode); p-nodek=expk; p-kind.ex
10、p=opk; p-attr.op=tokenwordindex; p-child0=t; t=p; wordindex+; t-child1=term(); t-child2=NULL; t-sibling=NULL; return t; 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 3 页,共 8 页 - - - - - - - - - treenode * term() treenode * t=factor(); while(tokenwordindex=TIMES)|(to
11、kenwordindex=OVER) treenode * p=(treenode *)malloc(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=e
12、xp(); if(tokenwordindex=RPAREN) wordindex+; else printf(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=(treen
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 2022年编译原理实验三语义分析 2022 编译 原理 实验 语义 分析
![提示](https://www.taowenge.com/images/bang_tan.gif)
限制150内