当前位置: 代码迷 >> 综合 >> 将C语言的声明翻译成通俗语言——cdecl
  详细解决方案

将C语言的声明翻译成通俗语言——cdecl

热度:29   发布时间:2024-01-16 13:38:10.0

《c专家编程》中的一个程序,可以将声明翻译为通俗语言,主要运用栈,挺难的,我自己是写不出的,照着书敲了一遍。

普通方法:

#include<stdio.h>
#include<string.h>
#include<ctype.h>
#include<stdlib.h>
#define MAXTOKENS 100
#define MAXTOKENLEN 64enum type_tag {IDENTIFIER,QUALIFIER,TYPE};struct token
{char type;char string[MAXTOKENLEN];
};int top = -1;
struct token stack[MAXTOKENS];
struct token now;#define pop stack[top--]
#define push(s) stack[++top]=senum type_tag classify_string(void)
{char *s = now.string;if (!strcmp(s, "const")){strcmp(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, "int")) return TYPE;if (!strcmp(s, "long")) 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(void)//读取下一个标记到now
{char *p =