views:

246

answers:

1

Hello, I have a few source code files, such hashtable.c and such. The main issue is that when I write my main.c as such:

#include "tokens.h"
#include <stdio.h>
void yyerror(char *errorMsg)
{
fprintf(stderr, "%s\n", errorMsg);
}


main() 
{
  yyparse();
  hsh = createHashtable();

}

And at the top of my yacc file (parser.y), I want to declear a hash table as such:

%{
#include <stdio.h>
#include "tokens.h"
#include "ast.c"


struct hashtable *hsh;
.............................
..............................

However I am getting this error.

main.c: In function ‘main’:
main.c:24: error: ‘hsh’ undeclared (first use in this function)
main.c:24: error: (Each undeclared identifier is reported only once
main.c:24: error: for each function it appears in.)
make: *** [main.o] Error 1

I am rather naive while it comes to C programming, any assistance will be greatful

+5  A: 

You need an extern struct hashtable* hsh; in your main.c

mrkj
can you be a little more specific, i'm confused
iva123
You need to declare in your main.c that the symbol 'hsh' is defined in some other compilation unit. This is accomplished with the 'extern' modifier; see http://wiki.answers.com/Q/What_is_the_use_of_extern_in_C
mrkj
It would be better if the user put the extern declaration in a header file and included that header in both the grammar and the main program. One of the two files should actually define the variable - doing it in the grammar is fine.
Jonathan Leffler