[英]Mixing Bison and C code
Here is part of my program. 这是我程序的一部分。
%{
#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);}
;
and some C functions at the end 最后还有一些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;
}
I simply want to add all IDENTIFIERS to linked list but gcc give me such errors. 我只是想将所有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]
so isnt it possible to make such mixs in bison ? 那么有可能在野牛中做这样的混合吗? or there is something wrong with my C code part?
还是我的C代码部分有问题?
head = NULL;
This is a statement outside of any function. 这是任何函数之外的语句。 This is not allowed.
这是不允许的。
If you want to initialize global data, do it at the point of declaration: 如果要初始化全局数据,请在声明时进行:
list_ID * curr, * head = NULL;
In addition, you should not cast the result of malloc
. 另外,您不应该转换
malloc
的结果。 Make sure you have zero warnings while compiling with -Wall -Wextra.
使用
-Wall -Wextra.
进行编译时,请确保您的警告为零-Wall -Wextra.
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.