编译原理实验四 代码生成.doc_第1页
编译原理实验四 代码生成.doc_第2页
编译原理实验四 代码生成.doc_第3页
编译原理实验四 代码生成.doc_第4页
编译原理实验四 代码生成.doc_第5页
免费预览已结束,剩余1页可下载查看

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

编译原理实验四 代码生成输入:语法树。输出:生成P代码存入codestrcodeindex。样例程序已经能生成P代码。文法:stmt_seq -statement ; stmt_seq | statementstatement-decl_stmt | assign_stmtdecl_stmt-type var_listtype-int |floatvar_list-id , var_list | idassign_stmt- id := expexp-exp + term | exp - term |termterm- term * factor | term * factor | factorfactor-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;typedef 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 char * name; exptype dtype; struct bucket * next; bucket;bucket * hashtable211;tokentype token=ID,ASSIGN,NUM,PLUS,NUM,TIMES,ID,SEMI,ID,ASSIGN,NUM,DOLLAR; char tokenstring30=ab,:=,12,+,5,*,x,;,xy,:=,34,$; int wordindex=0; /*以上两个数组的索引*/char codestr3030;int codeindex=0;treenode * t;treenode * decl();treenode * factor();treenode * term();treenode * exp();treenode * assign_stmt();treenode * stmt_seq();pretraverse(treenode *);expcode(treenode *);gencode(treenode *);main()int i; t=stmt_seq();pretraverse(t);gencode(t);for(i=0; isibling=q; p=q; return t;treenode * assign_stmt()treenode * t=(treenode *)malloc(sizeof(treenode); if(tokenwordindex=ID) t-nodek=stmtk; t-kind.stmt=assignk; =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) 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=term(); t-child2=NULL; t-sibling=NULL; return t;treenode * term()treenode * t=factor(); while(tokenwordindex=TIMES)|(tokenwordindex=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=exp(); 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=(treenode *)malloc(sizeof(treenode); t-nodek=expk; t-kind.exp=idk; =tokenstringwordindex; wordindex+; break; default: printf(error); return t;pretraverse(treenode * t)if (t!=NULL) if (t-nodek=stmtk) printf(stmt-id:%sn,); if (t-nodek=expk & t-kind.exp=idk) printf(exp-id:%sn,); if (t-nodek=expk & t-kind.exp=opk) printf(exp-op:%dn,t-attr.op); if (t-nodek=expk & t-kind.exp=constk) printf(exp-val:%dn,t-attr.val); 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(treenode); t-nodek=stmtk; t-kind.stmt=declk; =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; =tokenstringwordindex; t1-child0=NULL; t1-child1=NULL; t1-child2=NULL; t1-sibling=NULL; wordindex+; p=t1; while( tokenwordindex=COMMA) wordindex+; q=(treenode *)malloc(sizeof(treenode); q-nodek= expk; q-kind.exp=idk; =tokenstringwordindex; q-child0=NULL; q-child1=NULL; q-child2=NULL; q-sibling=NULL; p-sibling=q; wordindex+; p=q; return t; expcode(treenode * t)char * s1, * s2; if(t-child0!=NULL) expcode(t-child0); if(t-child1!=NULL) expcode(t-child1); if(t-nodek=expk & t-kind.exp=opk) if (t-attr.op=PLUS) strcpy(codestrcodeindex+,adi); if (t-attr.op=MINUS) strcpy(codestrcodeindex+,sbi); if (t-attr.op=TIMES) strcpy(codestrcodeindex+,mpi); if (t-attr.op=OVER) strcpy(codestrcodeindex+,ovi); if(t-nodek=expk & t-kind.exp=idk) strcpy(codestrcodeindex+,strcat(lod ,); if(t-nodek=expk & t-kind.exp=constk) strcpy(s1,ldc ); itoa(t-attr.val, s2,10); strcat(s1,s2); strcpy(codestrcodeindex+,s1); genco

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论