用法:运行该程序后,输入一个C语言声明,即可翻译成通俗易懂的语言
-
#include<stdio.h>
-
#include<string.h>
-
#include<ctype.h>
-
#include<stdlib.h>
-
-
#define MAXTOKENS 100
-
#define MAXTOKENLEN 64
-
-
enum type_tag{IDENTIFIER,QUALIFIER,TYPE};
-
-
struct token
-
{
-
char type;
-
char string[MAXTOKENLEN];
-
};
-
-
int top=-1;
-
struct token stack[MAXTOKENS];
-
struct token this;
-
-
#define pop stack[top--]
-
#define push(s) stack[++top]=s
-
-
enum type_tag classify_string()
-
{
-
char *s=this.string;
-
if(!strcmp(s,"const"))
-
{
-
strcpy(s,"read-only");
-
return QUALIFIER;
-
}
-
-
if(!strcmp(s,"volatile"))
-
return QUALIFIER;
-
if(!strcmp(s,"void"))
-
return TYPE;
-
if(!strcmp(s,"char"))
-
return TYPE;
-
if(!strcmp(s,"signed"))
-
return TYPE;
-
if(!strcmp(s,"unsigned"))
-
return TYPE;
-
if(!strcmp(s,"short"))
-
return TYPE;
-
if(!strcmp(s,"int"))
-
return TYPE;
-
if(!strcmp(s,"lone"))
-
return TYPE;
-
if(!strcmp(s,"float"))
-
return TYPE;
-
if(!strcmp(s,"double"))
-
return TYPE;
-
if(!strcmp(s,"struct"))
-
return TYPE;
-
if(!strcmp(s,"union"))
-
return TYPE;
-
if(!strcmp(s,"enum"))
-
return TYPE;
-
return IDENTIFIER;
-
};
-
-
void gettoken()
-
{
-
char *p=this.string;
-
-
while((*p=getchar())==' ');/*skip over the ' '*/
-
-
if(isalnum(*p))
-
{
-
while(isalnum(*++p=getchar()))/*read the next identifier starting with a-Z or 0-9*/
-
;
-
ungetc(*p,stdin);
-
*p='\0';
-
this.type=classify_string();
-
return;/*return to void function*/
-
}
-
-
if(*p=='*')
-
{
-
strcpy(this.string," pointer to ");
-
this.type='*';
-
return;
-
}
-
-
this.string[1]='\0';
-
this.type=*p;
-
return;
-
}
-
-
/*understand all the code stack in the analysis*/
-
read_to_first_identifier()
-
{
-
gettoken();
-
while(this.type!=IDENTIFIER)
-
{
-
push(this);/*stack[++top]=this*/
-
gettoken();
-
}
-
printf(" %s is ",this.string);
-
gettoken();
-
}
-
-
deal_with_arrays()
-
{
-
while(this.type=='[')
-
{
-
printf(" array ");
-
gettoken();/*read a number or ']'*/
-
if(isdigit(this.string[0]))
-
{
-
printf(" 0..%d ",atoi(this.string)-1);
-
gettoken();
-
}
-
gettoken();
-
printf(" of ");
-
}
-
}
-
-
deal_with_function_args()
-
{
-
while(this.
-
gettoken();
-
-
gettoken();
-
printf(" function returing ");
-
}
-
-
deal_with_pointers()
-
{
-
while(stack[top].type=='*')
-
printf("%s ",pop.string);
-
}
-
-
deal_with_declarator()
-
{
-
switch(this.type)
-
{
-
case '[':
-
deal_with_arrays();
-
break;
-
case '(':
-
deal_with_function_args();
-
}
-
-
deal_with_pointers();
-
-
while(top>=0)
-
{
-
if(stack[top].type=='(')
-
{
-
pop;
-
gettoken();
-
deal_with_declarator();
-
}
-
else
-
printf("%s ",pop.string);
-
}
-
}
-
-
int main()
-
{
-
read_to_first_identifier();
-
deal_with_declarator();
-
printf("\n");
-
return 0;
-
}
阅读(1700) | 评论(0) | 转发(0) |