Three_Code.cpp
上传用户:wangchao
上传日期:2013-05-12
资源大小:9k
文件大小:2k
源码类别:

文本生成

开发平台:

Visual C++

  1. #include<stdio.h>
  2. #include <iostream.h>
  3. #include <string.h>
  4. #include <ctype.h>
  5. #define CODESIZE 100
  6. int i=1,j=0;
  7. char token;
  8. char id[20];
  9. typedef enum{plus,Assign} optype;
  10. typedef enum{opkind,constkind,idkind }nodekind;
  11. typedef struct streenode
  12. {
  13. nodekind kind;
  14. optype op;
  15. struct streenode *lchild,*rchild;
  16. int val;
  17. char * strval;    
  18. }streenode;
  19. typedef streenode* syntaxtree;
  20. void gencode(syntaxtree t)
  21. {
  22. char codestr[CODESIZE];
  23. if(t!=NULL)
  24. {
  25. switch(t->kind)
  26. {
  27. case opkind:
  28. switch(t->op)
  29. {
  30. case plus:
  31. gencode(t->lchild);
  32. gencode(t->rchild);
  33. t->strval=new char[10];
  34.             sprintf(t->strval,"t%d",i++);
  35. sprintf(codestr,"%s=%s+%s",t->strval,t->lchild->strval,t->rchild->strval);
  36. cout<<codestr<<endl;
  37. break;
  38. case Assign:
  39. gencode(t->lchild);
  40.             sprintf(codestr,"%s=%s",t->strval,t->lchild->strval);
  41.             sprintf(t->strval,t->lchild->strval);
  42. cout<<codestr<<endl;
  43. break;
  44. }
  45. break;
  46. case constkind:
  47.             break;
  48. case idkind:
  49. break;
  50. }
  51. }
  52. }
  53. syntaxtree exp();
  54. void getid()
  55. {
  56. token=getchar();
  57. while(isalnum(token))
  58. {
  59. id[j++]=token;
  60. token=getchar();
  61. }
  62. id[j]='';
  63. j=0;
  64. }
  65. syntaxtree factor()
  66. {
  67. syntaxtree temp;
  68. if(token=='(')
  69. {
  70. temp=exp();
  71. if(token!=')')
  72. {
  73. cout<<"error";
  74. }
  75. else getid();
  76. }
  77. else 
  78. {
  79. temp=new streenode;
  80. temp->kind=idkind;
  81. temp->strval=new char[10];
  82.         sprintf(temp->strval,id);
  83. temp->lchild=temp->rchild=NULL;
  84. }
  85. return temp;
  86. }
  87. syntaxtree aexp()
  88. {
  89.     syntaxtree temp;
  90. temp=factor();
  91. while(token=='+')
  92. {
  93.         syntaxtree temp1;
  94. temp1=new streenode;
  95. temp1->kind=opkind;
  96. temp1->op=plus;
  97. temp1->lchild=temp;
  98. getid();
  99. temp1->rchild=factor();
  100. temp=temp1;
  101. }
  102. return temp;
  103. }
  104. syntaxtree exp()
  105. {
  106.     getid();
  107. if(strlen(id)!=0&&token=='=')
  108. {
  109.          syntaxtree temp;
  110.  temp=new streenode;
  111.  temp->strval=new char[20];
  112.  sprintf(temp->strval,id);
  113.  temp->kind=opkind;
  114.  temp->op=Assign;
  115.  temp->rchild=NULL;
  116.  temp->lchild=exp();
  117.  return temp;
  118. }
  119. if(token!='=')
  120.     {
  121. syntaxtree temp1;
  122.         temp1=aexp();
  123. return temp1;
  124. }
  125. }
  126. int main(int argc, char* argv[])
  127. {
  128.     syntaxtree t;
  129. cout<<"请输入一个表达式:"<<endl;
  130. t=exp();
  131. cout<<"三地址代码如下:"<<endl;
  132. gencode(t);
  133. getchar();
  134. return 0;
  135. }