cdecl程序,用于分析C语言的声明
2010-04-04 10:59
387 查看
#include <stdio.h>
#include <ctype.h>
#include <stdlib.h>
#include <string.h>
#define STRCMP(a, R, b) (strcmp(a, b) R 0)
#define MAXTOKENS 100
#define MAXTOKENLEN 64
enum type_tag{ Identifer, Qualifier, Type };
struct token
{
char type;
char string[MAXTOKENLEN];
};
struct token stack[MAXTOKENS];
struct token this;
int top = -1;
#define pop stack[top--]
#define push(s) stack[++top] = s
void Read_to_first_identifer(void);
enum type_tag Classify_str(void); /*推断标识符的类型*/
void Gettoken(void);/*读取下一个标记到this*/
void Deal_with_declarator(void);
void Deal_with_arrays(void);
void Deal_with_function_args(void);
void Deal_with_pointers(void);
void Deal_wirh_declarator(void);
int main(void)
{
char next = 0;
printf("输入一个C语言声明,cdecl程序会把声明转化成通俗文字:/n");
do{
/*将标记压入堆栈,直至遇到标识符*/
Read_to_first_identifer();
Deal_with_declarator();
printf("/n是否进行下一个声明的推断(y//n)?");
}while('Y' == toupper( next = getchar() ));
return 0;
}
void Read_to_first_identifer(void)
{
Gettoken();
while(Identifer != this.type)
{
push(this);
Gettoken();
}
printf("%s is ", this.string);
Gettoken();
return;
}
void Gettoken(void)
{
char *p = this.string;
while( ' ' == (*p = getchar()) );/*忽略空格*/
if(isalnum(*p))/*A~z,0~9*/
{
while(isalnum( *++p = getchar() ));
ungetc(*p, stdin);
*p = '/0';
this.type = Classify_str();
return;
}
if('*' == *p)
{
strcpy(this.string, "pointer to");
this.type = '*';
return;
}
this.string[1] = '/0';
this.type = *p;
return;
}
enum type_tag Classify_str(void)
{
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, ==, "int")) return Type;
if(STRCMP(s, ==, "long")) return Type;
if(STRCMP(s, ==, "float")) return Type;
if(STRCMP(s, ==, "double")) return Type;
if(STRCMP(s, ==, "signed")) return Type;
if(STRCMP(s, ==, "unsigned")) return Type;
if(STRCMP(s, ==, "struct")) return Type;
if(STRCMP(s, ==, "union")) return Type;
if(STRCMP(s, ==, "enum")) return Type;
return Identifer;
}
void Deal_with_declarator(void)
{
/*处理标识符之后可能存在的函数或数组*/
switch(this.type)
{
case '[': Deal_with_arrays(); break;
case '(': Deal_with_function_args(); break;
default: break;
}
Deal_with_pointers();
/*处理在读入标识符之前压入堆栈的符号*/
while(-1 < top)
{
if('(' == stack[top].type)
{
pop;
Gettoken();
Deal_with_declarator();
}
else
printf("%s ", pop.string);
}
return;
}
void Deal_with_arrays(void)
{
while('[' == this.type)
{
printf("array ");
Gettoken();/*数字或']'*/
if(isdigit(this.string[0]))
{
printf("0..%d ", atoi(this.string)-1);
Gettoken();/*读取']'*/
}
Gettoken();/*读取']'之后的再一个标记*/
printf("of ");
}
return;
}
void Deal_with_function_args(void)
{
while(')' != this.type)
Gettoken();
Gettoken();
printf("function returning ");
return;
}
void Deal_with_pointers(void)
{
while('*' == stack[top].type)
printf("%s ", pop.string);
return;
}
farsight@farsight-ubuntu:~/chendan/link$ ./cdecl
输入一个C语言声明,cdecl程序会把声明转化成通俗文字:
int *p
p is pointer to int
是否进行下一个声明的推断(y/n)?y
char *const *(*next)()
next is pointer to function returning pointer to read-only pointer to char
#include <ctype.h>
#include <stdlib.h>
#include <string.h>
#define STRCMP(a, R, b) (strcmp(a, b) R 0)
#define MAXTOKENS 100
#define MAXTOKENLEN 64
enum type_tag{ Identifer, Qualifier, Type };
struct token
{
char type;
char string[MAXTOKENLEN];
};
struct token stack[MAXTOKENS];
struct token this;
int top = -1;
#define pop stack[top--]
#define push(s) stack[++top] = s
void Read_to_first_identifer(void);
enum type_tag Classify_str(void); /*推断标识符的类型*/
void Gettoken(void);/*读取下一个标记到this*/
void Deal_with_declarator(void);
void Deal_with_arrays(void);
void Deal_with_function_args(void);
void Deal_with_pointers(void);
void Deal_wirh_declarator(void);
int main(void)
{
char next = 0;
printf("输入一个C语言声明,cdecl程序会把声明转化成通俗文字:/n");
do{
/*将标记压入堆栈,直至遇到标识符*/
Read_to_first_identifer();
Deal_with_declarator();
printf("/n是否进行下一个声明的推断(y//n)?");
}while('Y' == toupper( next = getchar() ));
return 0;
}
void Read_to_first_identifer(void)
{
Gettoken();
while(Identifer != this.type)
{
push(this);
Gettoken();
}
printf("%s is ", this.string);
Gettoken();
return;
}
void Gettoken(void)
{
char *p = this.string;
while( ' ' == (*p = getchar()) );/*忽略空格*/
if(isalnum(*p))/*A~z,0~9*/
{
while(isalnum( *++p = getchar() ));
ungetc(*p, stdin);
*p = '/0';
this.type = Classify_str();
return;
}
if('*' == *p)
{
strcpy(this.string, "pointer to");
this.type = '*';
return;
}
this.string[1] = '/0';
this.type = *p;
return;
}
enum type_tag Classify_str(void)
{
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, ==, "int")) return Type;
if(STRCMP(s, ==, "long")) return Type;
if(STRCMP(s, ==, "float")) return Type;
if(STRCMP(s, ==, "double")) return Type;
if(STRCMP(s, ==, "signed")) return Type;
if(STRCMP(s, ==, "unsigned")) return Type;
if(STRCMP(s, ==, "struct")) return Type;
if(STRCMP(s, ==, "union")) return Type;
if(STRCMP(s, ==, "enum")) return Type;
return Identifer;
}
void Deal_with_declarator(void)
{
/*处理标识符之后可能存在的函数或数组*/
switch(this.type)
{
case '[': Deal_with_arrays(); break;
case '(': Deal_with_function_args(); break;
default: break;
}
Deal_with_pointers();
/*处理在读入标识符之前压入堆栈的符号*/
while(-1 < top)
{
if('(' == stack[top].type)
{
pop;
Gettoken();
Deal_with_declarator();
}
else
printf("%s ", pop.string);
}
return;
}
void Deal_with_arrays(void)
{
while('[' == this.type)
{
printf("array ");
Gettoken();/*数字或']'*/
if(isdigit(this.string[0]))
{
printf("0..%d ", atoi(this.string)-1);
Gettoken();/*读取']'*/
}
Gettoken();/*读取']'之后的再一个标记*/
printf("of ");
}
return;
}
void Deal_with_function_args(void)
{
while(')' != this.type)
Gettoken();
Gettoken();
printf("function returning ");
return;
}
void Deal_with_pointers(void)
{
while('*' == stack[top].type)
printf("%s ", pop.string);
return;
}
farsight@farsight-ubuntu:~/chendan/link$ ./cdecl
输入一个C语言声明,cdecl程序会把声明转化成通俗文字:
int *p
p is pointer to int
是否进行下一个声明的推断(y/n)?y
char *const *(*next)()
next is pointer to function returning pointer to read-only pointer to char
相关文章推荐
- 程序比程序员更理解c语言! cdecl程序(分析c语言的声明
- C专家编程:用python写cdecl程序,以分析C语言程序的声明语句
- 程序实现分析C语言的声明语句含义
- 写一个程序,用于分析一个字符串中各个单词出现的频率,并将单词和它出现的频率输出显示。(单词之间用空格隔开,如“Hello World My First Unit Test”);
- 【C语言】03-第一个C程序代码分析
- 一个生成COBOL测试数据的小程序——分析COBOL中数据声明的语句
- 分析复杂的C语言声明
- 【C语言】03-第一个C程序代码分析
- 如何分析复杂的C语言声明
- 【C语言】03-第一个C程序代码分析
- 《C专家编程》笔记二:第三章 分析C语言的声明
- 《C专家编程》第三章——分析C语言的声明
- C语言分析数据包程序
- c专家编程笔记之第三章分析c语言的声明
- 编写Java程序,模拟简单的计算器。 定义名为Number的类,其中有两个整型数据成员n1和n2,应声明为私有。 编写构造方法,用于初始化n1和n2,再为该类定义 加(addition)、 减(su
- C/C++拾遗录--关于一个C语言小程序的分析
- C语言声明的分析方法
- C编译器剖析_3.3 语法分析_C语言的外部声明(2)
- 实验一 词法分析程序的设计与实现(C语言)
- C语言拾遗(三):分析C语言声明