Chinaunix首页 | 论坛 | 博客
  • 博客访问: 417524
  • 博文数量: 121
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 1393
  • 用 户 组: 普通用户
  • 注册时间: 2014-03-11 12:17
个人简介

www.vibexie.com vibexie@qq.com

文章分类

全部博文(121)

文章存档

2015年(55)

2014年(66)

我的朋友

分类: C/C++

2014-10-13 20:03:19

用法:运行该程序后,输入一个C语言声明,即可翻译成通俗易懂的语言

点击(此处)折叠或打开

  1. #include<stdio.h>
  2. #include<string.h>
  3. #include<ctype.h>
  4. #include<stdlib.h>

  5. #define MAXTOKENS 100
  6. #define MAXTOKENLEN 64

  7. enum type_tag{IDENTIFIER,QUALIFIER,TYPE};

  8. struct token
  9. {
  10.     char type;
  11.     char string[MAXTOKENLEN];
  12. };

  13. int top=-1;
  14. struct token stack[MAXTOKENS];
  15. struct token this;

  16. #define pop stack[top--]
  17. #define push(s) stack[++top]=s

  18. enum type_tag classify_string()
  19. {
  20.     char *s=this.string;
  21.     if(!strcmp(s,"const"))
  22.     {
  23.         strcpy(s,"read-only");
  24.         return QUALIFIER;
  25.     }

  26.     if(!strcmp(s,"volatile"))
  27.         return QUALIFIER;
  28.     if(!strcmp(s,"void"))
  29.         return TYPE;
  30.     if(!strcmp(s,"char"))
  31.         return TYPE;
  32.     if(!strcmp(s,"signed"))
  33.         return TYPE;
  34.     if(!strcmp(s,"unsigned"))
  35.         return TYPE;
  36.     if(!strcmp(s,"short"))
  37.         return TYPE;
  38.     if(!strcmp(s,"int"))
  39.         return TYPE;
  40.     if(!strcmp(s,"lone"))
  41.         return TYPE;
  42.     if(!strcmp(s,"float"))
  43.         return TYPE;
  44.     if(!strcmp(s,"double"))
  45.         return TYPE;
  46.     if(!strcmp(s,"struct"))
  47.         return TYPE;
  48.     if(!strcmp(s,"union"))
  49.         return TYPE;
  50.     if(!strcmp(s,"enum"))
  51.         return TYPE;
  52.     return IDENTIFIER;
  53. };

  54. void gettoken()
  55. {
  56.     char *p=this.string;

  57.     while((*p=getchar())==' ');/*skip over the ' '*/
  58.     
  59.     if(isalnum(*p))
  60.     {
  61.         while(isalnum(*++p=getchar()))/*read the next identifier starting with a-Z or 0-9*/
  62.             ;
  63.         ungetc(*p,stdin);
  64.         *p='\0';
  65.         this.type=classify_string();
  66.         return;/*return to void function*/
  67.     }

  68.     if(*p=='*')
  69.     {
  70.         strcpy(this.string," pointer to ");
  71.         this.type='*';
  72.         return;
  73.     }

  74.     this.string[1]='\0';
  75.     this.type=*p;
  76.     return;
  77. }

  78. /*understand all the code stack in the analysis*/
  79. read_to_first_identifier()
  80. {
  81.     gettoken();
  82.     while(this.type!=IDENTIFIER)
  83.         {
  84.             push(this);/*stack[++top]=this*/
  85.             gettoken();
  86.         }
  87.     printf(" %s is ",this.string);
  88.     gettoken();
  89. }

  90. deal_with_arrays()
  91. {
  92.     while(this.type=='[')
  93.     {
  94.         printf(" array ");
  95.         gettoken();/*read a number or ']'*/
  96.         if(isdigit(this.string[0]))
  97.         {
  98.             printf(" 0..%d ",atoi(this.string)-1);
  99.             gettoken();
  100.         }
  101.         gettoken();
  102.         printf(" of ");
  103.     }
  104. }

  105. deal_with_function_args()
  106. {
  107.     while(this.
  108.         gettoken();

  109.     gettoken();
  110.     printf(" function returing ");
  111. }

  112. deal_with_pointers()
  113. {
  114.     while(stack[top].type=='*')
  115.         printf("%s ",pop.string);
  116. }

  117. deal_with_declarator()
  118. {
  119.     switch(this.type)
  120.     {
  121.         case '[':
  122.             deal_with_arrays();
  123.             break;
  124.         case '(':
  125.             deal_with_function_args();
  126.     }

  127.     deal_with_pointers();

  128.     while(top>=0)
  129.     {
  130.         if(stack[top].type=='(')
  131.         {
  132.             pop;
  133.             gettoken();
  134.             deal_with_declarator();
  135.         }
  136.         else
  137.             printf("%s ",pop.string);
  138.     }
  139. }

  140. int main()
  141. {
  142.     read_to_first_identifier();
  143.     deal_with_declarator();
  144.     printf("\n");
  145.     return 0;
  146. }

阅读(1700) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~