Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/138.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/visual-studio/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ yyparse()的未解析外部符号_C++_Visual Studio_Bison - Fatal编程技术网

C++ yyparse()的未解析外部符号

C++ yyparse()的未解析外部符号,c++,visual-studio,bison,C++,Visual Studio,Bison,我的main.cpp如下所示: #include <assert.h> #include <fstream> #include <iostream> #include "astgen.h" #include "astexec.h" extern int yyparse(); extern FILE *yyin; int main() { //yydebug = 0; struct AstElement *a = 0; FILE *f

我的main.cpp如下所示:

#include <assert.h>
#include <fstream>
#include <iostream>
#include "astgen.h"
#include "astexec.h"

extern int yyparse();
extern FILE *yyin;

int main()
{
    //yydebug = 0;
    struct AstElement *a = 0;
    FILE *fp ;
    fopen_s(&fp,"example.txt","r");
    //std::cout<<fp;
    if (fp==NULL)
    {
        std::cout<<"error";
    }
    yyin=fp;
    yyparse();

    assert(a);
    struct ExecEnviron* e = createEnv();
    execAst(e, a);
    freeEnv(e);
    /* TODO: destroy the AST */
}
编辑1:我正在VS 2012环境中使用winflexbison生成解析器和词法分析器。构建的过程相当简单。只需添加

我还想指出,我采用了相同的方法构建了一个示例flex和bison文件,并随后集成到一个项目中。这对我很管用


我做错了什么?

您需要使用Bison从语法文件生成一个C文件。然后,您需要编译该C文件(默认名为
y.tab.C
),并将其与包含
main

定义的C文件链接。您的bison输入包括一个注释良好但过时的定义[注1]:

/* Since the parser must return the AST, it must get a parameter where
 * the AST can be stored. The type of the parameter will be void*. */
#define YYPARSE_PARAM astDest
如注释所示,这将导致
yyparse
的原型为:

int yyparse(void* astDest);
但是,在
main
函数中,您已经声明

extern int yyparse();
由于您正在使用C++编译此代码,<<代码> yyPARSE <代码>在Meal.CPP<代码>中声明,并由<代码>主< /COD>调用,与PysRel.Tab.H./Cudi>中声明的<代码> yyPARSE < /> >不相同,并在<代码>语法分析器.tab > CPP < /代码>中定义。因此,会出现链接器错误

如果您将
#include“parser.tab.h”
放入
main.cpp
文件中,而不是手动声明
yyparse
,您可能会看到一条更容易理解的错误消息,尽管我无法担保VS 2012生成的错误消息


注:

  • YYPARSE_PARAM
    自bison 1.875版(十多年前)以来就已被弃用,目前它仅适用于与yacc兼容的解析器。您应该使用
    %parse param
    声明,该声明适用于所有bison生成的解析器,并允许您指定实际的参数类型,而不必放弃类型安全性。有关详细信息,请参阅

  • 谢谢你的回复。我确实使用bison生成了解析所需的cpp文件。在VS 2012中,我正在使用winflexbison。因此生成的文件是parser.tab.cpp和parser.tab.h。另外,我在cpp中使用flex生成了所需的lexer等效项。错误是在执行这些步骤后产生的。@分段:由于问题是在构建中而不是(显然)语法中,您可能需要向我们更详细地展示构建步骤,而不是语法。
    extern int yyparse();