c - 在 yacc 中声明全局变量

标签 c global-variables yacc

我有一些源代码文件,例如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/

相关文章:

c - C中的wait()函数

c++ - 在 switch case 语句中初始化数组

c++ - 如何在visual studio 2022 for c/c++中指定多个调试命令?

asp.net - 如何声明其他 Web 表单也可以在 ASP.NET 中使用的全局变量

python全局变量未在for循环中定义

c - 结构类型上的 Yacc union

c - yacc 错误 : '' x' has no declared type

c - 分析 u-Boot/内核启动顺序

php - 将两个 JavaScript 变量传递给 PHP 变量

compiler-errors - 使用flex和yacc的整数 token