c语言经一个字母赋值给变量,一个枚举变量赋值给一个字符变量?

这是源程序代码,有兴趣的童鞋可以看看~有关于栈的使用的。

函数的功能是,对一个变量和函数的声明的解读:如判断 char * const *(* next)();

这个是在讲什么?

#include

#include

#include

#include

#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 the;

#define pop stack[top--]

#define push(s) stack[++top] = s

enum type_tag classify_string(void)

{

char * s = the.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,"singed")) return TYPE;

if(!strcmp(s,"unsigned")) return TYPE;

if(!strcmp(s,"short")) return TYPE;

if(!strcmp(s,"int")) return TYPE;

if(!strcmp(s,"long")) return TYPE;

if(!strcmp(s,"float")) return TYPE;

if(!strcmp(s,"double")) return TYPE;

if(!strcmp(s,"union")) return TYPE;

if(!strcmp(s,"enum")) return TYPE;

return IDENTIFIER;

}

void gettoken(void)

{

char *p = the.string;

while((*p = getchar()) == ' ');

//如果是字母+数字,那么就把它的类型存入the.type,内同存入the.string

if(isalnum(*p))

{

while(isalnum(*++p = getchar()));

ungetc(*p,stdin);

*p = '\0';

the.type = classify_string();

return ;

}

//如果是*,那么就把它的类型存入the.type,内同存入the.string

if(*p == '*')

{

strcpy(the.string,"pointer to");

the.type = '*';

return ;

}

the.string[1] = '\0';  //字符串后面手动加上‘\0’

the.type = *p;  //如果不是前面两者,那么可能是(或[,先将它存入the.type中

return ;

}

void read_to_first_identifier()

{

gettoken();

while(the.type != IDENTIFIER) //如果没得到标识符,就将内容压入栈中,继续读下一个内容

{

push(the);

gettoken();

}

printf("%s is ",the.string); //跑出循环,说明得到标识符,输出标识符。

gettoken(); //再读下一个内容。

}

void deal_with_arrays()

{

while(the.type == '[')

{

printf("array ");

gettoken();

if(isdigit(the.string[0]))

{

printf("0..%d ",atoi(the.string)-1);

gettoken();

}

gettoken();

printf("of");

}

}

void deal_with_function_args()

{

while(the.type != ')')

{

gettoken();

}

gettoken();

printf("function returning ");

}

void deal_with_pointers()

{

while(stack[top].type == '*')

{

printf("%s ",pop.string);

}

}

void deal_with_declarator()

{

switch(the.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;

}

[本帖最后由 ysa555 于 2013-4-27 00:47 编辑]

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值