这个程序有点问题,有哪位高手能帮我检查一下错在哪里了吗?
#include <stdio.h>#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <ctype.h>
#define MAXTOKENLEN 80
#define MAXTOKENS 100
enum type_tag { IDENTIFIER, QUALIFIER, TYPE };
struct token {
char type;
char string[MAXTOKENLEN];
};
/*保存第一个标识符之前的所有标记*/
static struct token stack[MAXTOKENS];
/*保存栈顶指针*/
static int top = -1;
/*保存刚读入的那个标记*/
static struct token this = { 255, "" };
/*---------------栈操作---------------------*/
#define PUSH(elem) stack[++top] = elem
#define POP stack[top--]
/*--------------实用程序------------------*/
void input_elem(char *string)
{
bzero(this.string, MAXTOKENLEN);
strcat(this.string, string);
}
enum type_tag classify_string(void)//字符串分类
{
char *s = this.string;
if (strcmp(s, "const") == 0) {
strcpy(s, "read-only");
return QUALIFIER;
}
if (strcmp(s, "volatile") == 0)
return QUALIFIER;
if (strcmp(s, "void") == 0)
return TYPE;
if (strcmp(s, "char") == 0)
return TYPE;
if (strcmp(s, "signed") == 0)
return TYPE;
if (strcmp(s, "unsigned") == 0)
return TYPE;
if (strcmp(s, "short") == 0)
return TYPE;
if (strcmp(s, "int") == 0)
return TYPE;
if (strcmp(s, "long") == 0)
return TYPE;
if (strcmp(s, "float") == 0)
return TYPE;
if (strcmp(s, "double") == 0)
return TYPE;
if (strcmp(s, "struct") == 0)
return TYPE;
if (strcmp(s, "union") == 0)
return TYPE;
if (strcmp(s, "enum") == 0)
return TYPE;
return IDENTIFIER;
}
void gettoken(void)//取标记
{
char *p = this.string;
/**/
while ((*p = getchar()) == ' ')
;
if (isalnum(*p) != 0) {
while (isalnum(*++p = getchar()) != 0)
;
ungetc(*p, stdin);
*p = '\0';
this.type = classify_string();
return;
}
if (*p == '*') {
strcpy(this.string, "pointer to");
this.type = '*';
return;
}
this.string[1] = '\0';
this.type = *p;
return;
}
void read_to_first_identifier(void)//读至第一个标识符
{
gettoken();
while (this.type != IDENTIFIER) {
PUSH(this);
gettoken();
}
printf("declare %s as ", this.string);
gettoken();
}
/*---------------解析程序-----------------*/
void deal_with_function_args(void)// 处理函数参数
{
while (this.type != ')') {
gettoken();
}
gettoken();
printf("function returning ");
}
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 ");
}
}
void deal_with_any_pointers(void)//处理指针
{
while (stack[top].type == '*') {
printf("%s ", POP.string);
}
}
void deal_with_declarator(void)//处理声明器
{
switch (this.type) {
case '[':
deal_with_arrays();
break;
case '(':
deal_with_function_args();
break;
default:
break;
}
deal_with_any_pointers();
while (top >= 0) {
if (stack[top].type == '(') {
top--;
gettoken();
deal_with_declarator();
} else {
printf("%s ", POP.string);
}
}
}
int main(int argc , char *argv[])
{
pid_t pdt;
char backup[100];
int back_len;
if (argc == 1) {
pdt = fork();
if (pdt < 0) {
perror("fork");
exit(1);
} else if (pdt == 0) {
char string[100];
int ch;
int i = 0;
while (1) {
printf("cdel>>> ");
while ((ch = getchar()) != '\n') {
string[i++] = ch;
}
string[i] = '\0';
i = 0;
//scanf("%s", string);
if (strcmp(string, "exit") == 0) {
_exit(0);
} else if (string[0] == '\0') {
continue;
} else {
input_elem(string);
read_to_first_identifier();
deal_with_declarator();
putchar('\n');
}
}
} else {
wait(NULL);
return 0;
}
} else if (argc > 2) {
printf("Usage: %s [quote] \n", argv[0]);
exit(1);
} else {
bzero(backup, 100);
strcat(backup, &argv[1][1]);
back_len = strlen(backup);
backup[back_len-1] = '\0';
input_elem(backup);
read_to_first_identifier();
deal_with_declarator();
putchar('\n');
}
return 0;
}