在C中使用struct作为类型时输入name name?

时间:2015-03-27 17:50:12

标签: c bison yacc lex

我不能为我的生活解释为什么我的代码没有编译。我正在使用最新的gcc版本进行编译,这段代码位于lex文件的底部(因此在使用flex后将其直接复制到.c文件中)这使我90%确定这个问题是由于我在C中做错了。这是引起问题的代码:

void checkAliases() {
    if (aliasHead==0) {
        printf("No aliases have been created.");
    }
    else {
    struct AliasNode* current = aliasHead;
    printf("Current Aliases: \n");
    while (current!=0) {
        printAlias(current);
        current = current->next;
    }
}

struct AliasNode {
    struct AliasNode* next;
    char* key;
    char* value;
};

struct AliasNode* aliasHead = 0;


void printAlias(struct AliasNode* alias) {
    char* toPrint = alias->key;
    printf(toPrint);
    printf(": ");
    toPrint = alias->value;
    printf(toPrint);
    printf("\n");
}

以下是我遇到的错误:

error: ‘aliasHead’ undeclared (first use in this function)
   if (aliasHead==0) {
       ^
error: unknown type name ‘AliasNode’
   AliasNode* current = aliasHead;

error: request for member ‘next’ in something not a structure or union
    current = current->next;
                     ^

error: unknown type name ‘AliasNode’
   AliasNode* next;
   ^

此yacc文件生成此C代码(以及我遗漏的数千行):

%token CD BYE ALIAS

%%

program: /*empty*/
     | program command
    ;

command:
    cd|bye|alias

cd:     
    CD     {printf("Not a valid directory\n");    }
    ;
bye:    
    BYE    {exit(0); }
     ;
alias:
    ALIAS   {
    if (aliasHead==0) {
        printf("No aliases have been created.");
    }
    else {
    AliasNode* current = aliasHead;
    printf("Current Aliases: \n");
    while (current!=0) {
        printAlias(current);
        current = current->next;
    }


    }
    }
    ;

    %%

    /*global variables*/

/*linked list to store alias - command pairs */
struct AliasNode {
    AliasNode* next;
    char* key;
    char* value;
};

struct AliasNode* aliasHead = 0;

void printAlias(struct AliasNode* alias) {
    char* toPrint = alias->key;
    printf(toPrint);
    printf(": ");
    toPrint = alias->value;
    printf(toPrint);
    printf("\n");
}

我完全不知道如何解决这些错误。任何帮助,即使只是在正确的方向上推动,都非常感谢。提前谢谢!

编辑:

我在Yacc文件中更改了我的代码,所以现在当我把它通过Bison时,我得到的.c有这个:

struct AliasNode {
AliasNode* next;
char* key;
char* value;
};

struct AliasNode* aliasHead = 0;

void printAlias(struct AliasNode* alias) {
    char* toPrint = alias->key;
    printf(toPrint);
    printf(": ");
    toPrint = alias->value;
    printf(toPrint);
    printf("\n");
}

void aliasNoPrompt() {
    if (aliasHead==0) {
        printf("No aliases have been created.");
    }
    else {
        AliasNode* current = aliasHead;
        printf("Current Aliases: \n");
        while (current!=0) {
            printAlias(current);
            current = current->next;
    }
}

但是,我仍然遇到上述相同的错误。请注意,从main,我调用aliasNoPrompt()。

2 个答案:

答案 0 :(得分:3)

将声明放在代码的顶部。 C要求代码按从上到下的顺序编写,这意味着您应该在使用之前定义AliasNode

struct AliasNode {
    struct AliasNode* next;
    char* key;
    char* value;
};

struct AliasNode* aliasHead = 0;

// ... other code here

答案 1 :(得分:2)

您的结构声明必须高于使用它的函数。


struct AliasNode {
    struct AliasNode* next;
    char* key;
    char* value;
};

struct AliasNode* aliasHead = 0;

void printAlias(struct AliasNode* alias) {
    char* toPrint = alias->key;
    printf(toPrint);
    printf(": ");
    toPrint = alias->value;
    printf(toPrint);
    printf("\n");
}

void checkAliases() {
if (aliasHead==0) {
        printf("No aliases have been created.");
    }
    else {
    struct AliasNode* current = aliasHead;
    printf("Current Aliases: \n");
    while (current!=0) {
        printAlias(current);
        current = current->next;
    }
}