标题:这个程序有点问题,有哪位高手能帮我检查一下错在哪里了吗?
只看楼主
hwc125
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2010-3-29
结帖率:0
已结贴  问题点数:20 回复次数:3 
这个程序有点问题,有哪位高手能帮我检查一下错在哪里了吗?
#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;
}
搜索更多相关主题的帖子: 标识 
2010-12-24 14:05
venus85
Rank: 6Rank: 6
等 级:侠之大者
帖 子:159
专家分:477
注 册:2010-11-27
得分:7 
你要把问题讲具体点,你的程序那么长,不说详细点很难找的
2010-12-24 14:30
huaziforever
Rank: 1
等 级:新手上路
帖 子:7
专家分:8
注 册:2010-12-24
得分:7 
楼上正解,提问也是需要艺术的
2010-12-25 09:43
a343637412
Rank: 7Rank: 7Rank: 7
来 自:そ ら
等 级:黑侠
帖 子:357
专家分:620
注 册:2010-9-26
得分:7 
你要把问题讲具体点,你的程序那么长,不说详细点很难找的
        
        既然是提问,那么也考虑下,帮你解决问题的人.....        
2010-12-25 11:41



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-329803-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.021185 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved