Reputation: 1861
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
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.
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?
Upvotes: 0
Views: 83
Reputation: 120079
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
. Make sure you have zero warnings while compiling with -Wall -Wextra.
Upvotes: 2