我有一些源代码文件,例如hashtable.c等等。主要问题是,当我这样编写 main.c 时:
#include "tokens.h"
#include <stdio.h>
void yyerror(char *errorMsg)
{
fprintf(stderr, "%s\n", errorMsg);
}
main()
{
yyparse();
hsh = createHashtable();
}
在我的 yacc 文件 (parser.y) 的顶部,我想这样声明一个哈希表:
%{
#include <stdio.h>
#include "tokens.h"
#include "ast.c"
struct hashtable *hsh;
.............................
..............................
但是我收到此错误。
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
对于 C 编程,我相当天真,任何帮助都会非常感激
最佳答案
您的main.c
中需要一个extern struct hash table* hash;
关于c - 在 yacc 中声明全局变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1966051/