gpt4 book ai didi

c - 混合 Bison 和 C 代码

转载 作者:太空宇宙 更新时间:2023-11-04 08:31:55 25 4
gpt4 key购买 nike

这是我的程序的一部分。

    %{
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <math.h>
int yylex(void);
int yylineno;
char* yytext;
void yyerror(const char *s) { printf("ERROR: %s\n", s); }
void addID(char *ID);

%}

%union{ char * string;}


%%
program:
|DECLARE vdeclarations IN commands END
;

vdeclarations:
vdeclarations IDENTIFIER {addID($2);}
| IDENTIFIER {addID($1);}
;

最后还有一些C函数

    struct list_ID {
char *ID;
int index;
struct list_ID * next;
};
typedef struct list_ID list_ID;
list_ID * curr, * head;
head = NULL;
int i = 0;
void addID(char *s)
{
curr = (list_ID *)malloc(sizeof(list_ID));
curr->ID = strdup(s);
curr->index = i++;
free(s);
curr->next = head;
head = curr;
}

我只是想将所有 IDENTIFIERS 添加到链表中,但 gcc 给我这样的错误。

kompilator.y:74:1: warning: data definition has no type or storage class [enable
d by default]
kompilator.y:74:1: error: conflicting types for 'head'
kompilator.y:73:19: note: previous declaration of 'head' was here
kompilator.y:74:8: warning: initialization makes integer from pointer without a
cast [enabled by default]
kompilator.y: In function 'addID':
kompilator.y:82:13: warning: assignment makes pointer from integer without a cas
t [enabled by default]
kompilator.y:83:7: warning: assignment makes integer from pointer without a cast
[enabled by default]

那么在 bison 中做这样的混合是不可能的吗?还是我的 C 代码部分有问题?

最佳答案

head = NULL; 

这是任何函数之外的语句。这是不允许的。

如果要初始化全局数据,在声明的时候进行:

list_ID * curr, * head = NULL;

此外,您不应该强制转换malloc 的结果。确保在使用 -Wall -Wextra 进行编译时出现零警告。

关于c - 混合 Bison 和 C 代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27942585/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com