c专家编程里面的分析c语言声明的代码有关问题
c专家编程里面的分析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 thos;
#define pop stack[top--]
#define push(s) stack[++top] = s
enum type_tag classify_string(void)
/* 推断标识符的类型*/
{
char *s = thos.string;
if( !strcmp(s, "const") )
{
strcpy(s, "ready-only");
return QUALIFIER;
}
if( !strcmp(s, "volatile") ) return QUALIFIER;
if( !strcmp(s, "void") ) return TYPE;
if( !strcmp(s, "char") ) return TYPE;
if( !strcmp(s, "signde") ) 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, "struct") ) return TYPE;
if( !strcmp(s, "union") ) return TYPE;
if( !strcmp(s, "enum") ) return TYPE;
return IDENTIFIER;
}
void gettoken(void)
/* 读取下一个标记到“this” */
{
char *p = thos.string;
/* 略过空白字符 */
while( (*p == getchar()) == ' ' );
if( isalnum(*p) )
/* 原型:extern int isalnum(int c); 说明:当c为数字0-9或字母a-z及A-Z时,返回非零值,否则返回零。 */
{
/*读入的字符以A-Z,0-9开头*/
while( isalnum( *++p = getchar() ) );
ungetc(*p, stdin);
*p = '\0';
thos.type = classify_string();
return;
}
if( *p == '*')
{
strcpy(thos.string, "pointer to");
thos.type = '*';
return ;
}
thos.string[1] = '\0';
thos.type = * p;
return;
}
void read_to_first_identifer()
{
gettoken();
while( thos.type != IDENTIFIER )
{
push(thos);
gettoken();
}
printf("%s is ",thos.string);
gettoken();
}
void deal_with_arrays()
{
while( thos.type == '[' )
{
printf("array ");
gettoken(); /* 数字或者 ']' */
if( isdigit(thos.string[0]) )
{
printf("0..%d ",atoi(thos.string)-1);
gettoken();
}
gettoken();
printf("of ");
}
}
void deal_with_function_args()
{
while( thos.type != ')' )
{
gettoken();
}
gettoken();
printf("function returning ");
}
void deal_with_pointers()
{
while( stack[top].type == '*' )
{
printf("%s ", pop.string);
}
}
void deal_with_declarator()
/* 处理标识符以后的可能存在的函数/数组 */
{
switch(thos.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(void)
{
/* 将标记压入堆栈中,直到遇见标识符 */
read_to_first_identifer();
deal_with_declarator();
printf("\n");
return 0;
}
代码看书核实了好几遍 但是却得不到想到的结果
不管输入什么 都只有单一的输出 求解求改错
------解决方案--------------------
#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 thos;
#define pop stack[top--]
#define push(s) stack[++top] = s
enum type_tag classify_string(void)
/* 推断标识符的类型*/
{
char *s = thos.string;
if( !strcmp(s, "const") )
{
strcpy(s, "ready-only");
return QUALIFIER;
}
if( !strcmp(s, "volatile") ) return QUALIFIER;
if( !strcmp(s, "void") ) return TYPE;
if( !strcmp(s, "char") ) return TYPE;
if( !strcmp(s, "signde") ) 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, "struct") ) return TYPE;
if( !strcmp(s, "union") ) return TYPE;
if( !strcmp(s, "enum") ) return TYPE;
return IDENTIFIER;
}
void gettoken(void)
/* 读取下一个标记到“this” */
{
char *p = thos.string;
/* 略过空白字符 */
while( (*p == getchar()) == ' ' );
if( isalnum(*p) )
/* 原型:extern int isalnum(int c); 说明:当c为数字0-9或字母a-z及A-Z时,返回非零值,否则返回零。 */
{
/*读入的字符以A-Z,0-9开头*/
while( isalnum( *++p = getchar() ) );
ungetc(*p, stdin);
*p = '\0';
thos.type = classify_string();
return;
}
if( *p == '*')
{
strcpy(thos.string, "pointer to");
thos.type = '*';
return ;
}
thos.string[1] = '\0';
thos.type = * p;
return;
}
void read_to_first_identifer()
{
gettoken();
while( thos.type != IDENTIFIER )
{
push(thos);
gettoken();
}
printf("%s is ",thos.string);
gettoken();
}
void deal_with_arrays()
{
while( thos.type == '[' )
{
printf("array ");
gettoken(); /* 数字或者 ']' */
if( isdigit(thos.string[0]) )
{
printf("0..%d ",atoi(thos.string)-1);
gettoken();
}
gettoken();
printf("of ");
}
}
void deal_with_function_args()
{
while( thos.type != ')' )
{
gettoken();
}
gettoken();
printf("function returning ");
}
void deal_with_pointers()
{
while( stack[top].type == '*' )
{
printf("%s ", pop.string);
}
}
void deal_with_declarator()
/* 处理标识符以后的可能存在的函数/数组 */
{
switch(thos.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(void)
{
/* 将标记压入堆栈中,直到遇见标识符 */
read_to_first_identifer();
deal_with_declarator();
printf("\n");
return 0;
}
代码看书核实了好几遍 但是却得不到想到的结果
不管输入什么 都只有单一的输出 求解求改错
c语言
enum
栈
编程
c
------解决方案--------------------